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

My Character won't move on the screen

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

主要class:

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
}

有人可以帮忙吗,我将不胜感激。

当您创建用于保存变量的接口时,实际上是将它们设为常量。

如果你真的需要全局变量(我强烈反对)使用这种class:

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

此解决方案应该可以直接使用。但我相信您需要重新设计您的应用程序。

更好的解决方案是将变量封装在它们相关的 classes 中。例如,一个 SpaceShip class 包含有关其位置和速度的私有变量以及 getter 和 setter。

像这样:

public class SpaceShip {

    private int shipX;
    private int shipY;
    private int shipWidth;
    private int shipHeight;
    private int shipMovement;

    public SpaceShip() {
        shipX = 230;
        shipY = 670;
        shipWidth = 40;
        shipHeight = 20;
        shipMovement = 5;
    }

    public int getShipX() {
        return shipX;
    }

    public void setShipX(int shipX) {
        this.shipX = shipX;
    }

    public int getShipY() {
        return shipY;
    }

    public void setShipY(int shipY) {
        this.shipY = shipY;
    }

    public int getShipWidth() {
        return shipWidth;
    }

    public void setShipWidth(int shipWidth) {
        this.shipWidth = shipWidth;
    }

    public int getShipHeight() {
        return shipHeight;
    }

    public void setShipHeight(int shipHeight) {
        this.shipHeight = shipHeight;
    }

    public int getShipMovement() {
        return shipMovement;
    }

    public void setShipMovement(int shipMovement) {
        this.shipMovement = shipMovement;
    } 

}

还要注意变量的命名。在 java 中,使用 Ship_X

这样的变量名是不符合惯例的

我发现您的代码结构很难遵循:

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);
}

我不知道你为什么要创建额外的方法来调用几行代码。没有理由代码不能全部在构造函数中:

Galactic()
{
    setIconImage(windowicon.getImage());

     GalacticPanel g1=new GalacticPanel();
     add(g1);

     GalacticEngine1 ge=new GalacticEngine1();
     addMouseMotionListener(ge);
     addKeyListener(ge);
}

不知道它是否会解决问题,但在 GalacticEngine class 中你有:

GalacticPanel g1=new GalacticPanel();

您不需要该语句,因为您在 Galactic class 中创建了 GalacticPane,因此,您应该将 GalacticPanel 作为参数传递给 GalacticEngine class。

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

GalacticEngine1 中还有一个问题。您在此 class 中创建并尝试在鼠标移动侦听器中重新绘制的面板变量 g1 与您在主 class 中创建和显示的 Galactic 面板不同。

同样在 GalacticEngine1 中,您正在更新名为 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) {}

}