当我按住鼠标时,如何将程序绘制到何处?当我不按住鼠标时,如何将其擦除

时间:2019-05-31 01:59:55

标签: java

我需要知道如何创建一个值,该值在我按住鼠标时为true,而在未单击鼠标按钮时为false。如果为true,则运行paint(g);如果为false,则运行delete(g)

这对我来说是新的,我仍然可以在代码中尝试尝试并帮助自己进行解释。

    import java.awt.*;
    import java.applet.*;


    public class FinalProject extends Applet {
     int xCoord, yCoord;


    boolean ready,painting =false;
     public void init(Graphics g) {
      xCoord = yCoord = 0;
       Expo.setBackground(g,Expo.white);
     }

     public void white(Graphics g) {

       Expo.setBackground(g,Expo.white);
     }

     public void paint(Graphics g) {
       if (ready == false)
       {
         white(g);
       }
       Expo.setColor(g,Expo.black);
       Expo.fillCircle(g,xCoord,yCoord,4);

     }

      public void erase(Graphics g) {
       Expo.setColor(g,Expo.gray);
       Expo.fillCircle(g,xCoord,yCoord,4);
      }

     public boolean mouseDrag(Event e, int x, int y) {
      xCoord = x;
      yCoord = y;
      repaint();
      ready = true;
      painting = true;
      return true;
     }

     public boolean mouseMoved(Event e, int x, int y) {
      xCoord = x;
      yCoord = y;

      repaint();
      painting = false;
      return true;

     }


     public void update(Graphics g)   
     {
       if (painting == true) {
        erase(g);
       }
       else {
         paint(g);
       }

     }
    }

当我单击鼠标并取消单击鼠标按钮时,没有任何反应。它应在未单击时擦除并在单击时绘制。

0 个答案:

没有答案