7 Sept 2012

How to move the display in java


package Game;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferStrategy;

@SuppressWarnings("serial")
public abstract class GameCanvas extends Canvas implements Runnable,KeyListener{
 public static final long ONE_SECOND_MILI = 1000;
 protected int frameW;
 protected int frameH;
 protected long fps;
 private long period = 15;
 private BufferStrategy buff;
 private Graphics graph;
 private Color bckGround=(Color.GRAY);
 private Image bckGround_img;
 private Thread t;
 boolean left;
 boolean right;
 boolean up;
 boolean down;
 int lastpressed;
 int newlastpressed;

 private String drawFps = "0";
    public GameCanvas(int w,int h){
        this.frameW=w;
        this.frameH=h;
        this.setIgnoreRepaint(true);
        this.setBounds(0,0,frameW,frameH);
        this.setBackground(Color.GREEN);
        this.setVisible(true);
    }

    public GameCanvas(int w,int h,Color bck){
        this.frameW=w;
        this.frameH=h;
        this.bckGround=bck;
        this.setIgnoreRepaint(true);
        this.setBounds(0,0,frameW,frameH);
        this.setBackground(bckGround);
        this.setVisible(true);
    }

    public void addNotify(){
        super.addNotify();
        this.createBufferStrategy(2);
        this.buff=this.getBufferStrategy();
        requestFocus();
        startGame();
    }
    public void startGame(){
        if (t==null){
            t=new Thread(this);
            t.start();
        }
    }
    public void run(){
        while(true){
            long beginTime=System.currentTimeMillis();
//          try {
//              Thread.sleep(25);
//          } catch (InterruptedException e1) {
//              // TODO Auto-generated catch block
//              e1.printStackTrace();
//          }
            Update();
            Render();
            Draw();
            fps=System.currentTimeMillis() - beginTime ;
            long sleepTime=period-fps;
            if (sleepTime == 30) sleepTime = -1;
            fps= ONE_SECOND_MILI / ((period * 2) - sleepTime);
            try{
                if (sleepTime > 0){
                    Thread.sleep(sleepTime);

                }
            }
            catch(Exception e){
            }
        }
    }
    public void Render(){
        graph = buff.getDrawGraphics();

        if (!HasImgBackground()){
            graph.setColor(bckGround);
            graph.fillRect(0, 0, frameW, frameH);
        }else{
            graph.drawImage(bckGround_img, 0, 0, frameW, frameH,null);
        }
        graph.setColor(new Color(255,255,255));
        graph.drawString("FPS: " + fps , 10, 15);
        Paint(graph);
    }
    private void Draw(){
        if(!buff.contentsLost()){
            buff.show();
            if(graph != null){
                graph.dispose();
            }
        }
    }
    private boolean HasImgBackground(){
        if (bckGround_img==null){
            return false;
        }
        return true;
    }
    public void setBackgroundImg(Image image){
        this.bckGround_img=image;
    }
    public void deleteBackground(){
        this.bckGround_img=null;
    }
    public void keyPressed(KeyEvent e) {
        if(e.getKeyCode()==KeyEvent.VK_LEFT){
            left=true;
            if(lastpressed!=4)
                newlastpressed=lastpressed;
            lastpressed=4;
        }
        if(e.getKeyCode()==KeyEvent.VK_UP){
            up=true;
            if(lastpressed!=1)
                newlastpressed=lastpressed;
            lastpressed=1;
        }
        if(e.getKeyCode()==KeyEvent.VK_RIGHT){
            right=true;
            if(lastpressed!=2)
                newlastpressed=lastpressed;
            lastpressed=2;
        }
        if(e.getKeyCode()==KeyEvent.VK_DOWN){
            down=true;
            if(lastpressed!=3)
                newlastpressed=lastpressed;
            lastpressed=3;
        }

    }
    public void keyReleased(KeyEvent e) {
        if(e.getKeyCode()==KeyEvent.VK_LEFT){
            left=false;
            if(up||right||down)
                if(newlastpressed!=0)
                lastpressed=newlastpressed;
        }
        if(e.getKeyCode()==KeyEvent.VK_UP){
            up=false;
            if(left||right||down)
                if(newlastpressed!=0)
                lastpressed=newlastpressed;
        }
        if(e.getKeyCode()==KeyEvent.VK_RIGHT){
            right=false;
            if(up||left||down)
                if(newlastpressed!=0)
                lastpressed=newlastpressed;
        }
        if(e.getKeyCode()==KeyEvent.VK_DOWN){
            down=false;
            if(up||right||left)
                if(newlastpressed!=0)
                lastpressed=newlastpressed;
        }

    }
    public void keyTyped(KeyEvent e) {
    }
    abstract void Update();
    abstract void Paint(Graphics g);
}


package Game;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import javax.swing.JFrame;


public class Mainth extends GameCanvas{

    private long timeDown = 0;
//  private StopWatch t = new StopWatch();
    static Collision coll=new Collision();
    Character character=new Character();
    static Image wallImage=ImageLoader.getImg().getImage("data/images/objects/brick_wall.png");
    static Image bushImage=ImageLoader.getImg().getImage("data/images/objects/hedge_ver.png");
    static ArrayList<Image> wallArray=new ArrayList<Image>();
    static ArrayList<Image> wall2Array=new ArrayList<Image>();
    static Sprite wallSprite;
    static Sprite wall2Sprite;
    static IndexCounter devIndex;
    static Dude dev;
    static Wall wall;
    static Wall bush;
    static Wall wall2;
    /**not used*/
    int x=0,y=0;
    static ArrayList objects=new ArrayList<Entity>();
    static ArrayList chars=new ArrayList<Dude>();
    static ArrayList projectiles=new ArrayList<>();

    public static void main(String[] args){
        Mainth Canvas=new Mainth(1500,1000);
        JFrame frame=new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);
        frame.add(Canvas);
        frame.pack();
        frame.setSize(750, 400);
        frame.setVisible(true);
        Mainth.chars.add(dev);
        Mainth.objects.add(wall);
        Mainth.objects.add(bush);
        Mainth.objects.add(wall2);
    }
    public Mainth(int w,int h){
        super(w,h);
        this.addKeyListener(this);
        CharacterCreation();
    }
    public void CharacterCreation(){
        wallArray.add(wallImage);
        wall2Array.add(bushImage);
        wallSprite=new Sprite(wallArray,1);
        wall2Sprite=new Sprite(wall2Array,1);
        dev=new Dude(character.LinkStandingDown,character.LinkStandingDown.getID(),100,300);
        devIndex=new IndexCounter(character.LinkWalkingDownArray.size(),3);
        wall=new Wall(wallSprite,1,100,50){};
        bush=new Wall(wall2Sprite,1,185,55){};
        wall2=new Wall(wallSprite,1,100,225){};
        bush.setHardness(1);
    }
    Movement movem=new Movement();
    void Update() {
        movem.movement(dev, character, left, right, up, down, lastpressed, devIndex);
        dev.move();
        coll.objectCollision(chars,objects);
        devIndex.Counter();
    }
    void Paint(Graphics g) {
        bush.Draw(g,0);
        wall.Draw(g,0);
        wall2.Draw(g,0);
        dev.Draw(g,devIndex.getIndex());
        Graphics2D g2d= (Graphics2D) g;
    }
    public void animation(){
        String[] animationSprites=dev.getImgs("walk_down");
        int aniTime=0;
        aniTime++;
    }
    public ArrayList<Entity> exportObjects(){
        return objects;
    }
    public ArrayList<Dude> getMainChar(){
        return chars;
    }
}

void renderFrame(Rectangle frame) {
    for(GameObject go : gameObjects) {
        if(frame.contains(go.getGlobalCoordinates())) {
            Rectangle windowCoordinates = new Rectangle();
            windowCoordinates.x = go.getGlobalCoordinates().x - frame.x;
            windowCoordinates.x = go.getGlobalCoordinates().y - frame.y;
            windowCoordinates.x = go.getGlobalCoordinates().width - frame.width;
            windowCoordinates.x = go.getGlobalCoordinates().height - frame.height;

            go.paint(g2, windowCoordinates);
        }
    }
}

0 comments:

Post a Comment