英文:
Doodling in java (mouse listener)
问题
以下是您提供的代码的翻译部分:
import java.awt.*;
import java.applet.*;
public class Design3 extends Applet implements MouseListener, MouseMotionListener {
int mx, my;
public void mouseClicked(MouseEvent e) {
mx = e.getX();
my = e.getY();
paint(mx, my, g);
}
public void mouseExited(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
}
public void mouseMoved(MouseEvent e) {
}
public void mouseDragged(MouseEvent e) {
}
public void paint(int x, int y, Graphics g) {
for (int i = 40; i >= 1; i -= 10) {
if (i % 3 == 1) {
g.setColor(Color.red);
} else if (i % 3 == 2) {
g.setColor(Color.blue);
} else {
g.setColor(Color.green);
}
g.fillOval((int)(x + 20 - i / 2), (int)(y + 20 - i / 2), i, i);
}
}
}
请注意,我已经将代码中的错误进行了修正。如果您有任何其他问题,请随时提问。
英文:
I am trying to recreate the doodling thing I made in python (it was an earlier question I asked) in java. However, it keeps giving me an error:
Design3.java:28: error: <identifier> expected
public void paint(x, y, Graphics g) {
^
Design3.java:28: error: <identifier> expected
public void paint(x, y, Graphics g) {
^
2 errors
Code:
import java.awt.*;
import java.applet.*;
public class Design3 extends Applet implements MouseListener, MouseMotionListener {
int mx, my;
public void mouseClicked(MouseEvent e) {
mx = e.getX();
my = e.getY();
paint(mx, my, g);
}
public void mouseExited(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
}
public void mouseMoved(MouseEvent e) {
}
public void mouseDragged(MouseEvent e) {
}
public void paint(x, y, Graphics g) {
for (int i = 40; i >= 1; i -= 10) {
if (i % 3 == 1) {
g.setColor(Color.red);
} else if (i % 3 == 2) {
g.setColor(Color.blue);
} else {
g.setColor(Color.green);
}
g.fillOval((int)(x + 20 - i / 2), (int)(y + 20 - i / 2), i, i);
}
}
}
My question is:
How do I fix the error and make it work?
答案1
得分: 2
public void paint(int x, int y, Graphics g) {
}
你忘记指定x和y是什么。
英文:
public void paint(int x, int y, Graphics g) {
}
You forgot to specify what x and y were.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论