简体   繁体   中英

Drawing from one panel class to another panel class.

I have two classes. The first class is called Fishy1, and the second class is called Fishy2. This is the code for my first class:

import java.awt.Graphics;
import javax.swing.JPanel;

public class Fishy1 extends JPanel {

Fishy1 fishy1 = new Fishy1();

/* Graphics goes here */
public void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.drawOval(50, 50, 50, 50);

}

}

As you can see, the code basically draws an oval in fishy1. And here is the code for my second class:

import java.awt.Graphics;
import javax.swing.JPanel;

public class Fishy2 extends JPanel {

Fishy2 fishy2 = new Fishy2();

}

As you can see, in the second class, there is no paintComponet method to draw to fishy2. So, my question is, is there a way to draw to the second class using the paintComponent method in the first class? If there's no way to do it, please let me know. Thank you.

To achieve graphics replication between 2 swing classes at same time

public class Fishs extends JPanel {

    //Static list, all fishes panel will display the same objects at same positions
    private static List<OvalObj> lstOvalObjects;

    public Fishs() {
        //if the list is null just initialize it.
       lstOvalObjects = lstOvalObjects == null? new ArrayList():lstOvalObjects;
    }

    /* Graphics goes here */
    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        lstOvalObjects.forEach(ovalObject ->  g.drawOval(ovalObject.getX(), ovalObject.getY(), ovalObject.getWidth(), ovalObject.getHeight()));
    }

    public static List<OvalObj> getLstOvalObjects() {
        return lstOvalObjects;
    }

    public static void setLstOvalObjects(List<OvalObj> lstOvalObjects) {
        Fishs.lstOvalObjects = lstOvalObjects;
    }

}

The oval object:

public class OvalObj{
        private int x,y,width,height;
        public OvalObj(int x, int y, int width, int height) {
            this.x = x;
            this.y = y;
            this.width = width;
            this.height = height;
        }

        public int getX() {
            return x;
        }

        public void setX(int x) {
            this.x = x;
        }

        public int getY() {
            return y;
        }

        public void setY(int y) {
            this.y = y;
        }

        public int getWidth() {
            return width;
        }

        public void setWidth(int width) {
            this.width = width;
        }

        public int getHeight() {
            return height;
        }

        public void setHeight(int height) {
            this.height = height;
        }

    }

Implementation:

        //First Frame
        JFrame frame1 = new JFrame();
        frame1.setLayout(new BorderLayout());

        //First Fish Panel that will go to frame1
        Fishs fish1 = new Fishs();
        fish1.setVisible(true);

        frame1.add(fish1, BorderLayout.CENTER);
        frame1.pack();
        frame1.setVisible(true);


        //Second Frame
        JFrame frame2 = new JFrame();
        //Second Fish Panel that will go to frame2
        Fishs fish2 = new Fishs();
        fish2.setVisible(true);
        frame2.setLayout(new BorderLayout());
        frame2.add(fish2, BorderLayout.CENTER);
        frame2.pack();
        frame2.setVisible(true);

        /// you can add many objects to draw as you like in a static way anywhere in your code, they will render in every fish panel at same time
        Fishs.getLstOvalObjects().add(new OvalObj(0, 0, 50, 50));
        Fishs.getLstOvalObjects().add(new OvalObj(20, 20, 50, 50));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM