在Java中涂鸦(鼠标监听器)

huangapple go评论74阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2020年10月18日 03:11:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/64406335.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定