我在做一个类似于喂食狂的游戏。我使用mouseMotionListener
在JFrame
周围移动JLabel
。同时,还有其他的JLabels
,作为其他的鱼,将被JLabel
控制的老鼠吃掉。每次JLabel fish
(不受鼠标控制)进出屏幕时,由鼠标控制的JLabel
返回到屏幕上的一个标准位置,即屏幕上半部分的中心位置。我能做些什么来阻止这一切的发生?
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import sun.audio.*;
import java.io.*;
import java.util.Timer;
import java.util.TimerTask;
import java.awt.event.MouseEvent;
public class playFishGame extends JPanel implements MouseMotionListener {
private JFrame board;// the main board
private JLabel fish;
private JLabel enemyFishS;
private JLabel enemyFishS2;
private JLabel enemyFishS3;
private ImageIcon fishPic;
private ImageIcon enemyFishSPic;
private ImageIcon winBackground;
private ImageIcon background;
private ImageIcon loseBackground;
ImageIcon fishSmall1r = new ImageIcon("data/fishSmall1r.png");
ImageIcon fishSmall1l = new ImageIcon("data/fishSmall1l.png");
ImageIcon fishSmall2r = new ImageIcon("data/fishSmall2r.png");
ImageIcon fishSmall2l = new ImageIcon("data/fishSmall2l.png");
ImageIcon fishSmall4r = new ImageIcon("data/fishSmall4r.png");
ImageIcon fishSmall4l = new ImageIcon("data/fishSmall4l.png");
private fish fishFish;
private fish enemyFishSFish;
private fish enemyFishSFish2;
private fish enemyFishSFish3;
private int origin;
private boolean contact, win;
Cursor blankCursor = null;
public static void main(String args[]) {
playFishGame play = new playFishGame();
}
public playFishGame() {
blankCursor = Toolkit.getDefaultToolkit().createCustomCursor(
Toolkit.getDefaultToolkit().createImage("data/blank.png"),
new Point(0, 0), "blankCursor"); // blank.png is any tranparent
// image.
board = new JFrame("Play Fish Game");
board.setSize(1300, 700);
board.getContentPane().setCursor(blankCursor);
board.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
board.add(this);// adds JLabel to JFrame
this.addMouseMotionListener(this);
board.setVisible(true);
ImageIcon fishPic = new ImageIcon("data/sfr.gif");
fish = new JLabel(fishPic);
enemyFishS = new JLabel(" ");
enemyFishS2 = new JLabel(" ");
fishFish = new fish(617, 0);
enemyFishSFish = new fish(1300, 350);
enemyFishSFish2 = new fish(0, 500);
// enemyFishSFish3= new fish(1300,200);
this.add(fish);
this.add(enemyFishS);
this.add(enemyFishS2);
contact = false;
win = false;
repaint();
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
if (enemyFishSFish.initiate == true) {
randomFishSmall(enemyFishSFish, enemyFishS);// picks image and
// starting
// point/side
enemyFishSFish.initiate = false;
}
if (enemyFishSFish.chooseSide == 0) {
enemyFishSFish.moveLeft();
if (enemyFishSFish.getX() < 5) {
enemyFishSFish.initiate = true;
}
} else if (enemyFishSFish.chooseSide == 1) {
enemyFishSFish.moveRight();
if (enemyFishSFish.getX() > 1295) {
enemyFishSFish.initiate = true;
}
}
if (enemyFishSFish2.initiate == true) {
randomFishSmall(enemyFishSFish2, enemyFishS2);// picks image and
// starting
// point/side
enemyFishSFish2.initiate = false;
}
if (enemyFishSFish2.chooseSide == 0) {
enemyFishSFish2.moveLeft();
if (enemyFishSFish2.getX() < 5) {
enemyFishSFish2.initiate = true;
}
} else if (enemyFishSFish2.chooseSide == 1) {
enemyFishSFish2.moveRight();
if (enemyFishSFish2.getX() > 1295) {
enemyFishSFish2.initiate = true;
}
}
enemyFishS.setLocation(enemyFishSFish.getX(), enemyFishSFish.getY());
contact(enemyFishSFish);
enemyFishS2.setLocation(enemyFishSFish2.getX(),
enemyFishSFish2.getY());
contact(enemyFishSFish2);
// contact(enemyFishSFish);
// enemyFishS2.setLocation(enemyFishSFish2.getX(),enemyFishSFish2.getY());
// enemyFishS3.setLocation(enemyFishSFish3.getX(),enemyFishSFish3.getY());
}
}, 0, 100);
board.setState(Frame.ICONIFIED);
board.setState(Frame.NORMAL);
}
public void mouseMoved(MouseEvent evt) {
System.out.println(evt.getPoint().x + ", " + evt.getPoint().y);
if ((evt.getPoint().x < 1231) && (evt.getPoint().y < 623)) {
fish.setLocation(evt.getPoint().x, evt.getPoint().y);
fishFish.override(evt.getPoint().x, evt.getPoint().y);
}
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
origin = fishFish.getX();
}
}, 0, 100);
int posneg = origin - evt.getPoint().x;
ImageIcon sfr = new ImageIcon("data/sfr.gif");
ImageIcon sfl = new ImageIcon("data/sfl.gif");
ImageIcon mfr = new ImageIcon("data/mfr.gif");
ImageIcon mfl = new ImageIcon("data/mfl.gif");
if (posneg < 0) {
if (fishFish.sFish < 10)
fish.setIcon(sfr);
if (fishFish.sFish > 9)
fish.setIcon(mfr);
}
if (posneg > 0) {
if (fishFish.sFish < 10)
fish.setIcon(sfl);
if (fishFish.sFish > 9)
fish.setIcon(mfl);
}
}
public void mouseDragged(MouseEvent evt) {
}
// 95/34
public void contact(fish enemyFish) {
if ((Math.abs(fishFish.getX() - enemyFish.getX())) < 48
&& (Math.abs(fishFish.getY() - enemyFish.getY()) < 36)
&& (fishFish.sFish < 10)) {
fishFish.sFish++;
enemyFish.initiate = true;
}
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
background = new ImageIcon("data/background3.png");
winBackground = new ImageIcon("data/");
loseBackground = new ImageIcon("data/");
if ((contact == false) && (win == false))
g.drawImage(background.getImage(), 0, 0, null);
if (contact == true)
g.drawImage(loseBackground.getImage(), 0, 0, null);
if (win == true)
g.drawImage(winBackground.getImage(), 0, 0, null);
}
public void randomFishSmall(fish changeFishFish, JLabel changeFish) {
int chooseType = (int) (Math.random() * 3);
// int chooseType=2;
int chooseSide = (int) (Math.random() * 2);// left=0right=1
int chooseSpeed = (int) (Math.random() * 12) + 6;
int choosePlacement = (int) (Math.random() * 1288) + 15;
changeFishFish.chooseType = chooseType;
changeFishFish.chooseSide = chooseSide;
changeFishFish.chooseSpeed = chooseSpeed;
changeFishFish.choosePlacement = choosePlacement;
if (chooseType == 0) {
if (chooseSide == 0) {
changeFish.setIcon(fishSmall1l);
changeFishFish.override(1300, choosePlacement);
// changeFishFish.
} else {
changeFish.setIcon(fishSmall1r);
changeFishFish.override(0, choosePlacement);
}
changeFish.setLocation(changeFishFish.getX(), changeFishFish.getY());
} else if (chooseType == 1) {
if (chooseSide == 0) {
changeFish.setIcon(fishSmall2l);
changeFishFish.override(1300, choosePlacement);
} else {
changeFish.setIcon(fishSmall2r);
changeFishFish.override(0, choosePlacement);
}
changeFish.setLocation(changeFishFish.getX(), changeFishFish.getY());
} else if (chooseType == 2) {
if (chooseSide == 0) {
changeFish.setIcon(fishSmall4l);
changeFishFish.override(1300, choosePlacement);
// changeFishFish.
} else {
changeFish.setIcon(fishSmall4r);
changeFishFish.override(0, choosePlacement);
}
changeFish.setLocation(changeFishFish.getX(), changeFishFish.getY());
}
}
}
发布于 2015-07-24 10:57:08
首先:
board.setSize(1300, 700);
并不是每个人都使用相同的分辨率。要最大化您可以使用的框架:board.setExtendedState(JFrame.MAXIMIZED_BOTH);
返回到屏幕上的标准位置,即屏幕上半部分的中心。
默认情况下,JPanel
使用FlowLayout
。当更改其他标签的位置时,布局管理器将被调用到由布局管理器确定的位置中的某个位置的组件中。
如果由于使用鼠标拖动组件而随机放置组件,则需要在面板上使用“空布局”。当您这样做时,您现在负责手动设置添加到面板中的每个组件的“大小和位置”。
https://stackoverflow.com/questions/31614891
复制相似问题