-
Random drawRect
Hi there.
This is my first code post on this site. I am trying to create a building on the screen, really it is just a rectangle. Which I would like to have random parameters and then I will use a loop to make 5 of them once i have the first part figured out.
Code:
import java.awt.*;
import javax.swing.*;
// new definition of a building class which will make a new
// frame and that frame will store some rect. images
public class Skyline {
public static void main (String[] args){
// create a new frame
JFrame frame = new JFrame("Skyline");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
// here the program will ask for a new panel which will get its definition
// from the other .class file
frame.getContentPane().add(new SkylinePanel());
frame.pack();
frame.setVisible(true);
}
}
Code:
import java.awt.*;
import javax.swing.*;
// new definition of a building class which will make a new
// frame and that frame will store some rect. images
public class Building {
// set private varibles even tho the whole program will be able to access them... I think
private int areaX, areaY, x, y;
private Color color;
// Constructor get the building blueprints
public Building (int sizeX, int sizeY, Color shade, int upperX, int upperY) {
areaY = sizeY;
areaX = sizeX;
color = shade;
x = upperX;
y = upperY;
}
// makes the draw method which will be called in the driver program
// this will use the graphics method
public void draw (Graphics page) {
// set the color and passes a varible to get the color
page.setColor (color);
// sets the rect properties
page.fillRect (x, y, areaX, areaY);
}
// area mutator
public void setAreaX ( int sizeX ) {
areaX = sizeX;
}
// Color mutator
public void setColor ( Color shade ) {
color = shade;
}
// x mutator
public void setX ( int upperX ) {
x = upperX;
}
// y mutator
public void setY ( int upperY ) {
y = upperY;
}
// area accessor
public int getAreaX () {
return areaX;
}
// area accessor
public int getAreaY () {
return areaY;
}
// Color accessor
public Color getColor () {
return color;
}
// x accessor
public int getX () {
return x;
}
// y accessor
public int getY () {
return y;
}
}
Code:
import java.awt.*;
import javax.swing.*;
// new definition of a building class which will make a new
// frame and that frame will store some rect. images
public class Building {
// set private varibles even tho the whole program will be able to access
private int areaX, areaY, x, y;
private Color color;
// Constructor get the building blueprints
public Building (int sizeX, int sizeY, Color shade, int upperX, int upperY) {
areaY = sizeY;
areaX = sizeX;
color = shade;
x = upperX;
y = upperY;
}
// makes the draw method which will be called in the driver program
// this will use the graphics method
public void draw (Graphics page) {
// set the color and passes a varible to get the color
page.setColor (color);
// sets the rect properties
page.fillRect (x, y, areaX, areaY);
}
// area mutator
public void setAreaX ( int sizeX ) {
areaX = sizeX;
}
// Color mutator
public void setColor ( Color shade ) {
color = shade;
}
// x mutator
public void setX ( int upperX ) {
x = upperX;
}
// y mutator
public void setY ( int upperY ) {
y = upperY;
}
// area accessor
public int getAreaX () {
return areaX;
}
// area accessor
public int getAreaY () {
return areaY;
}
// Color accessor
public Color getColor () {
return color;
}
// x accessor
public int getX () {
return x;
}
// y accessor
public int getY () {
return y;
}
}
This is for my java class and the code is modified from Java Software Solutions foundations of program design 4th edition Chapter 4 page 180-181
It very modified tho.
Thanks for any help. I believe that the problem is with this statment :
new Building ( rndZ, rndB, Color.blue, rndC, rndD ).draw(page);
not sure tho.
-
Ehm, you copied the class building twice, so I assume one of those should be the class SkylinePanel.
-
Haha
like it says my first post lawl !
ok sorry about that part
here is the code
Code:
import java.awt.*;
import javax.swing.*;
// name the driver program
public class SkylinePanel extends JPanel {
// declare the amount of buildings to be placed on the panel
private Building building1;
public SkylinePanel(){
int rndZ =(int)((Math.random()*10));
building1 = new Building ( rndZ, rndZ, Color.blue, rndZ, rndZ );
setPreferredSize (new Dimension(300, 200));
setBackground (Color.black);
}
// sets the buildings which where declared earlier
// uses the paint method to display to the screen
// this also calls the draw method which is in the file skyline
public void paintComponent (Graphics page) {
super.paintComponent(page);
building1.draw(page);
}
}
I have changed it several times putting the call for the Building class in the paintComponent area , like this new Building (rndZ, rndZ, Color.blue, rndZ, rndZ ).draw(page);
I sometimes can get a black screen to appear other times nothing.
-
Interesting no one has any info on this... seems super easy
-
Congratulations. What's the problem/question?
-
Is this what you want?
The question is at the top toadaly...
I have an idea, but it may not be exactly what you are looking for.
I think that your problem is that you are creating a rectangle in Building, and only creating one Building in SkylinePanel. The code creating your rectangle is this:
Code:
private Building building1;
I believe you really want this statement
Code:
private Building building1, building2, building3, building4, building5;
You would then have to position the rectangles created by this statement. I'm not sure how you should do that though.
Hope this helps! :)