Jump to content

Java help

Tuero

So I am working on a small project, and I am having a bit of trouble. 

 

Here is the task that I need to do

1) - Create a class called Box.java, which should have instance variables (upperLeftX, upperLeftY, height, width, and color)

- provide the body of the following constructor that sets the values of the instance variables: public Box(int upperX, int upperY, int h, int w, Color myColor)

- create a method called display(Graphics g) that uses SetColor(boxColor) and fillRect(upperLeftX, upperLeftY, width, height) to fill the box

 

2) create a class named Grid.java which should create a 4x4 grid of boxes (16 instances of the box class should be created)

 

To start off, I began to fill the code for Box.java (Note: the garbage after the variable initializations at the top are getters and setters which we were told to create using the build in feature of Eclipse)

 

import java.awt.*;

import java.applet.Applet;
 
 
public class Box extends Applet {
private Integer upperLeftX, upperLeftY, height, width;
private Color boxColor;
 
public Integer getUpperLeftX() {
return upperLeftX;
}
public void setUpperLeftX(Integer upperLeftX) {
this.upperLeftX = upperLeftX;
}
public Integer getUpperLeftY() {
return upperLeftY;
}
public void setUpperLeftY(Integer upperLeftY) {
this.upperLeftY = upperLeftY;
}
public Integer getHeight() {
return height;
}
public void setHeight(Integer height) {
this.height = height;
}
public Integer getWidth() {
return width;
}
public void setWidth(Integer width) {
this.width = width;
}
public Color getBoxColor() {
return boxColor;
}
public void setBoxColor(Color boxColor) {
this.boxColor = boxColor;
}
 
public Box(int upperX, int upperY, int h, int w, Color myColor){
upperLeftX = upperX;
upperLeftY = upperY;
height = h;
width = w;
boxColor = myColor;
}
 
public void display(Graphics g){
g.setColor(boxColor);
g.fillRect(upperLeftX, upperLeftY, width, height);
}
 
}

 

Then I created the code for Grid.java (Note: I am just testing to see if I can make 1 box, then I can just copy and replicate for the reaminig 15 boxes):

 

import java.awt.*;

import java.applet.Applet;
 
public class Grid {
public static void main(String args[]){
Box box1 = new Box(10,10,20,20,Color.RED);
box1.display();
}
 
}

 

Coming from python, in Box.java the box object is initialized in the Box method, and the display method can be called from Grid.java by using the object.method(). However, to call the display method, it needs a graphics object for the method. Where is this graphics object coming from in order for the code to be valid?

Link to comment
Share on other sites

Link to post
Share on other sites

I believe you need to import this line in the box class.

import java.awt.Graphics;

Also, I think you need a JFrame to display it.

 

I am a novice programmer, so this is what I learned. I hope someone else has a better way or can confirm this method.

Link to comment
Share on other sites

Link to post
Share on other sites

import java.awt.*;public class Box {    int x, y, width, height;    Color color;    Public Box(int x, int y, int width, int height, Color color) {        this.x = x;        this.y = y;        this.width = width;        this.height = height;        this.color = color;    }    public void display(Graphics g) {        g.setColor(color);        g.fillRect(x, y, width, height);    }}
public class Grid {    for (int y = 0; y < 4; y++) {        for (int x = 0; x < 4; x++) {            new Box(x * width, y * height, width, height, Color.RED).display();        }    }}

Something like this is what the program should look like, you'll need a frame and a way to display the graphics though.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×