1. 程式人生 > >Java 圖形影象程式設計 2

Java 圖形影象程式設計 2

新增 影象 處理:

src  資料夾下 有  2個:  package1包   和    img  ,  其中, img下有3幅影象。  可以執行。  複合影象。

package package1;

import java.awt.*;
import java.awt.event.*;

public class GameFrame {

    public GameFrame() {
        Frame app = new Frame("GameFrame");

        app.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        app.setLocation(100, 100);
        drawImage drawB = new drawImage();
        app.add(drawB, BorderLayout.CENTER);
     
        app.pack();
        app.setVisible(true);
        drawB.gameStart();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        new GameFrame();
    // TODO code application logic here
    }
}

package package1;


import java.awt.*;
import java.awt.image.*;
import java.io.IOException;
import java.util.*;
import javax.imageio.ImageIO;

/**
 *
 * @author Administrator
 */
public class drawImage extends Panel implements Runnable {

    private int width;
    private int height;
    private BufferedImage im;
    private BufferedImage bg1,  bg2,  bg3;
    private Graphics dbg;
    private Thread gamethread;
    private static final int FPS = 50;

    public drawImage() {
        bg1 = loadImage("/img/mountains.gif");
        bg2 = loadImage("/img/houses.gif");
        bg3 = loadImage("/img/trees.gif");

        width = 500;
        height = bg1.getHeight();
        setBackground(Color.pink);
        setPreferredSize(new Dimension(width, height));

    }

    public BufferedImage loadImage(String fnm) {
        BufferedImage bim = null;
        try {
            bim = ImageIO.read(getClass().getResource(fnm));
        } catch (IOException ex) {
            System.out.println("error load image");
        }
        return bim;

    }

    public void gameStart() {

        gamethread = new Thread(this);
        gamethread.start();
    }

    public void gamePaint() {
        Graphics g;
        try {
            g = this.getGraphics();
            if (g != null && im != null) {
                g.drawImage(im, 0, 0, null);
            }
            g.dispose();
        } catch (Exception e) {
        }
    }

    public void gameRender() {
        if (im == null) {
            im = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            if (im == null) {
                System.out.println("im is null");
            } else {
                dbg = im.getGraphics();
            }
        }

        dbg.drawImage(bg1, 0, 0, null);
        dbg.drawImage(bg2, 0, 0, null);
        dbg.drawImage(bg3, 0, 0, null);
    }

    public void gameUpdate() {

    }

    public void run() {
        long t1,t2,dt,sleepTime; 
        long period=1000/FPS; 
        t1=System.nanoTime();

        while (true) {
            gameUpdate();
            gameRender();
            gamePaint();
            t2= System.nanoTime() ;
            dt=(t2-t1)/1000000L;
            sleepTime = period - dt;
            if(sleepTime<=0)       
                  sleepTime=2;
            try {    
            Thread.sleep(sleepTime);
           } catch (InterruptedException ex) { }
              t1 = System.nanoTime(); 
        }
    }
}

===========================================================================================

移動遊戲的符合影象:

package package1;

import java.awt.*;
import java.awt.event.*;
import javax.swing.JFrame;

public class GameFrame {

    public GameFrame() {
        Frame app = new Frame("GameFrame");

        app.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        app.setLocation(100, 100);
        gamePanel drawB = new gamePanel();
        app.add(drawB, BorderLayout.CENTER);
     
        app.pack();
        app.setVisible(true);
        drawB.gameStart();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        new GameFrame();
    // TODO code application logic here
    }
}

package package1;

import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.*;
import java.io.IOException;
import java.util.*;
import javax.imageio.ImageIO;
import javax.swing.JPanel;

/**
 *
 * @author Administrator
 */
public class gamePanel extends Panel implements Runnable, KeyListener {

    public final static int WIDTH = 500;
    public final static int HEIGHT = 360;
    private BufferedImage im,im1,im2,im3;
    private Graphics dbg;
    private Thread gamethread;
    private static final int FPS = 50;
    private int imMoveSize;
    private ribbon bg1,  bg2,  bg3;

    public gamePanel() {

        imMoveSize = 5;
        im1=loadImage("/img/mountains.gif");
        im2=loadImage("/img/houses.gif");
        im3=loadImage("/img/trees.gif");
 
        bg1 = new ribbon((int) (imMoveSize * 0.2), im1);
        bg2 = new ribbon((int) (imMoveSize * 0.5), im2);
        bg3 = new ribbon(imMoveSize, im3);

        this.setFocusable(true);
        this.requestFocus();
        this.addKeyListener(this);
        setBackground(Color.pink);
        setPreferredSize(new Dimension(WIDTH,HEIGHT));

    }

    public void gameStart() {

        gamethread = new Thread(this);
        gamethread.start();
    }

    public void gamePaint() {
        Graphics g;
        try {
            g = this.getGraphics();
            if (g != null && im != null) {
                g.drawImage(im, 0, 0, this);
            }
            g.dispose();
        } catch (Exception e) {
        }
    }

    public void gameRender() {
        if (im == null) {
            im = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
            if (im == null) {
                System.out.println("im is null");
            } else {
                dbg = im.getGraphics();
            }
        }


        bg1.draw(dbg);
        bg2.draw(dbg);
        bg3.draw(dbg);
    }

    public void gameUpdate() {
        bg1.update();
        bg2.update();
        bg3.update();
    }

    public void run() {
        long t1,t2,dt,sleepTime; 
        long period=1000/FPS; 
        t1=System.nanoTime();

        while (true) {
            gameUpdate();
            gameRender();
            gamePaint();
            t2= System.nanoTime() ;
            dt=(t2-t1)/1000000L;
            sleepTime = period - dt;
            if(sleepTime<=0)       
                  sleepTime=2;
            try {    
            Thread.sleep(sleepTime);
           } catch (InterruptedException ex) { }
              t1 = System.nanoTime(); 
        }
    }
   
    public BufferedImage loadImage(String fnm) {
        try {
            BufferedImage im = ImageIO.read(getClass().getResource(fnm));
            return im;
        } catch (IOException e) {
            System.out.println("Load Image error for ");
            return null;
        }
    }

    public void keyTyped(KeyEvent e) {
    }

    public void keyPressed(KeyEvent e) {
        int keycode = e.getKeyCode();
        if (keycode == KeyEvent.VK_RIGHT) {
            bg1.canMoveLeft = true;
            bg2.canMoveLeft = true;
            bg3.canMoveLeft = true;
        }
        if (keycode == KeyEvent.VK_LEFT) {
            bg1.canMoveRight = true;
            bg2.canMoveRight = true;
            bg3.canMoveRight = true;
        }
    }

    public void keyReleased(KeyEvent e) {
        int keycode = e.getKeyCode();
        if (keycode == KeyEvent.VK_RIGHT) {
            bg1.canMoveLeft = false;
            bg2.canMoveLeft = false;
            bg3.canMoveLeft = false;
        }
        if (keycode == KeyEvent.VK_LEFT) {
            bg1.canMoveRight = false;
            bg2.canMoveRight = false;
            bg3.canMoveRight = false;
        }
    }
}

package package1;


import java.awt.*;
import java.awt.image.BufferedImage;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author Administrator
 */
public class ribbon {

 public BufferedImage bim;

 private int xImHead;

 private int pWidth;

 private int pHeight;

 private int width;

 private int moveSize;

 public boolean canMoveRight;

 public boolean canMoveLeft;

 public ribbon(int movS, BufferedImage im) {

  bim = im;
  xImHead = 0;
  pWidth = gamePanel.WIDTH;
  pHeight = gamePanel.HEIGHT;
  width = bim.getWidth();
  moveSize = movS;
  canMoveRight = false;
  canMoveLeft = false;
 }

 public void draw(Graphics g) {
  if (xImHead == 0) {
   drawRibbon(g, bim, 0, pWidth, 0, pWidth);
  }
  else if ((xImHead > 0) && (xImHead < pWidth)) {
   drawRibbon(g, bim, 0, xImHead, width - xImHead, width);
   drawRibbon(g, bim, xImHead, pWidth, 0, pWidth - xImHead);
  }
  else if (xImHead >= pWidth) {
   drawRibbon(g, bim, 0, pWidth, width - xImHead, width - xImHead + pWidth);
  }
  else if ((xImHead < 0) && (xImHead >= pWidth - width)) {
   drawRibbon(g, bim, 0, pWidth, -xImHead, pWidth - xImHead);
  }
  else if (xImHead < pWidth - width) {
   drawRibbon(g, bim, 0, width + xImHead, -xImHead, width);
   drawRibbon(g, bim, width + xImHead, pWidth, 0, pWidth - width - xImHead);
  }
 }

 private void drawRibbon(Graphics g, BufferedImage im, int dx1, int dx2,
   int sx1, int sx2) {
  g.drawImage(im, dx1, 0, dx2, pHeight, sx1, 0, sx2, pHeight, null);
 }

 public void update() {
  if (canMoveRight) {
   xImHead = (xImHead + moveSize) % width;
  } else if (canMoveLeft) {
   xImHead = (xImHead - moveSize) % width;
  }
 }
}