Drawing 3 simple houses. - brandonio21 - 10-05-2011
I have recently designed a bit of code for a school assignment that actually draws 3 houses, each one with a specified height and width. It is based on an OOP structure. So here is the house.java code:
Code: //House: Assignment P4.9 by Brandon Milton
/*
*
*
* Written by brandonio21
* http://brandonsoft.com
* http://bpforums.info
*
* Use as you'd like!
*/
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D.Double;
public class house extends Applet {
int height;
int width;
int x;
int y;
public house(int theight, int twidth, int tx, int ty){
height = theight;
width = twidth;
x = tx;
y = ty;
}
public void draw(Graphics2D g2){
Rectangle2D.Double base = new Rectangle2D.Double(x,y,width,height); //Create the square that will be the bottom of the house
int doorHeight = 0;
if (height < 50){
doorHeight = height;
} else{
doorHeight = 50;
}
//^ Size the door to the house if it is small
Rectangle2D.Double door = new Rectangle2D.Double(x + 20,y + (height - 50),20,doorHeight); //Create the door
Rectangle2D.Double window = new Rectangle2D.Double(x + 50,(y + height)-60,20,20); //Create the window
Line2D.Double leftRoof = new Line2D.Double(x,y,x + (width/2),y- (height/2)); //Create the left side of the roof
Line2D.Double rightRoof = new Line2D.Double(x + (width/2),(y - (height/2)),x+width,y); //Create the right side of the roof
g2.draw(base);
g2.draw(door);
g2.draw(window);
g2.draw(leftRoof);
g2.draw(rightRoof);
//Draw them all!
}
}
And here is the code for the tester class.. This is just an example, you can recode this to your needs if you are using it!
Code: //Assignment P4.9 by Brandon Milton
/*
*
*
* Coded by brandonio21
* http://brandonsoft.com
* http://bpforums.info
*
* Feel free to use wherever you'd like!
*/
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.applet.Applet;
import javax.swing.JOptionPane;
public class houseTester extends Applet {
int width1 = Integer.parseInt(JOptionPane.showInputDialog("Width of house1")); //Creates an input dialog
int width2 = Integer.parseInt(JOptionPane.showInputDialog("Width of house2"));//Creates an input dialog
int width3 = Integer.parseInt(JOptionPane.showInputDialog("Width of house3"));//Creates an input dialog
int height1 = Integer.parseInt(JOptionPane.showInputDialog("What is the height of house1"));//Creates an input dialog
int height2 = Integer.parseInt(JOptionPane.showInputDialog("What is the height of house2"));//Creates an input dialog
int height3 = Integer.parseInt(JOptionPane.showInputDialog("What is the height of house3"));//Creates an input dialog
int x = Math.max(height1, height2);
int maxheight = Math.max(x, height3);
//Finds the tallest house height
public void paint(Graphics g){
setSize(width1+width2+width3 + 25, (maxheight * 5)); //Sets the window size to give 25px of padding on the right, and a large amount of vertical padding
Graphics2D g2 = (Graphics2D)g;
house hs = new house(height1,width1,0,(0 + (height1*2))); //Draw house with user specs.
hs.draw(g2);
house hs2 = new house(height2,width2,width1 + 5,0 + (height2*2));//Draw house with user specs.
hs2.draw(g2);
house hs3 = new house(height3,width3,0 + (width1 + width2) + 5,0 + (height3*2));//Draw house with user specs.
hs3.draw(g2);
}
}
Here is the output of the already implemented code(With 100 set for every value):
[attachment=0]<!-- ia0 -->Houses.png<!-- ia0 -->[/attachment]
Enjoy! <!-- s --><img src="{SMILIES_PATH}/icon_e_smile.gif" alt="" title="Smile" /><!-- s -->
Re: Drawing 3 simple houses. - openeXpressions - 11-01-2011
How do you get the 4th caption button?
Re: Drawing 3 simple houses. - brandonio21 - 11-01-2011
I don't exactly know what you mean, could you possibly attach a screenshot of what you want an explanation of?
Re: Drawing 3 simple houses. - openeXpressions - 11-01-2011
The little button with arrows on each side in the NonClientArea.
Re: Drawing 3 simple houses. - brandonio21 - 11-02-2011
Ah, that is actually Teamviewer, I can click it and immediately broadcast a live stream of that window to anyone in my partner list.
|