paintComponent does not work on the East Panel, on West is ok

54 views Asked by At

i have no idea why paintComponent gets called for the WestPanel, but wont work for the East Panel... even if the code is similar. Pls some help.

     public class MyFrame extends JFrame{

        public static final int WIDTH = 1664;
        public static final int HEIGHT= 1088;
        public static final int TILE_W = 64;
        public static final int TILE_H = 64;
        private CenterPanel cP;
        private WestPanel wP;
        private EastPanel eP;
        /*private NorthPanel nP;
        private SouthPanel sP;*/



        public MyFrame(){

            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setSize(WIDTH, HEIGHT);
            setTitle("Chuta");
            setResizable(false);
            setLocationRelativeTo(null);




            cP = new CenterPanel();
            add(cP, BorderLayout.CENTER);
            wP = new WestPanel();
            add(wP, BorderLayout.WEST);
            eP = new EastPanel();
            add(eP, BorderLayout.EAST);


            setVisible(true);
        }

        public static void main(String[] args) {

            MyFrame f = new MyFrame();
        }

This west panel is drawing .
package frame;

    import java.awt.BasicStroke;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import javax.swing.JPanel;
    import static frame.MyFrame.*;

    public class WestPanel extends JPanel {



        private int wCP;


        public WestPanel(){

            wCP=3*TILE_W;



            Dimension size = getPreferredSize();
            size.width = wCP;
            setPreferredSize(size);
            setBackground(new Color(0, 115, 102));

            new CardGrid(TILE_W/2);

        }

        @Override
        public void paintComponent(Graphics g){
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g;
            g2d.setStroke(new BasicStroke(5));
            g2d.setColor(Color.white);
            for (int y = TILE_H ; y < TILE_H*15; y+=TILE_H*3) {
                g2d.drawRect(TILE_W / 2, y, 120, TILE_H * 3);
            }


        }

This East panel is not drawing

    import static frame.MyFrame.TILE_W;
    import static frame.MyFrame.TILE_H;
    import java.awt.BasicStroke;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import javax.swing.JPanel;

    public class EastPanel extends JPanel {

    private int eCP;


    public EastPanel(){

        eCP=3*TILE_W;
        Dimension size = getPreferredSize();
        size.width = eCP;
        setPreferredSize(size);
        setBackground(new Color(0, 112, 102));

        new CardGrid(MyFrame.WIDTH-120-TILE_W/2);


    }


    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        Graphics2D g2d = (Graphics2D) g;
        g2d.setStroke(new BasicStroke(5));
        g2d.setColor(Color.white);
        for (int y = TILE_H ; y < TILE_H*15; y+=TILE_H*3) {
            g2d.drawRect(MyFrame.WIDTH- 120 -TILE_W / 2, y, 120, TILE_H * 3);
        }
      }


   }
1

There are 1 answers

0
MadProgrammer On

The Graphics context is automatically configured so that position 0x0 represents the top/left corner of the component

This means when you using something like MyFrame.WIDTH - 120, you will be painting beyond the visible bounds of the component.

Remember, a component has its own area of context, 0x0xwidthxheight. You shouldn't be using MyFrame.WIDTH in you components