简体   繁体   中英

Using mouseEntered

I'm trying to make a game where you die when the object rock hit you but I'm failing to use the MouseEntered method. Could someone show me how? I also want to ask about loading all the sprite image. I success to load them in but fail to make the rocks change from image to image.

Tester class

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.Timer;



public class Tester extends JPanel{ 

    public JFrame window = new JFrame("Rock stuff");
    public Timer tmr ;
    public ArrayList<Rock> rocks = new ArrayList<Rock>();


    public Tester(){
        window.setBounds(500, 400, 500, 500);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setVisible(true);
        window.add(this);



        tmr = new Timer(1, new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                for(Rock r:rocks)
                    r.move();
                repaint();
            }
        });

        this.addMouseListener(new MouseListener() {

            @Override
            public void mouseReleased(MouseEvent e) { }

            @Override
            public void mousePressed(MouseEvent e) {
                for(Rock r:rocks) {
                    if(r.contains(e.getPoint())){
                        Rock newRock = r.split();
                        if(newRock != null){
                            rocks.add(newRock);
                            rocks.add(newRock);
                        }
                        else{
                            rocks.remove(r);
                            if(rocks.size()==0){
                                tmr.stop();
                                repaint();
                                JOptionPane.showMessageDialog(window, "Game Over");
                            }
                        }
                        return;

                    }
                }
            }

            @Override
            public void mouseExited(MouseEvent e) {
                // TODO Auto-generated method stub

            }

            @Override
            public void mouseEntered(MouseEvent e) {
                for(Rock r:rocks){
                    if(r.contains(e.getPoint())){
                        rocks.clear();
                        JOptionPane.showMessageDialog(window, "Game Over");
                    }
                }

            }

            @Override
            public void mouseClicked(MouseEvent e) {

            }
        });
        addrock(10);


        tmr.start();;
    }


    private void addrock(int count) {
        Random rnd = new Random();
        for(int i=0;i<count;i++)
            rocks.add(new Rock( rnd.nextInt(getWidth()),
                    rnd.nextInt(getHeight()),
                    32,32,
                    (rnd.nextBoolean()?1:-1),
                    (rnd.nextBoolean()?1:-1),
                    true
                    ) );
    }

    public void paint(Graphics g){
        super.paint(g);
        Rock.sw = getWidth();
        Rock.sh = getHeight();
        for(Rock r:rocks) r.draw(g);

    }

    public static void main(String[] args) {
        new Tester();
    }
}

Rocks class

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

public class Rock extends Rectangle{

    public static int sw, sh;

    public int dx, dy;
    public boolean isMoving;
    public BufferedImage sprite;
    //================================================= Constructors

    public Rock(int x, int y, int width, int height, int dx, int dy, boolean isMoving) {
        super();
        this.width = width;
        this.height = height;
        this.x = x;
        this.y = y;
        this.dx = dx;
        this.dy = dy;
        this.isMoving = isMoving;
        this.sprite = init();
    }

    //================================================= Methods
    public BufferedImage init(){
        BufferedImageLoader loader = new BufferedImageLoader();
        BufferedImage spriteSheet = null;
        try {
            spriteSheet = loader.loadImage("hero.png");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        SpriteSheet ss = new SpriteSheet(spriteSheet);
        sprite = ss.grabSprite(0, 0, 32, 32);
        return sprite;
    }

    public void move() {
        if(isMoving == false) return;

        x += dx;
        y += dy;

        if(x + width > sw) {
            x = sw - width;
            dx = -dx;
        }

        if(y + height > sh) {
            y = sh - height;
            dy = -dy;
        }

        if(x < 0) {
            x = 0;
            dx = -dx;
        }

        if(y < 0) {
            y=0;
            dy = -dy;
        }
    }

    public Rock split(){
        Rock r = null;
        Random rnd = new Random();
        if(width > 20){
            dx=(rnd.nextBoolean()?1:-1);
            dy=(rnd.nextBoolean()?1:-1);
            r = new Rock(x,y,width,height,dx,dy,true);
        }

        return r;
    }

    public void draw(Graphics g) {
        g.drawImage(sprite,x,y,null);
    }

}

The basic answer to your mouseEntered problem is...

mouseEntered will be called when you mouse enters the component, it will never be called while the mouse continues to be within the component.

Instead, maybe consider using MouseMotionListener#mouseMoved which will tell when the mouse is moved over your component.

Your second question requires more code, in particular the SpriteSheet class, but I suspect that you need to know the current frame you are painting and simply use SpriteSheet#grabSprite to grab the appropriate image for the frame.

This will require you to know:

  1. The number of frames per second you are trying to achieve
  2. The amount of time which has passed from the start of the cycle (a cycle been one second in this case)

This will allow you to calculate the frame and from that calculate the image you should be displaying

As a rough example, you could have a look at How to Draw an BufferedImage to a JPanel

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM