javaswingpaintcomponentjcomponent

paintComponent not changing shape


can someone take a look at my code below and tell me why, when I change the following two statements, I do not see a change on the rectangle that is painted. So if I change:

g.setColor(Color.black); 
g.fillRect(l, w, 100, 100);

The program still prints a black rectangle with the same dimensions and in the same position that I first started with even though I change color to yellow or try to change the dimensions or location. I am BlueJ. The following is my full code:

import java.awt.*;
import javax.swing.*;

public class SwingPaintDemo2 extends JComponent {

public static boolean isWall = true;

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            createAndShowGUI(); 
        }
    });
}


private static void createAndShowGUI() {
    //System.out.println("Created GUI on EDT? "+
    //SwingUtilities.isEventDispatchThread());
    JFrame f = new JFrame("Swing Paint Demo");
    JPanel MyPanel = new JPanel();
     MyPanel.setBorder(BorderFactory.createEmptyBorder(1000, 1000, 1000, 1000));
     MyPanel.setPreferredSize(new Dimension(250, 200));
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    f.add(new MyPanel());
    f.pack();
    f.setVisible(true);

}


public void paintComponent(Graphics g) {
    super.paintComponent(g); 
    int l = 10;
    int w = 10;

  g.setColor(Color.black); 
  g.fillRect(l, w, 100, 100);

        }

}

Any advice would be appreciated.


Solution

  • Your SSCCE doesnt compile where is MyPanel class or did you mean new SwingPaintDemo2()?

    On the assumption you meant new SwingPaintDemo2():

    The code does work just fine but the JFrame is sized very small:

    enter image description here

    because you dont give it any size and none of its components have a size as they do not have any components added to them, thus we must make the JComponent return a correct size so when we call pack() our JFrame is sized correctly

    Solution

    override getPreferredSize() of JComponent to return a width and height which fits all drawings.

    Some suggestions though:

    Here is an example (your code with above fixes implemented):

    enter image description here

    import java.awt.*;
    import javax.swing.*;
    
    public class SwingPaintDemo2 extends JPanel {
    
        public static boolean isWall = true;
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI();
                }
            });
        }
    
        private static void createAndShowGUI() {
            //System.out.println("Created GUI on EDT? "+
            //SwingUtilities.isEventDispatchThread());
            JFrame f = new JFrame("Swing Paint Demo");
            JPanel MyPanel = new JPanel();
            MyPanel.setBorder(BorderFactory.createEmptyBorder(1000, 1000, 1000, 1000));
            MyPanel.setPreferredSize(new Dimension(250, 200));
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(new SwingPaintDemo2());
            f.pack();
            f.setVisible(true);
    
        }
    
        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            int l = 10;
            int w = 10;
    
            g.setColor(Color.black);
            g.fillRect(l, w, 100, 100);
    
        }
    
        @Override
        public Dimension getPreferredSize() {
            return new Dimension(150, 150);
        }
    }