gpt4 book ai didi

java - 我是否正确使用了 Java swing 和图形?

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:53:31 26 4
gpt4 key购买 nike

<分区>

我刚刚学习 java,必须使用 java swing 库和 Graphics2D 类做一些事情。基本上我必须画一个建筑起重机,它有多个部分:一个 body (起重机的 body )和一些附加的 ARM (基本上它看起来像这样: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)
}
}

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com