Results 1 to 2 of 2
  1. #1
    tejens23 is offline Member
    Join Date
    Nov 2009
    Posts
    8
    Rep Power
    0

    Question Need help please

    I'm taking a java class and I have been assigned a project where we have to create some kind of spaceship(can be just a regular shape like a rectangle) that follows the mouse pointer after the pointer enters the application frame. It also has to shoot a continuous laser(a straight line that extends from the ship) when the mouse is pressed. The laser beam has to keep extending until the mouse button is released.

    The problem I am having is that the shape that follows the mouse pointer leaves a trail. And my laser stops before I release the mouse button.

    This is the code I started out with:

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class SpaceshipFrame extends JFrame implements MouseListener, MouseMotionListener
    {
        private JLabel statusBar;
        private int xValue = -10, yValue = -10;
        private boolean shoot = false;
        public SpaceshipFrame()
        {
            super("Spaceship");
            statusBar = new JLabel();
            getContentPane().add(statusBar);
            addMouseListener(this);
            addMouseMotionListener(this);
    
            setSize(1200, 400);
            setVisible(true);
        }
        public void mouseClicked(MouseEvent event){}
        public void mousePressed(MouseEvent event){
        	xValue = event.getX();
        	yValue = event.getY();
        	shoot = true;
        	repaint();
        }
        public void mouseReleased(MouseEvent event){
        	shoot = false;
        }
        public void mouseEntered(MouseEvent event){}
        public void mouseExited(MouseEvent event){}
        public void mouseDragged(MouseEvent event){}
        public void mouseMoved(MouseEvent event)
        {
            statusBar.setText("moved at ["+event.getX()+", "+event.getY()+"]");
            xValue = event.getX();
            yValue = event.getY();
            repaint();
        }
        public void paint(Graphics page)
        {
            page.setColor (Color.BLACK);
            page.drawRect(xValue-20, yValue-20, 100,50);
            if(shoot)
            {
            	page.setColor(Color.RED);
            	page.drawLine(xValue, yValue-10, 50+xValue, yValue-10);
            }
    
        }
       //-----------------------------------------------------------------
       //  Creates and displays the application frame.
       //-----------------------------------------------------------------
       public static void main (String[] args)
       {
          SpaceshipFrame frame = new SpaceshipFrame();
          frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
    
       }
    }
    Last edited by Fubarable; 11-07-2009 at 05:24 PM. Reason: code tags added to aid code readability

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I have several recommendations to make, that you may use or not as you choose.

    1) If you want your component to erase the old rectangle, you'll want to call
    Java Code:
    super.paint(page);
    as the first method call within your paint method. This tells the component to clear its contents and repaint the background.
    2) Having said this, you really shouldn't be drawing in a JFrame or adding a mouse listener to this JFrame. Rather you should draw in a JPanel and add the mouse (motion) listener (I'd use a private inner class that extends MouseAdapter myself) to the JPanel.
    3) When you do change this to a JPanel, then paint in the class's paintComponent override, not paint override.
    4) The first method call from within your paintComponent method should be
    Java Code:
    super.paintComponent(page);
    for the same reasons as outlined above.
    5) I'd also move the rectangle on mouse drag.
    6) To have an animation (the laser bolt), you'll want to use a Swing Timer that starts when the mouse is pressed and stops when it's released.

    Last: please use code tags when posting code. I've taken the liberty of editing your post and adding these since you are new, but in the future, we'd appreciate it if you do this yourself. It makes your code much easier to read and this will mean that more will likely read your code. Please see my signature and its link for more information on this.

    Much luck and welcome to the forum!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •