gpt4 book ai didi

java - JLabel,通过mosemotionliistner控制,当另一个jlabel(不受鼠标控制)进入和离开JFrame时返回到原来的位置

转载 作者:行者123 更新时间:2023-12-01 11:17:40 26 4
gpt4 key购买 nike

我正在制作一款类似于疯狂喂食的游戏。我使用 mouseMotionListenerJFrame 周围移动 JLabel。同时,还有其他的JLabel充当其他鱼的角色,被鼠标控制的JLabel吃掉。每次JLabel鱼(不受鼠标控制)移入和移出屏幕时,由鼠标控制的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());
}
}
}

最佳答案

首先:

  1. 类名称以大写字符开头。通过例子学习。只需查看 Java API,您就会看到所有类名都以大写字符开头。

  2. 不要在绘画方法中执行 I/O。每当 Swing 确定需要重新绘制组件时,都会调用绘制方法。每次都读取文件效率不高。创建类时读取图像。

  3. 不要对框架的大小进行硬编码 - board.setSize(1300, 700); 并非每个人都使用相同的分辨率。要最大化框架,您可以使用: board.setExtendedState(JFrame.MAXIMIZED_BOTH);

returns back to a standard location on the screen, the center of the top half of the screen

默认情况下,JPanel 使用FlowLayout。当您更改其他标签的位置时,将调用布局管理器来将组件放置在布局管理器确定的位置中。

如果由于使用鼠标拖动组件而随机放置组件,则需要在面板上使用“空布局”。当您执行此操作时,您现在负责手动设置添加到面板的每个组件的“大小和位置”。

关于java - JLabel,通过mosemotionliistner控制,当另一个jlabel(不受鼠标控制)进入和离开JFrame时返回到原来的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31614891/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com