gpt4 book ai didi

java - 我的角色不会在屏幕上移动

转载 作者:行者123 更新时间:2023-12-01 21:58:51 25 4
gpt4 key购买 nike

我正在编写一个星际飞船游戏,我的角色不会移动,我使用 MouseMotionListener 让角色移动,但我根本无法移动它。这是代码。

主类:

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.WindowConstants;
@SuppressWarnings("serial")
public class Galactic extends JFrame implements GalacticConstants1 {
private ImageIcon windowicon=new ImageIcon(getClass().getResource("galactic ship.jpg"));
Galactic(){
galacticComponents();
}
private void galacticComponents() {
setIconImage(windowicon.getImage());
panel();
}
private void panel(){
GalacticPanel g1=new GalacticPanel();
add(g1);

GalacticEngine1 ge=new GalacticEngine1();
addMouseMotionListener(ge);
addKeyListener(ge);
}
public static void main(String[] args) {
Galactic g=new Galactic();
//Initializing game
g.setSize(d);
//setting the game size
g.setTitle("Galactic Ship");
//setting the game title
g.setVisible(true);
//the visibility
g.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
//what to do when the close button is pressed
g.setResizable(false);
//if the window can be resized
g.setLocationRelativeTo(null);
//the location on the screen
}

}

绘图面板:

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;

import javax.swing.ImageIcon;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class GalacticPanel extends JPanel implements GalacticConstants1{

public Point point=new Point(Ship_X,Ship_Y);
Image img;
ImageIcon i;
public GalacticPanel() {
setBackground(Color.black);
}

public void paintComponent(Graphics g){
super.paintComponent(g);

g.setColor(Color.red);
//red star color
g.drawOval(20,40,Star_Width,Star_Height);
g.fillOval(20,40,Star_Width,Star_Height);
//first red star
g.drawOval(200,200,Star_Width,Star_Height);
g.fillOval(200,200,Star_Width,Star_Height);
//second red star
g.drawOval(300,400,Star_Width,Star_Height);
g.fillOval(300,400,Star_Width,Star_Height);
//third red star
g.drawOval(400,550,Star_Width,Star_Height);
g.fillOval(400,550,Star_Width,Star_Height);
//fourth red star

g.setColor(Color.black);
g.drawRect(0, 0, recW, recH);
//invisible bounds

i=new ImageIcon(getClass().getResource("galactic ship.jpg"));
img=i.getImage();
g.drawImage(img, Ship_X, Ship_Y, Ship_Width, Ship_Height,null);
//the ship x and y coordinates the ship width and height arcwidth and archeight


}

}

游戏引擎:

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;

public class GalacticEngine1 implements MouseMotionListener,KeyListener,GalacticConstants1 {
public int ShipX=Ship_X;
GalacticPanel g1=new GalacticPanel();

GalacticEngine1(){
}

@Override
public void mouseDragged(MouseEvent e) {

}
@Override
public void mouseMoved(MouseEvent e) {
int mouseX=e.getX();

if(mouseX<ShipX&&ShipX<recW){
ShipX-=Ship_Movement;
}else if(mouseX>ShipX){
ShipX+=Ship_Movement;
}
g1.repaint();
}

@Override
public void keyPressed(KeyEvent e) {
int key=e.getKeyCode();
if(key==KeyEvent.VK_ESCAPE){
System.exit(0);
}
}

@Override
public void keyReleased(KeyEvent e) {}

@Override
public void keyTyped(KeyEvent e) {}

}

这是界面:

import java.awt.Dimension;
public interface GalacticConstants1 {
int Width=500;
int Height=740;
Dimension d=new Dimension(Width,Height);
//Screen dimension
int Ship_X=230;
int Ship_Y=670;
int Ship_Width=40;
int Ship_Height=20;
int Ship_Movement=5;
//Ship qualities
int Star_Width=5;
int Star_Height=5;
//star qualities
int recW=490;
int recH=700;
//invisible rectangle width and height
}

有人可以帮忙吗,我真的很感激。

最佳答案

你这里有一些错误。首先是行 g.drawImage(img, Ship_X, Ship_Y, Ship_Width, Ship_Height,null); 请注意,您正在尝试在坐标 Ship_X/Ship_Y 处绘制图标。这些是常数值。

GalicateEngine1 中还有另一个问题。您在此类中创建并尝试在鼠标移动监听器中重新绘制的面板变量 g1 与您在主类中创建和显示的银河面板不同。

此外,在 GalicateEngine1 中,您正在更新一个名为 ShipX 的变量,这是您真正想在调用 drawImage 时使用的变量。

这里您需要做的是让引擎知道您创建的面板,并且您需要让面板知道引擎的 x 变量应该是什么。我不认为以下是一个好的面向对象的设计或任何东西,我不得不更改代码来绘制一个矩形而不是你的JPEG(无法加载JPEG),但它通常会做你正在尝试的事情做我认为的。常数没有改变。此外,您还应该注意其他答案中给出的 RE 结构和风格的建议。

主要

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.WindowConstants;
@SuppressWarnings("serial")
public class Galactic extends JFrame implements GalacticConstants1 {
// private ImageIcon windowicon=new ImageIcon(getClass().getResource("galactic ship.jpg"));
private ImageIcon windowicon=new ImageIcon("file:blah","");
Galactic(){
galacticComponents();
}
private void galacticComponents() {
setIconImage(windowicon.getImage());
panel();
}
private void panel(){
GalacticEngine1 ge=new GalacticEngine1();
GalacticPanel g1=new GalacticPanel();
ge.setPanel(g1);
g1.setEngine(ge);
add(g1);

g1.addMouseMotionListener(ge);
g1.addKeyListener(ge);
}
public static void main(String[] args) {
Galactic g=new Galactic();
//Initializing game
g.setSize(d);
//setting the game size
g.setTitle("Galactic Ship");
//setting the game title
g.setVisible(true);
//the visibility
g.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
//what to do when the close button is pressed
g.setResizable(false);
//if the window can be resized
g.setLocationRelativeTo(null);
//the location on the screen
}

}

面板

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;

import javax.swing.ImageIcon;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class GalacticPanel extends JPanel implements GalacticConstants1{

public Point point=new Point(Ship_X,Ship_Y);
Image img;
ImageIcon i;
GalacticEngine1 ge;

public void setEngine(GalacticEngine1 ge) {
this.ge = ge;
}
public GalacticPanel() {

setBackground(Color.white);
}

public void paintComponent(Graphics g){
super.paintComponent(g);

System.out.println("Painting");

g.setColor(Color.blue);
//red star color
g.drawOval(20,40,Star_Width,Star_Height);
g.fillOval(20,40,Star_Width,Star_Height);
//first red star
g.drawOval(200,200,Star_Width,Star_Height);
g.fillOval(200,200,Star_Width,Star_Height);
//second red star
g.drawOval(300,400,Star_Width,Star_Height);
g.fillOval(300,400,Star_Width,Star_Height);
//third red star
g.drawOval(400,550,Star_Width,Star_Height);
g.fillOval(400,550,Star_Width,Star_Height);
//fourth red star

g.setColor(Color.black);
g.drawRect(0, 0, recW, recH);
//invisible bounds


g.drawRect(ge.getX(), Ship_Y, 20, 20);
//the ship x and y coordinates the ship width and height arcwidth and archeight


}

}

引擎

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;

public class GalacticEngine1 implements MouseMotionListener,KeyListener,GalacticConstants1 {
public int ShipX=Ship_X;
GalacticPanel p;


public int getX() {return ShipX;}


public void setPanel(GalacticPanel p) {
this.p = p;
}

GalacticEngine1(){
}

public void mouseDragged(MouseEvent e) {

}
public void mouseMoved(MouseEvent e) {
int mouseX=e.getX();

if(mouseX<ShipX&&ShipX<recW){
ShipX-=Ship_Movement;
}else if(mouseX>ShipX){
ShipX+=Ship_Movement;
}

p.repaint();

}

public void keyPressed(KeyEvent e) {
int key=e.getKeyCode();
if(key==KeyEvent.VK_ESCAPE){
System.exit(0);
}
}

public void keyReleased(KeyEvent e) {}

public void keyTyped(KeyEvent e) {}

}

关于java - 我的角色不会在屏幕上移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33961556/

25 4 0
文章推荐: java - JPA 按列表元素属性从实体顺序中选择 List