#Java Swing color is allways (51, 51, 51)

1 messages ยท Page 1 of 1 (latest)

calm isleBOT
#

Detected code, here are some useful tools:

Formatted code
if (jFrame == null ) {
  jFrame = new JFrame();
  canvas = new Canvas();
  jFrame.add(canvas);
  canvas.setSize(280, 280);
  jFrame.setSize(280, 280);
  jFrame.setVisible(true);
}
for (int x = 0; x < 28; x++) {
  for (int y = 0; y < 28; y++) {
    canvas.getGraphics().setColor(Color.BLUE);
    System.out.println(canvas.getGraphics().getColor());
    canvas.getGraphics().fillRect(x * 10, y * 10, 10, 10);
  }
}
#

<@&987246487241105418> please have a look, thanks.

calm isleBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

#

Here is an AI assisted attempt to answer your question ๐Ÿค–. Maybe it helps! In any case, a human is on the way ๐Ÿ‘. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.

#
            @Override
            public void paint(Graphics g) {
                super.paint(g);
                g.setColor(Color.RED);
                g.fillRect(0, 0, getWidth(), getHeight());
            }
        });
        jFrame.add(canvas);
        jFrame.setSize(500, 500);
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jFrame.setVisible(true);
    }
}

This code should create a JFrame with a red background color. However, when I run it, the background color is always gray. What am I doing wrong?

young mauve
#

@last topaz try storing the graphics you get from canvas.getGraphics

#
if (jFrame == null) {
            jFrame = new JFrame();
            canvas = new Canvas();
            jFrame.add(canvas);
            canvas.setSize(280, 280);
            jFrame.setSize(280, 280);
            jFrame.setVisible(true);
        }
        for (int x = 0; x < 28; x++) {
            for (int y = 0;y < 28; y++) {
                var g = canvas.getGraphics();
                g.setColor(Color.BLUE);
                System.out.println(g.getColor());
                g.fillRect(x * 10, y * 10, 10, 10);
            }
        }
#

if i had to guess thats the problem

wary trellis
#

My bet is that you are not modifying the graphics during paint

#

You have to override it

#

Like always

last topaz
last topaz
#

oh yeah fixed it

#

thank you!

quiet kestrel
#

I think you are supposed to use JFrame#repaint

wary trellis
calm isleBOT
#

Closed the thread due to inactivity.

If your question was not resolved yet, feel free to just post a message to reopen it, or create a new thread. But try to improve the quality of your question to make it easier to help you ๐Ÿ‘