8

我只是在学习 java,必须使用 java swing 库和 Graphics2D 类来做一些事情。基本上我必须画一个有多个部分的建筑起重机:一个身体(起重机的身体)和一些附加的手臂(基本上它看起来像这样:http: //i.imgur.com/4YIkYqW.jpg) .

我的问题围绕着我是否正确使用了 Java swing 类?在下面的代码中,我省略了不必要的代码,因为我只是想确保我的结构正确(正确使用 JPanel、paintComponent() 等)。任何帮助将不胜感激,因为我正在学习 Java!多谢你们。

public class CraneSimulator {

    ...
    public JFrame frame;
    public MyPanel panel;

    public CraneSimulator() {
        frame = new JFrame("CraneSimulator");

        ...

        panel = new MyPanel();
        frame.add(panel);

    }

    public static void main(String[] args) {
        CraneSimulator simulator = new CraneSimulator();    
    }
}   

class MyPanel extends JPanel {
    CraneBody body;
    CraneArm arm1;
        ...
    Graphics2D graphics;

    public MyPanel() {
        body = new CraneBody();
        arm1 = new CraneArm(body);
        ...
        addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                ...         }
            }
            public void mouseReleased(MouseEvent e) {
                    ...
            }
        });

        addMouseMotionListener(new MouseAdapter() {
            public void mouseDragged(MouseEvent e) {
                ...             
            }
        });
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        graphics = (Graphics2D) g;
        ...
        body.paint(g);      
        arm1.paint(g);  
    }
}

class CraneBody {
    ...

    public CraneBody() {
         ....
    }
    ...
    public void paint(Graphics g) {
        Graphics2D g2 = (Graphics2D) g;

            // Use g2 to actual paint crane Body on screen here (ie. g2.drawRect, etc)      
    }
}

class CraneArm {
    ...

    public CraneArm() {
         ....
    }
    ...
    public void paint(Graphics g) {
        Graphics2D g2 = (Graphics2D) g;

            // Use g2 to actual paint the crane armon screen here (ie. g2.drawRect, etc)        
    }
}
4

2 回答 2

3

您的代码结构良好,并遵循使用 Java 图形和 OOP 的优秀实践。

正如评论中所建议的,如果您没有理由将其设为实例变量,则最好在本地定义您的 Graphics 对象。

于 2013-02-08T06:14:30.647 回答
1

您的代码几乎是完美的。但有几个建议:

  1. 声明初始化有一些优点
  2. 你只需要一个MouseAdapter;
  3. 使用 Graphics2D 作为参数,这样您就不需要再次从 Graphics 转换它。
  4. 删除字段“图形”,改为将其设为局部变量。(感谢@GuillaumePolet)。

有些人可能不同意,但根据您的代码,我会进行这些更改以使其更整洁。

public class CraneSimulator {
    ...
    private JFrame frame = new JFrame("CraneSimulator");
    private MyPanel panel = new JPanel();

    public CraneSimulator() {
        ...
        frame.add(panel);
    }
    public static void main(String[] args) {
        CraneSimulator simulator = new CraneSimulator();    
    }
}   

class MyPanel extends JPanel {
    CraneBody body = new CraneBody();
    CraneArm arm1 = new CraneArm(body);
    ...
    MouseAdapter mAdapter = new MouseAdapter() {
        public void mousePressed(MouseEvent e) {
            ...
        }
        public void mouseReleased(MouseEvent e) {
            ...
        }
        public void mouseDragged(MouseEvent e) {
            ...             
        }
    }

    public MyPanel() {
        ...
        addMouseListener(mAdapter);
        addMouseMotionListener(mAdapter);
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D graphics = (Graphics2D) g;
        ...
        body.paint(graphics);
        arm1.paint(graphics);  
    }
}

class CraneBody {
    ...
    public CraneBody() {
         ....
    }
    ...
    public void paint(Graphics2D g) {
        // You don't need to cast a Graphics again.      
    }
}

class CraneArm {
    ...
    public CraneArm() {
         ....
    }
    ...
    public void paint(Graphics2D g) {
        // You don't need to cast a Graphics again.
    }
}
于 2013-02-08T09:33:42.463 回答