Results 1 to 2 of 2
  1. #1
    MurderfaceX4 is offline Member
    Join Date
    Dec 2007
    Location
    TX
    Posts
    1
    Rep Power
    0

    Question I need help with my MouseMotionAdapter and MouseListener.

    import java.awt.*;
    import java.awt.image.*;
    import java.awt.event.*;

    import javax.swing.*;
    import javax.swing.event.*;

    public class Shooter extends Canvas implements MouseListener
    {
    public int width, height;
    public BufferStrategy bs; // <--- lol, bs.
    public final Image ship = Toolkit.getDefaultToolkit().createImage("Ship.bmp" );
    public final Image ground = Toolkit.getDefaultToolkit().createImage("ground.bm p");
    public final Cursor c = Toolkit.getDefaultToolkit().createCustomCursor(shi p, new Point(0, 0) , "cursor");
    public Graphics g = bs.getDrawGraphics();
    Thread t;
    Frame f;

    public boolean cached = false;

    public Shooter()
    {
    Frame f = new Frame();
    f.setCursor(c);
    f.setLayout(new FlowLayout());
    f.resize(900, 900);
    f.addWindowListener(new WindowHandler());
    f.setVisible(true);
    try
    {
    repaint();
    Thread.sleep( 1000 );
    }
    catch(Exception e) {System.out.println(e);}
    }
    public static void main(String[] args)
    {

    }

    public void mouseClicked(MouseEvent arg0) {Toolkit.getDefaultToolkit().beep();}
    public void mouseEntered(MouseEvent arg0) {}
    public void mouseExited(MouseEvent arg0) {}
    public void mousePressed(MouseEvent arg0) {}
    public void mouseReleased(MouseEvent arg0) {}

    }
    class WindowHandler extends WindowAdapter
    {
    public void windowClosing(WindowEvent event)
    {
    System.exit(0);
    }
    }
    class Ship
    {
    public int lives = 3;
    public int speed = 3;
    public boolean moving = false;
    public boolean fire = true;

    }
    class Display extends Canvas
    {
    public Ship s = new Ship();
    public static int score = 0;
    public static int level = 1;

    public final Image ship = Toolkit.getDefaultToolkit().createImage("Ship.bmp" );
    public final Image ground = Toolkit.getDefaultToolkit().createImage("ground.bm p");

    public static final int WIDTH = 900;
    public static final int HEIGHT = 900;

    public long gameTime;
    public long numNums;
    public Display()
    {
    Frame f = new Frame();
    f.setSize(WIDTH, HEIGHT);
    f.setLocation(50, 50);
    f.setUndecorated(true);
    f.add(this);
    this.setFocusable(false);
    f.addWindowListener(new WindowHandler());

    }
    public void drawScreen(Graphics g)
    {
    Graphics2D g2d = (Graphics2D) g;

    g2d.drawImage(ship, -100, -100, this);
    g2d.drawImage(ground, getWidth(), getHeight(), this);

    g2d.setColor(Color.BLACK);
    g2d.fillRect(0,0, getWidth(), getHeight());

    g2d.setColor(Color.WHITE);
    g2d.drawString("Score: " + score, 20, 20);
    g2d.drawString("Lives: " + s.lives, 20, 30);
    g2d.drawString("Level " + level, WIDTH - 60, 20);
    }
    public void update(Graphics g)
    {
    drawScreen(g);
    }
    }
    class Mouse extends MouseAdapter
    {
    public void mouseClicked(MouseEvent me)
    {
    // i want to make it where you click the mouse and,
    // a bullet will come out. I have the collision detecting already.
    }
    }

    class Mouse2 extends MouseMotionAdapter
    {
    public void mouseMoved(MouseEvent me)
    {
    // i want to make the mouse the ship.
    //like every time you move your mouse the ship moves.
    }
    }

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    When you compile and get something like this in the console
    Java Code:
    C:\jexp>javac shooterrx.java
    Note: shooterrx.java uses or overrides a deprecated API.
    Note: Recompile with -Xlint:deprecation for details.
    you can do this to find out what is deprecated
    Java Code:
    C:\jexp>javac -Xlint:deprecation shooterrx.java
    shooterrx.java:27: warning: [deprecation] resize(int,int) in java.awt.Component
    has been deprecated
            f.resize(900, 900);
             ^
    1 warning
    and then look up the method in the indicated api to find what to replace it with.
    Java Code:
    import java.awt.*;
    import java.awt.image.*;
    import java.awt.event.*;
    import javax.swing.event.MouseInputAdapter;
    
    public class ShooterRx extends Canvas
    {
        Image ground;
        Image ship;
        Thread t;
        public Ship s = new Ship();
        public static int score = 0;
        public static int level = 1;
        public long gameTime;
        public long numNums;
        public boolean cached = false;
    
        public ShooterRx()
        {
            ship = loadImage("Ship.bmp");
                             //"images/dukeWaveRed.gif");
            ground = loadImage("ground.bmp");
            Cursor c = Toolkit.getDefaultToolkit().
                       createCustomCursor(ship, new Point(0, 0), "cursor");
            setCursor(c);
            MouseHandler handler = new MouseHandler();
            addMouseListener(handler);
            addMouseMotionListener(handler);
        }
    
        private Image loadImage(String path)
        {
            Image image = Toolkit.getDefaultToolkit().createImage(path);
            MediaTracker mt = new MediaTracker(this);
            mt.addImage(image, 0);
            try
            {
                mt.waitForID(0);
            }
            catch(InterruptedException e)
            {
                System.out.println("load interruption for: " + path);
            }
            return image;
        }
    
        public void paint(Graphics g)
        {
            drawScreen(g);
        }
    
        public void update(Graphics g)
        {
    //        drawScreen(g);
            paint(g);
        }
    
        public void drawScreen(Graphics g)
        {
            Graphics2D g2d = (Graphics2D) g;
    
            g2d.setColor(Color.BLACK);
            g2d.fillRect(0,0, getWidth(), getHeight());
    
            g2d.drawImage(ship, 200, 200, this);
            int x = 0;
            int y = getHeight() - ground.getHeight(this);
            g2d.drawImage(ground, x, y, this);
    
            g2d.setColor(Color.WHITE);
            g2d.drawString("Score: " + score, 20, 20);
            g2d.drawString("Lives: " + s.lives, 20, 30);
            g2d.drawString("Level " + level, getWidth() - 60, 20);
        }
    
        public static void main(String[] args) 
        {
            ShooterRx app = new ShooterRx();
            Frame f = new Frame();
    //        f.setLayout(new FlowLayout());
            System.out.println("Frame default layout = " +
                                f.getLayout().getClass().getName());
            f.add(app);
            f.setSize(600, 600);
            f.addWindowListener(new WindowHandler());
            f.setVisible(true);
        }
    
        private class MouseHandler extends MouseInputAdapter
        {
            public void mouseClicked(MouseEvent arg0)
            {
                // i want to make it where you click the mouse and,
                // a bullet will come out. I have the collision detecting already.
                Toolkit.getDefaultToolkit().beep();
            }
    
            public void mouseMoved(MouseEvent me)
            {
                // i want to make the mouse the ship.
                //like every time you move your mouse the ship moves.
                // Should work if you make the ship the Cursor.
            }
        }
    }
    
    class WindowHandler extends WindowAdapter
    {
        public void windowClosing(WindowEvent event)
        {
            System.exit(0);
        }
    }
    
    class Ship 
    {
        public int lives = 3;
        public int speed = 3;
        public boolean moving = false;
        public boolean fire = true;
    }

Similar Threads

  1. Null array when passed to MouseListener
    By stevemcc in forum New To Java
    Replies: 2
    Last Post: 04-02-2008, 10:42 PM

Posting Permissions

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