Can't set color to JPanel in Java

can synonym
can definition
can verb
can noun meaning
can.'' acronym
cant
can band
could

I have just started with Graphics in java, and I'm already stuck. I have tried to set the color of the JPanel to red but nothing seems to work! Any help is highly appreciated.

JFrame class:

import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.Color;


public class redBoxFrame{

    public static void main(String[]args){
        JFrame f = new JFrame();
        f.setSize(400, 200);
        f.setTitle("A red box");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel p = new redBoxPanel();
        p.setBackground(Color.RED);
        f.add(p);
        f.setVisible(true);
  }

} 

JPanel class:

   import java.awt.Graphics;
   import javax.swing.JPanel;
   import java.awt.Color;

  public class redBoxPanel extends JPanel {

     public void paintComponent(Graphics g){
     g.fillRect(0, 0, 100, 100);
     g.setColor(Color.RED);

     }
  }

As you can see I have both tried to declare the color in the JFrame class and in the JPanel class but none of them seem to work. Thank you!

Everyone here seems to miss the fact that the colour should be set before drawing.

I'll set the main background to BLUE for demo purposes.

public static void main(String[] args) {
    //...
    JPanel p = new redBoxPanel();
    // BLUE bg. This covers the whole panel.
    p.setBackground(Color.BLUE);
    //...
}

And now for the red box!

public static class redBoxPanel extends JPanel {
    @Override public void paintComponent(Graphics g) {
        // You need to call the superclass' paintComponent() so that the 
        // setBackground() you called in main() is painted.
        super.paintComponent(g);

        // You need to set the colour BEFORE drawing your rect
        g.setColor(Color.RED);

        // Now that we have a colour, perform the drawing
        g.fillRect(0, 0, 100, 100);

        // More, for fun
        g.setColor(Color.GREEN);
        g.drawLine(0, 0, 100, 100);
    }
}

Can, can meaning: 1. to be able to: 2. used to say that you can and will do something: 3. to be allowed to: . Learn more. Can definition, to be able to; have the ability, power, or skill to: She can solve the problem easily, I'm sure. See more.

I think you you are missing super.paintComponent(g); in your painComponent method.

Can, 1.4Used to indicate that something is typically the case. 'antique clocks can seem out of place in modern homes'. More example sentences. Define can. can synonyms, can pronunciation, can translation, English dictionary definition of can. to be able to, have the power or skill to: I can take a bus to the

I Believe that the solution is working however you are, like you said in your question, setting the background in the JFrame class and the JPanel class. if you remove the setBackground from the JFrame class you should see only the rectangle that you are drawing. Please try the below solution and let us know if it works.

JFrame class:

import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.Color;


public class redBoxFrame{

    public static void main(String[]args){
        JFrame f = new JFrame();
        f.setSize(400, 200);
        f.setTitle("A red box");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel p = new redBoxPanel();
        f.add(p);
        f.setVisible(true);
  }

} 

JPanel class:

  import java.awt.Graphics;
  import javax.swing.JPanel;
  import java.awt.Color;

  public class redBoxPanel extends JPanel {

     public void paintComponent(Graphics g){
     super.paintComponent(g);
     g.fillRect(0, 0, 100, 100);
     g.setColor(Color.RED);

     }
  }

CAN, VerbEdit. can (third-person singular simple present can, present participle -, simple past could, past participle (obsolete except in adjectival use) couth). can definition: 1. to be able to: 2. used to say that you can and will do something: 3. to be allowed to: . Learn more.

Can, Can was the leading avant-garde rock group of the 70s. Can experimented with noise, synthesizers, non-traditional music, cut-and-paste techniques, and, most� 110 synonyms of can from the Merriam-Webster Thesaurus, plus 97 related words, definitions, and antonyms. Find another word for can. Can: to bring (as an action or operation) to an immediate end.

can, can 2. (kăn). n. 1. A usually cylindrical metal container. 2. Can(stylised as CAN) was a German experimental rockband formed in Colognein 1968 by the core quartet of Holger Czukay(bass, tape editing), Irmin Schmidt(keyboards), Michael Karoli(guitar), and Jaki Liebezeit(drums).

The Official CAN / Spoon Records Website, A can is a metal container in which something such as food, drink, or paint is put. The container is usually sealed to keep the contents fresh. Several young men� Find the latest Canaan Inc. (CAN) stock quote, history, news and other vital information to help you with your stock trading and investing.

Comments
  • Thank you! Also very nice of you that you added a few tricks like drawing a line across the box and adding comments describing what you do! I appreciate it very much : ) Have a nice day.
  • Hi, Thanks for the answer but it undforgunately does not seem to work, I tried updating the code to g.fillRect(0, 0, 100, 100); super.paintComponent(g); g.setColor(Color.RED); But now the whole frame turns red! You don't happen to have another idea? Thanks!
  • @First75 was on the right track i believe. You have to call super.paintComponent(g); but this has to be the first line before you do the fillRect and the setColor
  • Aha okay! well now everything but the JPanel is red... So the JFrame is red and the panel remains dark gray. You don't happen to know something I might do to get only the box red? Thanks!
  • This doesn't seem to do it either, unforgunately. It still renders the box as dark gray... You don't happen to have another trick I could try? also, If it worked for you maybe there is a problem with my IDE or something on my end? Thanks for the help! :)
  • Didn't test it to be honest. I will have a look later today if I get time