如何在jframes中使用2类绘制在netbeans中通过调色板生成代码?

iq3niunx  于 2021-07-03  发布在  Java
关注(0)|答案(1)|浏览(290)
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                   
}

我怎么能两者都用呢 Class01 以及 Class02 在这个框架里画画, Class01 将绘制管道 Class02 会画青蛙(快乐的青蛙)。我试着创造 Container contain = getContentPane(); 再加上两个班。但是它只需要添加一个类就可以了。如果我添加两个类,jframe就什么也画不出来了。

lndjwyie

lndjwyie1#

您犯了一个常见的错误:创建扩展swing组件的图形类时,这些组件应该是逻辑类,而不是组件类:
如果你在做动画,通常只有一个组件,通常一个jpanel做绘图,意思是 paintComponent 方法在一个组件中被重写。
如果您希望一个类来绘制一个特定的精灵,例如青蛙或管道,那么这些精灵将在上面描述的同一个绘制类中绘制。
组件类,扩展swing gui组件(如jpanels)的类通常作为组件放置在gui中——gui的构建块,放置在gui中的有限位置,并且只绘制它们自己和它们的子组件。
因此,要绘制多个精灵占用相同绘图空间的图形,创建这些精灵的类不应扩展jpanel或类似的swing组件,而应仅为逻辑类(不是组件的类),然后由该单个图形jpanel绘制。
例如

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?
    }
}

相关问题