简体   繁体   English

使用户制作的矩形出现在屏幕上

[英]Making a rectangle the user makes appear on the screen

The user clicks in one spot and releases in another. 用户单击一个位置,然后释放另一个位置。 I use the where the click started and where it ends to make two coordinates. 我使用点击开始和结束的位置来创建两个坐标。 This is how I get the width and the height of the rectangle i want to make so that all parameters of the drawRect() method are met. 这就是我要获取的矩形的宽度和高度的方式,以便满足drawRect()方法的所有参数。 I want to make the rectangle they made to appear on the screen. 我想将它们制作的矩形显示在屏幕上。 It shows the width and height but doesn't create the rectangle. 它显示宽度和高度,但不创建矩形。

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

    import javax.swing.Timer;

    @SuppressWarnings("serial")
    public class PracticeClass extends Applet implements MouseListener, ActionListener, KeyListener
    {
       int x, y, velX, velY, Width, Height, x2, y2;
       String tracer, tracer2, tracer3;
       Timer tm = new Timer(4, this);

       public PracticeClass()
       { 
          tm.start();
          setFocusable(true);
          setSize(900, 900);
          setFocusTraversalKeysEnabled(false);
          addKeyListener(this);
          addMouseListener(this);
       } 
       public void mousePressed(MouseEvent e){} 
       public void mouseClicked(MouseEvent e)
       {
           x = e.getX(); 
           y = e.getY(); 
           tracer = " x = " + x + " y = " + y;
           // repaint the applet// 
           repaint(); 
       } 
       public void mouseEntered(MouseEvent e){} 
       public void mouseExited(MouseEvent e){} 
       public void mouseReleased(MouseEvent e)
       {
           int x2 = e.getX();
           int y2 = e.getY();
           int Width = x2 - x;
           int Height = y2 - y;
           tracer2 = "Width = " + Width + " Height = " + Height;
           tracer3 = "X2 = " + x2 + " Y2 = " + y2;
           repaint();
       } 
       // paint method 
       public void paint(Graphics g){ 
            g.drawString(tracer, 0, 20);
            g.drawString(tracer2,  0, 40);
            g.drawString(tracer3,  0,  60);
            g.drawRect(x,y, Width, Height); 
            g.setColor(Color.BLUE);
       } 

       public void actionPerformed(ActionEvent e)
       {
           if (x < 0)
           {
               velX = 0;
               x = 0;
           }
           if (x > 470)
           {
               velX = 0;
               x = 470;
           }
           if (y < 0)
           {
               velY = 0;
               y = 0;
           }
           if (y > 470)
           {
               velY = 0;
               y = 470;
           }
           x = x + velX;
           y = y + velY;
           repaint();
       }

       public void keyPressed(KeyEvent e)
        {
            int c = e.getKeyCode();

            if (c == KeyEvent.VK_LEFT)
            {
                velX = -1;
            }
            if (c == KeyEvent.VK_RIGHT)
            {
                velX = 1;
            }
            if (c == KeyEvent.VK_UP)
            {
                velY = -1;
            }
            if (c == KeyEvent.VK_DOWN)
            {
                velY = 1;
            }
            repaint();
        } 

       public void keyTyped(KeyEvent e){}
       public void keyReleased(KeyEvent e)
       {
          int c = e.getKeyCode();

          if ((c == KeyEvent.VK_LEFT) || (c == KeyEvent.VK_RIGHT))
          {
              velX = 0;
          }

          if ((c == KeyEvent.VK_UP) || (c == KeyEvent.VK_DOWN))
          {
              velY = 0;
          }
       }
  } 
g.drawRect(x, y, Width, Height)

put it right before repaint in the mouseReleased function 在重新绘制之前将其放在mouseReleased函数中

you already did all the hard work, lol 你已经做了所有的努力,大声笑

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM