How can I use 2 Class paint in JFrames generate code by Palette in Netbeans?

Viewed 42
public class DrawST extends javax.swing.JFrame {

    /**
     * Creates new form DrawST
     */
    
    
    public DrawST() {
        initComponents();
    }
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    Generated code
     /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new DrawST().setVisible(true);
            }
        });
    }
    class Class01 extends JPanel{
       @Override
       public void paintComponent(Graphics g) {
           super.paintComponent(g);
       }
    }
    class Class02 extends JPanel{
        @Override
        public void paintComponent(Graphics g){
            super.paintComponent(g);
        }
    }
    // Variables declaration - do not modify                     
    // End of variables declaration                   
}

How can I use both Class01 and Class02 to paint in this JFrame, Class01 will draw pipes, and Class02 will draw frog(Happy Frog). I try to create Container contain = getContentPane(); and add two classes. But it work only 1 class added.If I add two classes, the JFrame won't draw anything.

1 Answers

You're making a common mistake: creating drawing classes that extend Swing components when they should be logical classes, not component classes:

  • If you're doing animation, usually only one component, often a JPanel does the drawing, meaning the paintComponent method is overridden in one component.
  • If you desire a class to draw a specific sprite, such as a frog, or pipes, then these sprites will be drawn in that same single drawing class described above.
  • Component classes, classes that extend Swing GUI components such as JPanels are usually placed into the GUI as components -- building blocks of the GUI that are placed in a limited location in the GUI, and that paint themselves and their children only.
  • So to do drawings where multiple sprites occupy the same drawing space, the classes that create these sprites should not extend JPanel or similar Swing components and instead should only be logical classes (classes that are not components), and then that are drawn by that single drawing JPanel.

For example

public class DrawST extends javax.swing.JFrame {

    
    public DrawST() {
        // add the DrawingPanel to the JFrame here
    }
    
}
// this class extends JPanel and does *all* the drawing
public class DrawingPanel extends JPanel {
    private Frog frog = new Frog(0, 0);
    private List<Pipe> pipes = new ArrayList<>();
    private Timer timer; // you'll probably need a Swing Timer to drive the animation
    
    // a single paintComponent method is present
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);  // Don't forget this
        frog.draw(g);  // have this component draw a frog
        for (Pipe pipe : pipes) {
            pipe.draw(g);  // draw all the pipes
        }
    }

}
// does not extend JPanel
public class Frog {
    private int x;
    private int y;
    
    public Frog(int x, int y) {
        this.x = x;
        this.y = y;
    }
    
    public void draw(Graphics g) {
        // use x, y, and g to draw a frog at x,y location 
    }
}
// does not extend JPanel (not sure what a pipe is, TBH)
public class Pipe {
    private int x;
    private int y;
    // other? length, color, width?
    
    public void draw(Graphics g) {
        // use x, y, and g to draw a pipe. 
        // maybe also needs a length, color, width?
    }
}
Related