Home > Software design >  Component visible in frame but not in panel
Component visible in frame but not in panel

Time:07-18

I was doing some JFrame work and I got into this problem. Basically I have a component, if I add it to my frame it's visible but if I add it to a panel it doesn't show up, anybody knows why?

This is my component (basically a geometric shape):

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


public class RosaDeiVentiComponent extends JComponent {

    private static int rdvRadius = 300;

    private static int centerX = GraphicInterfaceManager.getBorderX()   rdvRadius;
    private static int centerY = GraphicInterfaceManager.getBorderY()   rdvRadius;


    
    private int xN = centerX; private int yN = centerY - rdvRadius;
    private int xS = centerX; private int yS = centerY   rdvRadius;
    private int xE = centerX   rdvRadius; private int yE = centerY;
    private int xO = centerX - rdvRadius; private int yO = centerY;

    private int xNO = centerX - 150; private int yNO = centerY - 150;
    private int xNE = centerX   150; private int yNE = centerY - 150;
    private int xSO = centerX - 150; private int ySO = centerY   150;
    private int xSE = centerX   150; private int ySE = centerY   150;
    
    private int xNtO = centerX - 55; private int yNtO = centerY - 55;
    private int xNtE = centerX   55; private int yNtE = centerY - 55;
    private int xStO = centerX - 55; private int yStO = centerY   55;
    private int xStE = centerX   55; private int yStE = centerY   55;
    private int xNOtN = centerX; private int yNOtN = centerY - 55;
    private int xNEtE = centerX   55; private int yNEtE = centerY;
    private int xSEtS = centerX; private int ySEtS = centerY   55;
    private int xSOtO = centerX - 55; private int ySOtO = centerY;

    public RosaDeiVentiComponent(){}


    public void paintComponent(Graphics g){
        Graphics2D g2 = (Graphics2D) g;

        g2.setColor(Color.decode("#9999ff"));
        int[] x1 = {centerX, xN, xNtO};
        int[] y1 = {centerY, yN, yNtO};
        g2.fillPolygon(x1, y1, 3);

        g2.setColor(Color.decode("#e5e5e5"));
        int[] x2 = {centerX, xN, xNtE};
        int[] y2 = {centerY, yN, yNtE};
        g2.fillPolygon(x2, y2, 3);

        g2.setColor(Color.decode("#9999ff"));
        int[] x3 = {centerX, xE, xNtE};
        int[] y3 = {centerY, yE, yNtE};
        g2.fillPolygon(x3, y3, 3);

        g2.setColor(Color.decode("#e5e5e5"));
        int[] x4 = {centerX, xE, xStE};
        int[] y4 = {centerY, yE, yStE};
        g2.fillPolygon(x4, y4, 3);

        g2.setColor(Color.decode("#9999ff"));
        int[] x5 = {centerX, xS, xStE};
        int[] y5 = {centerY, yS, yStE};
        g2.fillPolygon(x5, y5, 3);

        g2.setColor(Color.decode("#e5e5e5"));
        int[] x6 = {centerX, xS, xStO};
        int[] y6 = {centerY, yS, yStO};
        g2.fillPolygon(x6, y6, 3);

        g2.setColor(Color.decode("#9999ff"));
        int[] x7 = {centerX, xO, xStO};
        int[] y7 = {centerY, yO, yStO};
        g2.fillPolygon(x7, y7, 3);

        g2.setColor(Color.decode("#e5e5e5"));
        int[] x8 = {centerX, xO, xNtO};
        int[] y8 = {centerY, yO, yNtO};
        g2.fillPolygon(x8, y8, 3);

        g2.setColor(Color.decode("#9999ff"));
        int[] x9 = {centerX, xNOtN, xNO};
        int[] y9 = {centerY, yNOtN, yNO};
        g2.fillPolygon(x9, y9, 3);

        g2.setColor(Color.decode("#e5e5e5"));
        int[] x10 = {centerX, xNOtN, xNE};
        int[] y10 = {centerY, yNOtN, yNE};
        g2.fillPolygon(x10, y10, 3);

        g2.setColor(Color.decode("#9999ff"));
        int[] x11 = {centerX, xNEtE, xNE};
        int[] y11 = {centerY, xNEtE, yNE};
        g2.fillPolygon(x11, y11, 3);

        g2.setColor(Color.decode("#e5e5e5"));
        int[] x12 = {centerX, xNEtE, xSE};
        int[] y12 = {centerY, yNEtE, ySE};
        g2.fillPolygon(x12, y12, 3);

        g2.setColor(Color.decode("#9999ff"));
        int[] x13 = {centerX, xSEtS, xSE};
        int[] y13 = {centerY, ySEtS, ySE};
        g2.fillPolygon(x13, y13, 3);

        g2.setColor(Color.decode("#e5e5e5"));
        int[] x14 = {centerX, xSEtS, xSO};
        int[] y14 = {centerY, ySEtS, ySO};
        g2.fillPolygon(x14, y14, 3);

        g2.setColor(Color.decode("#9999ff"));
        int[] x15 = {centerX, xSOtO, xSO};
        int[] y15 = {centerY, ySOtO, ySO};
        g2.fillPolygon(x15, y15, 3);

        g2.setColor(Color.decode("#e5e5e5"));
        int[] x16 = {centerX, xSOtO, xNO};
        int[] y16 = {centerY, ySOtO, yNO};
        g2.fillPolygon(x16, y16, 3);

        g2.setColor(Color.BLACK);
        g2.setFont(new Font("Serif", Font.ITALIC, 36));
        g2.drawChars("N".toCharArray(),0,1,338,100);
        g2.drawChars("S".toCharArray(),0,1,341,727);
        g2.drawChars("O".toCharArray(),0,1,21, 411);
        g2.drawChars("E".toCharArray(),0,1,655,411);
    }

Adding it to my frame works:

    public static void frameCreator() {

        frame.add(new RosaDeiVentiComponent());
        frame.setSize(width, height);
        frame.setTitle("APP");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

If I set my layout as null it doesn't show up. Neither if I add it to a panel, like here.

    public static void frameCreator() {

        frame.setSize(width, height);
        frame.setTitle("APP");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        rdvPanel.add(new RosaDeiVentiComponent());
        rdvPanel.setBackground(Color.WHITE);
        //Here I basically set the bound based on the shape dimension
        rdvPanel.setBounds(new Rectangle(0,0, borderX   RosaDeiVentiComponent.getRdvRadius()*2   200, height));
        rdvPanel.setVisible(true);
        frame.add(rdvPanel);
}

After the frameCreator method I set the frame as visible.

Can anybody tell me where I'm making a mistake? And possibly explaining way the absolute layout doesn't work even if I use absolute coordinate? Thx.

CodePudding user response:

You have to set prefferedSize to your component.

It would be also good, if you'd call super as a first line in your paintComponent:

public void paintComponent(Graphics g){
   super.paintComponent(g);
      //your code
}

By the way, you may initialize all your component's variables in contructor, as most probably you won't use them anywhere else:

public RosaDeiVentiComponent(){
   int rdvRadius = 300;
   int centerX = 500;
   int centerY = 500;
   //...
}

You may also consider shorten up creation of your Graphics.

Wind rose is made of only 1 triangle (or you can consider them as pair of triangles) only oriented in different directions (and different size or colour, if you want).

You can create only 1 or 2 triangles and add them in a loop, with different orientations. Creating each triangle separately looks redundant

  • Related