startX | startY | endX | endY | |
---|---|---|---|---|
line 1 | L1X1: 0 | L1Y1: 140 | L1X2: 25 | L1Y2: 115 |
line 2 | L2X1: L1X2 | L2Y1: L1Y2 | L2X2: houseX | L2Y2: 145 |
line 3 | L3X1: houseX+houseW | L3Y1: 135 | L3X2: 245 | L3Y2: 130 |
line 4 | L4X1: L3X2 | L4Y1: L3Y2 | L4X2: TrunkX | L4Y2: 145 |
line 5 | L5X1: trunkX+trunkW | L5Y1: L4Y2 | L5X2: width | L5Y2: 110 |
Similar work is done for the roof of the house. With all these new blanks filled in, the program is complete.
import javax.swing.*; import java.awt.*; class HousePanel extends JPanel { final int width = 350, height = 250; final int houseX = 50, houseY = 100, houseW = 150, houseH = 100 ; final int doorY = 150, doorW = 20, doorH = 50 ; final int lWindX = 75, lWindY = 140, lWindW = 25, lWindH = 40 ; final int rWindX = 160, rWindY = 140, rWindW = 25, rWindH = 40 ; final int trunkX = 260, trunkY = 65, trunkW = 10, trunkH = 100 ; final int doorX = houseX + houseW/2 - doorW/2; // tree leaves final int treeX = 200, treeY = 10, treeW = 125, treeH = 55; // tree leaves final int L1X1 = 0, L1Y1 = 140, L1X2 = 25, L1Y2 = 115; final int L2X1 = L1X2, L2Y1 = L1Y2, L2X2 = houseX, L2Y2 = 145; final int L3X1 = houseX+houseW, L3Y1 = 135, L3X2 = 245, L3Y2 = 130; final int L4X1 = L3X2, L4Y1 = L3Y2, L4X2 = trunkX, L4Y2 = 145; final int L5X1 = trunkX+trunkW, L5Y1 = L4Y2, L5X2 = width, L5Y2 = 110; final int roof1X1 = houseX, roof1Y1 = houseY; final int roof1X2 = houseX + houseW/2, roof1Y2 = 75; final int roof2X1 = roof1X2, roof2Y1 = roof1Y2; final int roof2X2 = houseX + houseW, roof2Y2 = houseY; public HousePanel() { setPreferredSize( new Dimension( width, height) ); setBackground( Color.WHITE); } public void paintComponent ( Graphics gr ) { super.paintComponent( gr ); gr.setColor( Color.orange ); // there is no color brown gr.drawRect( houseX , houseY , houseW, houseH); // house gr.drawRect( doorX , doorY , doorW , doorH ); // door gr.drawRect( lWindX , lWindY , lWindW, lWindH); // lwind gr.drawRect( rWindX , rWindY , rWindW, rWindH); // rwind gr.drawRect( trunkX , trunkY , trunkW, trunkH); // trunk gr.drawLine( roof1X1, roof1Y1, roof1X2, roof1Y2 ); gr.drawLine( roof2X1, roof2Y1, roof2X2, roof2Y2 ); gr.setColor( Color.green ); gr.drawOval( treeX, treeY, treeW, treeH ); gr.drawLine( L1X1, L1Y1, L1X2, L1Y2 ); // line 1 gr.drawLine( L2X1, L2Y1, L2X2, L2Y2 ); // line 2 gr.drawLine( L3X1, L3Y1, L3X2, L3Y2 ); // line 3 gr.drawLine( L4X1, L4Y1, L4X2, L4Y2 ); // line 4 gr.drawLine( L5X1, L5Y1, L5X2, L5Y2 ); // line 5 } } public class HouseComplete { public static void main ( String[] args ) { JFrame frame = new JFrame( "House Picture: Complete" ); frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); frame.getContentPane().add( new HousePanel() ); frame.pack(); frame.setVisible( true ); } }
If your entire objective was to draw this picture on the computer screen, is writing this program a sensible way to do so?