I am working on an interesting assignment consisting of a small ship that moves with mouseMoved () and shoots a laser beam in random directions. I want to use drawLine (mouse_x, mouse_y,?,?) For the laser, but I cannot determine the coordinates x2 and y2. The laser should cross the screen.
This is what I still have. page.drawLine(mouse_x-15, mouse_y-5,300,300);Of course, I do not want the laser to continue to shoot at an angle (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;
private int applet_height =300;
private int mouse_x, mouse_y;
private int shots = 0;
private boolean buttonPressed = false;
public void init() {
setSize(applet_width, applet_height);
setBackground( Color.black );
mouse_x = applet_width/2;
mouse_y = applet_height/2;
addMouseListener( this );
addMouseMotionListener( this );
}
public void paint( Graphics page ) {
page.setColor(colorRand());
page.drawLine(mouse_x-15, mouse_y-5,300,300);
page.setColor( Color.YELLOW );
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 ) {
}
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;
}
}
Thanks in advance, I’ve been stuck with this for a long time.
Didi
source
share