我正在编写一个星舰游戏并且我的角色不会移动,我使用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
}
有人可以帮忙,我真的很感激。
答案 0 :(得分:1)
当您创建用于保存变量的接口时,实际上它们就是常量。
如果真的需要全局变量(我强烈反对),请使用此类:
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
}
此解决方案应该可以直接使用。但我相信你需要重新设计你的应用程序。
更好的解决方案是将变量封装在相关的类中。例如,SpaceShip
类包含有关它的私有变量的位置和速度与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
答案 1 :(得分:1)
我发现你的代码有一个难以理解的结构:
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课程中你有:
GalacticPanel g1=new GalacticPanel();
因为你在Galactic课程中创建了GalacticPane,所以你不需要那个声明,所以,你应该将GalacticPanel作为参数传递给你的GalacticEngine课程。
答案 2 :(得分:1)
你在这里遇到了一些错误。第一行g.drawImage(img, Ship_X, Ship_Y, Ship_Width, Ship_Height,null);
请注意,您正尝试在坐标Ship_X / Ship_Y处绘制图标。这些是常数值。
GalacticEngine1还有另一个问题。您在此类中创建并尝试在鼠标移动侦听器中重新绘制的面板变量g1与您在主类中创建和显示的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) {}
}