#JComponent not displaying/drawing

40 messages · Page 1 of 1 (latest)

south musk
#

if i have a file DrawLines with

public class DrawLine extends JComponent {
    ...
    @Override
    protected void paintComponent(Graphics graphics) {
        super.paintComponent(graphics);

        Graphics2D graphics2d = (Graphics2D) graphics;
        graphics2d.setStroke(new BasicStroke(this.width));
        graphics2d.draw(new Line2D.Float(this.x1, this.y1, this.x2, this.y2));
    }
}

why doesnt this draw a line when i use it in my main JPanel class?

public class LevelBuilder extends JFrame {
    public LevelBuilder() {
        super("Level Builder");
        ...
        for (int horLine = 0; horLine < CELLS_IN_COL; horLine++) {
            getContentPane().add(new DrawLine(...));
        }

        for (int verLine = 0; verLine < CELLS_IN_ROW; verLine++) {
            getContentPane().add(new DrawLine(...));
        }
    }
}
kindred novaBOT
#

This post has been reserved for your question.

Hey @south musk! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

south musk
errant seal
#

the bounds of the component are not specified, the components size is therefore 0 and it is not displayed

south musk
#

woah

errant seal
#

Btw you can achieve the same behavior by using a custom JPanel, you don't need to create that many objects for something so trivial

south musk
#

what do you mean a custom JPanel?

#

im a bit new to java haha

errant seal
#

a class that extends JPanel that overrides its paintComponent method and does custom drawing in there. You can then set that JPanel as the contentPane of the JFrame.

south musk
#

oh i see

#

so like what you had done earlier for me?

#

let me try that

#

but with my other DrawCells class

errant seal
south musk
#

it seems to work but newer and newer bugs pop up hahah

#

thanks a lot!

kindred novaBOT
# south musk thanks a lot!

If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.

south musk
#

ill keep fixing this one step at a time

errant seal
#

you can share these bugs here.

south musk
#

this is what my DrawCells.java looks like

public class DrawCell extends JComponent {
    private int xIdx;
    private int yIdx;
    private int size;
    private Color colour;

    public DrawCell(int xIdx, int yIdx, int size, Color colour) {
        this.xIdx = xIdx;
        this.yIdx = yIdx;
        this.size = size;
        this.colour = colour;

        this.setBounds(this.xIdx * this.size, this.yIdx * this.size, this.size, this.size);
        this.setOpaque(true);
        
    }

    @Override
    protected void paintComponent(Graphics graphics) {
        super.paintComponent(graphics);
        graphics.setColor(colour);
        graphics.fillRect(this.xIdx * this.size, this.yIdx * this.size, this.size, this.size);
    }
}
#

and when i do this in my main class

#
                    DrawCell cell = new DrawCell(mouseXIdx, mouseYIdx, CELL_SIZE, Color.black);
                    add(cell);
#

nothing pops up

#

BUT

#

if i get rid of * this.size in DrawCells.java i see a square pop up but in the wrong position

#

i need to multiply the size of the cell in order to get the rid placing

#

which is weird

errant seal
#

is xIx the horizontal and yIdx the vertical index ?

south musk
#

yes

#

top to bottom

#

left to right

errant seal
#

You are currently thinking in the wrong system. The coordinates that are used in the paintComponent method should be relative to the component. Therefore
graphics.fillRect(0,0,this.size,this.size);

south musk
#

oh...

#

oh wow that works

#

DAMN i am dumb

#

THANK YOU SO MUCH

kindred novaBOT
# south musk THANK YOU SO MUCH

If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.