嗨,我是初学者,我正在做一个带有swing的java程序,选择一个形状,然后拖动它来制作并在屏幕上绘制一个形状(见下图)。
但我有一些问题:
2)的屏幕截图:
这是我的DrawPanel类:
class DrawingPanel extends JPanel {
public DrawingPanel() {
setBackground(Color.WHITE);
}
public void paint(Graphics g) {
super.paint(g);
if (x == 1) {
drawCircle(g);
}
else if (x == 2) {
drawOval(g);
}
else if (x == 3) {
drawRectangle(g);
}
else if (x == 4) {
drawSquare(g);
}
else if (x == 5) {
drawLine(g);
}
}
public void drawCircle(Graphics g) {
super.paint(g);
g.drawOval(xStrart, yStrart, width, width);
}
public void drawOval(Graphics g) {
super.paint(g);
g.drawOval(xStrart, yStrart, width, height);
}
public void drawRectangle(Graphics g) {
super.paint(g);
g.drawRect(xStrart, yStrart, width, height);
}
public void drawSquare(Graphics g) {
super.paint(g);
g.drawRect(xStrart, yStrart, width, width);
}
public void drawLine(Graphics g) {
super.paint(g);
g.drawLine(xStrart, yStrart, xEnd, yEnd);
}
}