SpaceShip that follows the mouse and fire a laser beam when you click the mouse
Posted by Techie456
Last Updated: March 26, 2013
  1998

import javax.swing.JLabel;
import javax.swing.JPanel;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.Random;


public class SpaceShip extends JPanel{
private int[] xRocket = {100, 120, 120, 130, 130, 70, 70, 80, 80};
private int[] yRocket = {15, 40, 115, 125, 150, 150, 125, 115, 40};
private int[] xWindow = {95, 105, 110, 90};
private int[] yWindow = {45, 45, 70, 70};
private int[] xFlame = {70, 70, 75, 80, 90, 100, 110, 115, 120, 130, 130};
private int[] yFlame = {155, 170, 165, 190, 170, 175, 160, 185, 160, 175, 155};
private boolean fire = false;
private int x, y;
  //-----------------------------------------------------------------
  //  Constructor: Sets up the basic characteristics of this panel.
  //-----------------------------------------------------------------
  public SpaceShip()
  {
   this.setBackground(Color.black);
   this.addMouseListener(new NavigationListener());
   this.addMouseMotionListener(new NavigationListener());   
  
  }

  //-----------------------------------------------------------------
  //  Draws a rocket using polygons and polylines.
  //-----------------------------------------------------------------
  public void paintComponent (Graphics page)
  {
     super.paintComponent (page);
     
     page.translate(x, y);
     
     if(fire == true)
     {
     page.setColor(Color.red);
     page.drawLine(100, 50, 100, -300);
     }
     
     
     
     page.setColor (Color.cyan);
       page.fillPolygon (xRocket, yRocket, xRocket.length);
       
       page.setColor (Color.gray);
     page.fillPolygon (xWindow, yWindow, xWindow.length);
       
       page.setColor (Color.red);
     page.drawPolyline (xFlame, yFlame, xFlame.length);
     
     repaint();
  }
  
  
  
  private class NavigationListener implements MouseListener, MouseMotionListener
{
public void mouseMoved(MouseEvent event)
{
x = event.getX() - 100;
y = event.getY() - 50;
event.consume();
}
   public void mouseReleased (MouseEvent event)
   {
    fire=false;
    event.consume();
   }
   

   public void mouseDragged(MouseEvent event)
   {
    x = event.getX() - 100;
y = event.getY() - 50;
    event.consume();
   }
   
   
   public void mousePressed(MouseEvent event)
   {
    fire=true;
    event.consume();
   }
   
   public void mouseClicked (MouseEvent event){}
   public void mouseEntered (MouseEvent event){}
   public void mouseExited (MouseEvent event){}
}
}

// The driver class the down below
import java.awt.*;
import javax.swing.*;

public class SpaceMission {

public static void main(String[] args)
{
JFrame frame = new JFrame("Space Mission");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(new Dimension(800, 700));
frame.setContentPane(new SpaceShip());
frame.setVisible(true);
}
}

   
  
 
 
   

 
 
nice!