简体   繁体   中英

My Character won't move on the screen

I am programming a star ship game and my character won't move, I am using MouseMotionListener for the character to move, but i can't move it at all. here's the code.

the main 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
    }

}

the panel for drawing:

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


    }

}

the game engine:

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

}

and here is the interface:

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
}

Can someone please help i would really appreciate it.

When you created the interface to hold the variables, you actually made them constants.

If you really need global variables (which I highly discourage) use this kind of 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
}

This solution should work straightaway. But I believe you need a redesign of your application.

A better solution would be to encapsulate the variables in their pertaining classes. For instance, a SpaceShip class that contains private variables about it's location and speed with getters and setters.

Something like this:

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

}

Also notice the naming of the variables. In java, it is not convention to have variable names such as Ship_X

I find your code has a hard structure to follow:

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

I don't know why you keep creating extra methods to invoke a couple of lines of code. There is no reason the code can't all be in the constructor:

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

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

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

Don't know if it will fix the problem but in the GalacticEngine class you have:

GalacticPanel g1=new GalacticPanel();

You don't need that statement since you create the GalacticPane in your Galactic class, So, instead, you should pass the GalacticPanel as a parameter to your GalacticEngine class.

You've got a couple of things wrong here. First the line g.drawImage(img, Ship_X, Ship_Y, Ship_Width, Ship_Height,null); Notice that you are trying to draw your icon at the coordinates Ship_X/Ship_Y. These are constant values.

There is another problem in GalacticEngine1. The panel variable g1 which you create in this class and also try to repaint in the mouse move listener is not the same Galactic panel that you created and displayed in the main class.

Also in GalacticEngine1 you are updating a variable named ShipX which is the variable you really want to use in the call to drawImage.

What you need to do here is make the engine aware of the panel you created and you need to make the panel aware of what the engine's x variable should be. I'm not holding the following out to be a good object oriented design or anything and I had to change the code to draw a rectangle instead of you JPEG (couldn't get a JPEG to load) but it generally does what you are trying to do I think. Constants did not change. Also you should heed the advice RE structure and style given in the other answers.

Main

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
    }

}

Panel

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


    }

}

Engine

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM