0

I have a GUI class where I have eight rectangles in fixed positions and I want to fill them with certain colors (red/green) depending on the program the GUI is visualizing. In addition to the GUI class, I made an extended JPanel class for drawing the rectangles, and also tried to make an extended JLabel class for filling them for short periods of time, but the JLabel doesn't seem to work.

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

public class MyPanel extends JPanel {
    @Override
    public void paintComponent(Graphics g){
        System.out.println("-------IN MYPANEL CONSTRUCTOR-------");
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        g2.setColor(Color.black); 

        g2.drawRect(152, 150, 60, 15);
        g2.drawRect(152, 275, 60, 15);
        g2.drawRect(152, 400, 60, 15);
        
        g2.drawRect(421, 205, 60, 15);
        g2.drawRect(421, 355, 60, 15);

        g2.drawRect(720, 130, 60, 15);
        g2.drawRect(720, 280, 60, 15);
        g2.drawRect(720, 430, 60, 15);
    } 
}
import java.awt.*;
import javax.swing.*;

public class DLabel extends JLabel { 
    @Override
    public void paintComponent(Graphics g) {
        System.out.println("--------IN DLABEL CONSTRUCTOR--------");
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        g2.setColor(Color.green);
        g2.fillRect(152, 150, 60, 15);
    }
}
import javax.swing.*;
import java.awt.*;

public class GUI extends JFrame {
    JFrame frame;
    MyPanel mypanel;
    DLabel dlabel;

    public GUI() throws Exception {
        super();
        frame = new JFrame("Test");

        System.out.println("-------ATTEMPTING DLABEL INITIALIZATION----------");
        dlabel = new DLabel();
        dlabel.setBounds(152, 150, 60, 15);

        System.out.println("-------ATTEMPTING MYPANEL INITIALIZATION----------");
        mypanel = new MyPanel();

        mypanel.setLayout(null);
        mypanel.add(dlabel);
        mypanel.validate();
        mypanel.repaint();
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.getContentPane().add(mypanel);
        frame.setSize(1000, 500);
        frame.setVisible(true);
        frame.setResizable(false);
    }
 }
public class Main {
    public static void main(String[] args) {
        try {
            GUI gui = new GUI();
        } catch (Exception e) {
            System.err.println(e.toString());
        }
    }
}

The visualization I get is the eight hollow rectangles, but the JLabel does not fill them (or just the specific one I'm testing). It prints out that it's in the paintComponent() method of the DLabel() class when I run Main, but it doesn't seem to do anything, and I can't really find anything on overriding JLabel's paintComponent class.

I'm very new to GUIs and Swing so I could definitely not be following best practice, so any help is appreciated.

bendy
  • 11
  • 2
  • 2
    Use layouts. With all the `null` layouts, it's hard to tell if the GUI is thinking that any component (or its parent panel) is of 0 size. Put visible borders around each of the components of interest. The results may surprise you. – Andrew Thompson Dec 05 '21 at 14:33
  • Thanks guys - label.setOpaque() seems to solve it (at least until I break it again), and I'll take a look at some layouts, as well. Cheers! – bendy Dec 05 '21 at 14:38

0 Answers0