Results 1 to 6 of 6

Thread: Object matrix

  1. #1
    MichaelG is offline Member
    Join Date
    Apr 2013
    Posts
    4
    Rep Power
    0

    Default Object matrix

    Hello guys,

    I am completely new in Java and I have to create a class draws a filled rectangle on screen
    I should create an applet that creates 16 instances of the class that draws the filled rectangle and show them in an applet in a 4x4 grid.
    I already have the class and have created two instances of the object (just for testing), the problem I have is that I am getting the first box in the position I want (x=4, y=5, w=50, h=50) but the second instance is shown at the bottom end of the frist box, I mean that the boxes are shown in diagonal and it doesn't displays next the first box, something like this:

    Object matrix-image.jpg

    Does anybody could give me any advice on how I could create this grid?

    Thank you all
    Last edited by MichaelG; 04-13-2013 at 12:12 AM.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Object matrix

    We'll need to see some code! Post what you are using to try and get the rectangles to show up net to one another. (Your approach of starting with simple code that displays just 2 boxes is the right one.)

  3. #3
    MichaelG is offline Member
    Join Date
    Apr 2013
    Posts
    4
    Rep Power
    0

    Default Re: Object matrix

    Hi pbrockway2, Thank you for answering!

    Here is the code:

    ########## BOX CLASS ##########

    import java.awt.Color;
    import java.awt.Component;
    import java.awt.Graphics;


    public class Box extends Component{
    private int upperLeftX, upperLeftY, height, width;
    private Color boxColor;

    public Box (int upperX, int upperY, int h, int w, Color myColor)
    {
    setUpperLeftX(upperX);
    setUpperLeftY(upperY);
    setHeight(h);
    setWidth(w);
    setBoxColor(myColor);
    }

    public void display(Graphics g)
    {
    g.setColor(getBoxColor());
    g.fillRect(getUpperLeftX(), getUpperLeftX(), getWidth(), getHeight());
    }

    public int getUpperLeftX() {
    return upperLeftX;
    }
    public void setUpperLeftX(int upperLeftX) {
    this.upperLeftX = upperLeftX;
    }
    public int getUpperLeftY() {
    return upperLeftY;
    }
    public void setUpperLeftY(int upperLeftY) {
    this.upperLeftY = upperLeftY;
    }
    public int getHeight() {
    return height;
    }
    public void setHeight(int height) {
    this.height = height;
    }
    public int getWidth() {
    return width;
    }
    public void setWidth(int width) {
    this.width = width;
    }
    public Color getBoxColor() {
    return boxColor;
    }
    public void setBoxColor(Color boxColor) {
    this.boxColor = boxColor;
    }
    }

    ########## GRID APPLET ##########

    import java.applet.Applet;
    import java.awt.*;

    public class Grid extends Applet {

    public void paint(Graphics g)
    {
    this.setLayout(null);

    int uX=0, uY=0, wDth=45, hGht=45;
    Color col = Color.blue;

    Box myBox = new Box(uX, uY, wDth, hGht, col);
    myBox.display(g);

    col = Color.red;
    uX=45;

    Box myBox1 = new Box(uX, uY, 45, 45, col);
    myBox1.display(g);
    }
    }

  4. #4
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    335
    Rep Power
    4

    Default Re: Object matrix

    Hi MichaelG, welcome to the forums.

    Please wrap your code in [code] tags as it makes it easier to read and reference.

    Unless specifically desired you would be better using a layout manager such as Grid to achieve your requirements.
    A Visual Guide to Layout Managers (The Java™ Tutorials > Creating a GUI With JFC/Swing > Laying Out Components Within a Container)

    But to correct your current issue, your problem is within the display() method of the Box class. When drawing the filled rectangle you are using getUpperLeftX() for both the x and y coordinates.

    Regards.

  5. #5
    MichaelG is offline Member
    Join Date
    Apr 2013
    Posts
    4
    Rep Power
    0

    Default Re: Object matrix

    Hi Ronin,
    Next time I will wrap my code in [code] tags as suggested. Sorry for this.
    Thank you very much for helping and SHAME ON ME! I didn't noticed I've used the same "get" for both coordinates!

    Best regards.

  6. #6
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    335
    Rep Power
    4

Similar Threads

  1. Replies: 1
    Last Post: 12-08-2012, 07:20 PM
  2. help in matrix
    By eng_hyzoom in forum New To Java
    Replies: 10
    Last Post: 03-11-2012, 07:32 AM
  3. Matrix
    By awesom in forum New To Java
    Replies: 9
    Last Post: 10-05-2011, 08:36 PM
  4. help in matrix
    By Engineer in forum New To Java
    Replies: 7
    Last Post: 10-06-2010, 01:26 PM
  5. Help with matrix
    By susan in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 04:37 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •