2

I am creating a small game for a java project (Swing GUI), we have to use a MVC pattern and I got the basic model and view down. I just can't get to understand how to use the keyboard keys to -for example- increment the X position of my character. What library should I use?

Here is my code (it's my first time coding):

/*A Thing type object can stand on a Tile,the characters and items class implement this interface.*/

public interface Thing {           
    public void attacked(int x);     
}                                   

public abstract class Character implements Thing{
    protected int health;
    protected int position[]=new int[2];
    protected int direction;
    protected void setHealth(int hp){
        this.health=hp;
    }
    public int getHealth(){
        return this.health;
    }
    public void setPositionX(int x){
        this.position[0]=x;
    }
    public void setPositionY(int y){
        this.position[1]=y;
    }
    public int getPositionX(){
        return this.position[0];
    }
    public int getPositionY(){
        return this.position[1];
    }
    public void setDirection(int x){
        this.direction=x;
    }
    public int getDirection(){
        return this.direction;
    }
    public void moveUp(){
        int posy=this.getPositionY();
        this.setPositionY(posy+1);
    }
    public void moveDown(){
        int posy=this.getPositionY();
        this.setPositionY(posy-1);
    }
    public void moveLeft(){
        int posx=this.getPositionX();
        this.setPositionX(posx-1);
    }
    public void moveRight(){
        int posx=this.getPositionX();
        this.setPositionX(posx+1);
    }
    public void attack(){

    }
    public void heavyAttack(){

    }
}
/*Only one type of character,just to have a really basic code to test then improve it*/

public class Mage extends Character{
    public Mage(){
        this.setHealth(5);
        this.setPositionX(3);
        this.setPositionY(3);
    }
    public void attacked(int x){
        this.setHealth(this.getHealth()-x);
    }
}

/*A Tile is a space on which a character/item (Thing) can stand,the level is a matrice of tiles*/

public class Tile {
    private Thing[] occupant;
    public Tile(){
        this.occupant=new Thing[1];
    }
    public Thing getOccupant(){
        return this.occupant[0];
    }
    public void addThing(Thing a){
        this.occupant[0]=a;
    }
    public void removeThing(){
        this.occupant[0]=null;
    }
}

public class Terrain {
    private String name;
    private Tile[][] checker;
    private int sizeX;
    private int sizeY;
    public Terrain(int x,int y,String Name){
        this.name=Name;
        this.sizeX=x;
        this.sizeY=y;
        this.checker= new Tile[y][x];
        for (int i=0; i<y; i++){
            for(int j=0; j<x; j++){
                this.checker[i][j]=new Tile();
            }
        }
    }
    public int getSizeX(){
        return sizeX;
    }
    public int getSizeY(){
        return sizeY;
    }
    public Thing getTileOccupant(int x,int y){
        return this.checker[y][x].getOccupant();
    }
    public void Spawn(Thing t,int x,int y){
        this.checker[y][x].addThing(t);
    }

}

/* The View,This was really just to test it out so it only shows the tiles and the mage sprite*/

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Array;

import javax.imageio.ImageIO;
import javax.swing.JPanel;
import javax.swing.Timer;
public class myPanel extends JPanel {
    private Terrain terrain;
    public myPanel(Terrain checker){
        this.terrain=checker;
        this.requestFocusInWindow();
    }
    protected void paintComponent(Graphics g){ 
        super.paintComponent(g);
        File file=new File("D:\\workspace\\Projet INFO-H-200\\src\\Images\\soltest.png");
        BufferedImage sol=null;
        try{
            sol=ImageIO.read(file);
        }
        catch (IOException e){
            e.printStackTrace();
        }
        file=new File("D:\\workspace\\Projet INFO-H-200\\src\\Images\\mage.png");
        BufferedImage mage=null;
        try{
            mage=ImageIO.read(file);
        }
        catch (IOException e){
            e.printStackTrace();
        }
        for (int i=0;i<terrain.getSizeY();i++){
            for (int j=0;j<terrain.getSizeX();j++){
                g.drawImage(sol, 33*j,33*i, null);
                if (terrain.getTileOccupant(j,i)!=null){
                    g.drawImage(mage, 33*j, 33*i, null);
                }
            }
        }
    }
}
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JFrame;

public class Main {
    public static void main(String[] args){
        Mage blubu=new Mage();
        Terrain terrain=new Terrain(10,10,"Test");
        terrain.Spawn(blubu, blubu.getPositionX(), blubu.getPositionY());

        Controler controler=new Controler(blubu);
        JFrame mainWindow=new JFrame();
        mainWindow.setSize(new Dimension(1000,800));
        mainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainWindow.setLocationRelativeTo(null); //au centre
        mainWindow.setLayout(new BorderLayout());//gérer les placements
        mainWindow.add(new myPanel(terrain), BorderLayout.CENTER); //on ajoute le panel au centre
        mainWindow.setVisible(true);
        mainWindow.toFront();
    }
}
zoisus
  • 31
  • 3
  • Console: Scanner, BufferedReader. GUI: KeyListener – dryairship Apr 10 '16 at 12:05
  • For better more specific help, please ask a more specific and informative question, including pertinent code and details of your problem. For instance if this were a Swing GUI (something you never tell us), a KeyListener would likely not be something most Swing experts would recommend. – Hovercraft Full Of Eels Apr 10 '16 at 12:33
  • [For example](http://stackoverflow.com/questions/12991986/pong-controls-with-keylistener/12992048#12992048). Also please have a look at [these answers](http://stackoverflow.com/search?tab=votes&q=user%3a522444%20keylistener). – Hovercraft Full Of Eels Apr 10 '16 at 12:35
  • Thank you very much,with KeyListeners I had the problem of never having focus but I didn't know why. I will definitely try the other method though. – zoisus Apr 10 '16 at 13:55
  • @zoisus: and that's exactly why you shouldn't use a KeyListener -- use Key Bindings. – Hovercraft Full Of Eels Apr 10 '16 at 14:05
  • And yes I am using a Swing GUI,I will first try to make it work by myself otherwise I will never learn how to code alone, but if I really can't make it I will post my code to ask for deeper help,thank you @HovercraftFullOfEels! – zoisus Apr 10 '16 at 14:06
  • Never read in images within a paintComponent method. 1) It's not necessary to keep reading in the same image over and over. Read it in once in a constructor, assign it to a field and be done with it. 2) This unnecessarily slows down painting and thus slows the perceived responsiveness of your GUI. – Hovercraft Full Of Eels Apr 10 '16 at 14:55
  • Your posted code is missing the Controler class. – Hovercraft Full Of Eels Apr 10 '16 at 15:01
  • I haven't written the controler yet,it's what I'm trying to do at the moment (reading the docs). Sorry for the slow answers but I'm running left and right and coding in between – zoisus Apr 10 '16 at 15:48
  • Thank you for the paintComponent tip,I'll change that asap – zoisus Apr 10 '16 at 15:51

0 Answers0