Jpanel does not appear in Jdialog

Asked

Viewed 69 times

0

I’m solving an activity with the following statement: Create an application to display eight Jpanel components in a Jdialog window. Each panel should be colored with one of the eight colors of Table 1.

Figura 1

In each panel should be written the word that translates the meaning of the color. Use font size 18. Each panel should be colored using a color from Table 1, specifying the amount of each RGB component (Red, Green, Blue) that corresponds to the meaning of the color. Use the java.awt.Color class. Should be implementing only a single paintComponent method to paint the 8 panels and write down the meaning of each color.

The problem is that my Jpanel does not appear in Jdialog. And I have no idea how to get him to show up.

Follow the code:

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridLayout;

import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class SigCoresGUI extends JDialog {

    private static final long serialVersionUID = 1L;
    private Color[] cores = { new Color(255, 255, 255), new Color(249, 206, 137), new Color(255, 128, 0),
            new Color(255, 0, 0), new Color(244, 102, 174), new Color(5, 120, 203), new Color(116, 186, 160),
            new Color(0, 0, 0) };
    private String[] sig = { "Paz", "Energia", "Criatividade", "Paixão", "Ternura", "Tranquilidade", "Harmonia",
            "Elegância" };
    private Font font = new Font("Arial", Font.BOLD, 18);

    public SigCoresGUI() {
        super();
        Desenha desenha = new Desenha();
        add(desenha);
        setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        setLocationByPlatform(true);
        setLayout(new GridLayout(8, 8));
        setSize(400, 500);
        setVisible(true);
    }

    public class Desenha extends JPanel {
        private static final long serialVersionUID = 1L;

        @Override
        public void paintComponents(Graphics g) {
            for (int i = 0; i < 8; i++) {
                JPanel panel = new JPanel();
                panel.setBackground(cores[i]);
                panel.setFont(font);
                JLabel label = new JLabel(sig[i]);
                label.setFont(font);
                if (i > 0)
                    label.setForeground(Color.WHITE);
                panel.add(label);
                add(panel);
            }
        }
    }
}

Main:

public class SigTest {

    public static void main(String[] args) {
        new SigCoresGUI();
    }
}

1 answer

1


One of the problems is that you are trying to create the colored panels during the panel drawing process Desenha, so nothing is displayed. The method paintComponents serves to draw that component on your screen, and to add other components to it, you need the most external already to be completely drawn. From what I understand of the problem, the use of paintComponents is a requirement, which makes you have to take a different approach from the one you are using.

You can color a component still in your construction using the class itself Graphics received as parameter. Graphics.setColor sets the color to be used in that context, combined with the method Graphics.fillRect it is possible to force the complete fill of the component.

It is also possible to draw strings in this method, defining its color with the method already mentioned, defining the font with the Graphics2D.setFont and drawing by itself the string with Graphics.drawString. It is necessary to convert Graphics for Graphics2D because the method responsible for painting the string on the canvas is in this last class, which is an "improved version" of the first.

That said, the code goes like this:

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;

import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class SigCoresGUI extends JDialog {

    private static final long serialVersionUID = 1L;
    private Color[] cores = { new Color(255, 255, 255), new Color(249, 206, 137), new Color(255, 128, 0),
            new Color(255, 0, 0), new Color(244, 102, 174), new Color(5, 120, 203), new Color(116, 186, 160),
            new Color(0, 0, 0) };
    private String[] sig = { "Paz", "Energia", "Criatividade", "Paixão", "Ternura", "Tranquilidade", "Harmonia",
            "Elegância" };
    private Font font = new Font("Arial", Font.BOLD, 18);

    public SigCoresGUI() {
        super();

        setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        setLayout(new GridLayout(4, 4));

        for (int i = 0; i < 8; i++) {
            add(desenhaPainel(cores[i], sig[i]));
        }

        setSize(400, 500);
        setVisible(true);
    }

    private JPanel desenhaPainel(Color cor, String significado) {
        return new JPanel() {

            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                Graphics2D g2 = (Graphics2D) g;
                g2.setColor(cor);
                g2.fillRect(0, 0, getWidth(), getHeight());
                g2.setColor(cor.getRGB() == Color.black.getRGB() ? Color.white : Color.black);
                g2.setFont(font);
                g2.drawString(significado, 5, 15);
            }
        };
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            new SigCoresGUI();
        });
    }
}

the result:

inserir a descrição da imagem aqui

Note that I set the color 2 times, the first to fill the component, and the second to draw the string. I made a small workaround to be able to invert the font color in the last square, because being the black background and the black font, it obviously goes away.

  • Thanks, thanks to your reply I managed to tidy up here.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.