我正在进行一项有趣的任务,该任务包括一个随着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;
}
}
提前谢谢你,我已经坚持了很长一段时间了。
嘀嘀
答案 0 :(得分: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 );
您可以获取屏幕尺寸,例如此线程:How can I get screen resolution in java?。
答案 1 :(得分: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);
这将为通过屏幕边缘的随机点创建一条线