如何使用drawLine()使线条随机方向运动?

4
我正在进行一项有趣的任务,其中包括一个小船,该船通过mouseMoved()移动并随机射出激光。我想使用drawLine(mouse_x, mouse_y, ?, ?)绘制激光,但是我无法定义x2和y2的坐标。激光必须横跨屏幕。
这是我目前的情况。 page.drawLine(mouse_x-15, mouse_y-5,300,300); 当然,我不想让激光一直射向角落(300,300)。
import java.applet.*;
import java.awt.*;
import java.awt.event.*;


public class SpaceShip extends Applet
   implements MouseListener, MouseMotionListener {

   private int applet_width = 300; //width of applet
   private int applet_height =300; //height of applet
   private int mouse_x, mouse_y;  // the mouse coordinates
   private int shots = 0; //count of shots
   private boolean buttonPressed = false;


   //init()
   public void init() {
      setSize(applet_width, applet_height); //set size of applet
      setBackground( Color.black ); //set color of background

      mouse_x = applet_width/2; //initiate mouse in the middle of the applet
      mouse_y = applet_height/2;

      addMouseListener( this ); //adding mouse listener
      addMouseMotionListener( this ); // adding motion listener
   }


  // Drawing of the spaceship and laser beam
   public void paint( Graphics page ) {

          page.setColor(colorRand()); // random color laser beam
          page.drawLine(mouse_x-15, mouse_y-5,300,300);

          page.setColor( Color.YELLOW );//yellow spaceship
          page.fillOval( mouse_x-30, mouse_y-15, 60, 30 );


       }

   public void mouseEntered( MouseEvent e ) {

   }
   public void mouseExited( MouseEvent e ) {

   }
   public void mouseClicked( MouseEvent e ) {
      shots++;
      showStatus("Number of shots: " + shots);

   }
   public void mousePressed( MouseEvent e ) { 
      buttonPressed = true;  
      repaint();

   }
   public void mouseReleased( MouseEvent e ) {
      buttonPressed = false;
      setBackground( Color.black );
      repaint();

   }
   public void mouseMoved( MouseEvent e ) { 
      mouse_x = e.getX();
      mouse_y = e.getY();
      repaint();

   }
   public void mouseDragged( MouseEvent e ) {

   }

   //method generating a random color RGB
   public Color colorRand(){

       int r = (int)(Math.random()*256);
       int g = (int)(Math.random()*256);
       int b = (int)(Math.random()*256);

        Color randomColor = new Color(r,g,b);
        return randomColor;
   }


}

非常感谢,我已经陷入了相当长时间的困境。

滴滴


为什么不使用Math.random()来让激光随机射击某个位置呢? - Outsider
2个回答

3
您可以使用一个简单的算法:

//find a random angle :
double randomAngle = Math.random()*Math.PI*2;

//find the diameter of the circle around your ship that contains all the screen
int maxX = Math.max( mouse_x, screenWidth - mouse_x );
int maxY = Math.max( mouse_y, screenHeight - mouse_y );
int diam = (int) Math.sqrt( maxX * maxX + maxY * maxY );

//Then take the point of this circle at randomAngle : 
int x2 = mouse_x + (int) diam*Math.cos( randomAngle );
int y2 = mouse_y + (int) diam*Math.sin( randomAngle );
page.drawLine( mouse_x - 15, mouse_y - 5, x2, y2 );

您可以在这个帖子中获取屏幕尺寸的信息:如何在Java中获取屏幕分辨率?


我实际上尝试过,但这行代码必须跨越整个屏幕。 - Didi Bui
你的意思是什么?全屏幕吗?还是从你的船到屏幕边缘? - Snicolas
从船的位置移动到屏幕边缘。 - Didi Bui
Yitzih和Snicolas,我印象深刻。你们太棒了。 - Didi Bui
没有秘诀,就是多练习。 - Snicolas

3
类似于Snicolas提供的答案:
int x2;
int y2;

//get direction for x cooord
int direction = (int) (Math.random() * 2);

if(direction == 0)
    x2 = (int) (300 + Math.random() * applet_width);
else
    x2 = ((int) (300 + Math.random() * applet_width)) * -1;


//get direction for the y coord
direction = (int) (Math.random() * 2);

if(direction == 0)
    y2 = (int) (300 + Math.random() * applet_width);
else
    y2 = ((int) (300 + Math.random() * applet_width)) * -1;


//draw the line
page.drawLine(mouse_x-15, mouse_y-5,x2,y2);

这将创建一条直线连接到一个随机点,超过屏幕边缘。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接