📄 graphicspanel.java#1.21
字号:
/* * GraphicsPanel.java * * Created on 2007年12月18日, 下午12:27 * * To change this template, choose Tools | Template Manager * and open the template in the editor. */package SBGameCore;import graphics.Bird;import graphics.Feather;import java.awt.Cursor;import java.awt.Font;import java.awt.MediaTracker;import java.awt.Point;import java.awt.Rectangle;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import java.util.Vector;import javax.swing.JFrame;import javax.swing.JPanel;import java.awt.Color;import java.awt.Dimension;import java.awt.Graphics;import java.awt.Graphics2D;import java.awt.Image;import java.awt.Toolkit;import javax.swing.JPanel;import sound.BGmusic;import sound.Gun;/** * * @author wusir */public class GraphicsPanel extends JPanel implements Runnable,MouseListener{ public Toolkit toolkit = null; public Image buffer = null ; // image buffer public Image backdrop = null ; // background public Image bgimg = null ; // privouse background public Image[] birds = new Image[7];//birds images public Image[] deadbirds = new Image[7];//dead birds image public Image cursor = null; //cursor public Graphics buf_g = null ; // graphics buffer public Graphics bkd_g = null ; // background graphics public Dimension window_size = null;//windows size public Font font ; public Font font_s ; //word fonts MediaTracker tracker = null ; public Thread game = null; //game thread public boolean game_over = false; public Point mouse = null; //mouse position public Rectangle paint_area = new Rectangle() ; //object's current area public Rectangle new_area = new Rectangle() ; //object's new area public Vector Vbird = new Vector(); //bird vector public int Nbird = 7; //count of bird private Point clickPoint=null; private Gun gunSound = null; private BGmusic bgSound = null; public int score = 0; public Graphics g2d; public GraphicsPanel(){ this.setSize(1000,700); init(); } public void init(){ tracker = new MediaTracker(this) ; toolkit = Toolkit.getDefaultToolkit(); bgimg = toolkit.getImage("image/123.JPG"); tracker.addImage(bgimg,0) ; for(int i=0;i<7;i++){ birds[i] = toolkit.getImage("image/"+i+".gif"); tracker.addImage(birds[i],0) ; } for(int i=0;i<7;i++){ deadbirds[i] = toolkit.getImage("image/d"+i+".gif"); tracker.addImage(deadbirds[i],0) ; } cursor = toolkit.getImage("image/aim.jpg"); tracker.addImage(cursor,0) ; try { tracker.waitForAll(); } catch (InterruptedException ex) { ex.printStackTrace(); } gunSound = new Gun(); bgSound = new BGmusic(); clickPoint = new Point(); font = new Font("Helvetica", Font.BOLD, 24) ; font_s = new Font("Helvetica", Font.BOLD, 14) ; addMouseListener(this); } public void Start(){ // setCursor(toolkit.createCustomCursor(cursor,new Point(0,0),"sight")) ; //set the cursor here window_size=this.size();//get the panel size buffer = null; buffer = createImage(window_size.width, window_size.height); backdrop = null; backdrop = createImage(window_size.width, window_size.height); buf_g = buffer.getGraphics(); buf_g.fillRect(0, 0, window_size.width, window_size.height); game_over = true ; bgSound.loop(); } public void paintComponent(Graphics g) { //draw the panel with the graphics from buffer super.paintComponent(g); g2d = g; g2d.drawImage(buffer,0,0,this); } public int stop() { if(game!=null){ game.stop(); game = null; } setCursor(Cursor.getDefaultCursor()) ; return score; } public void run() { Bird bird; Feather feather; Point point; long time = 0; long Time = 0; long count = 0; buf_g = backdrop.getGraphics(); buf_g.drawImage(bgimg,0,0,window_size.width,window_size.height,this) ; buf_g = getGraphics(); buf_g.drawImage(backdrop,0,0,this) ; buf_g = buffer.getGraphics(); buf_g.drawImage(bgimg,0,0,window_size.width,window_size.height,this) ; repaint() ; display_score() ; Time = System.currentTimeMillis() ; while(true){ time = System.currentTimeMillis() ; if ((Vbird.size() < Nbird)&&(System.currentTimeMillis()-Time>1000)){//&& (Math.random() > (Vbird.size() == 0 ? 0.90 : 0.98))) { bird = new Bird(this); point = bird.getPoint(); //if (score > 10 && Math.random() > 0.7) ;速度控制 Vbird.add(bird); Time = System.currentTimeMillis() ; } for(int i=0;i<Vbird.size();i++){ bird = (Bird)Vbird.elementAt(i); bird.move(); } for (int i=(Vbird.size()-1); i >=0 ; --i) { bird = (Bird) Vbird.elementAt(i); if(clickPoint.x!=-500&&(clickPoint.x>(bird.getPoint().x-50))&&(clickPoint.x<(bird.getPoint().x+50))&&(clickPoint.y<(bird.getPoint().y+50))&&(clickPoint.y>(bird.getPoint().y-50))){ score += bird.getScore(); display_score(); bird.changeToDeadImage(); bird.active(false); } if((bird.getFace()==-1&&bird.getPoint().x<=-100)||(bird.getFace()==1&&bird.getPoint().x>=1100)){ bird.reset(); } bird.draw(); } clickPoint.x = -500; time = System.currentTimeMillis() - time ; time = 20 - time; time = time > 0 ? 10 + time : 10 ; Thread.yield(); try { Thread.sleep(time); } catch (InterruptedException ex) {} if((count = ++count % 100)==0) repaint(); } } public void display_score() { Graphics bkd_g = backdrop.getGraphics(); bkd_g.clipRect(window_size.width/2, 0, window_size.width/2, 40); bkd_g.drawImage(bgimg,0,0,window_size.width,window_size.height,this) ; bkd_g.setColor(Color.red) ; bkd_g.setFont(font) ; String aux = score > 9 ? "" : "0" ; bkd_g.drawString(aux+score, window_size.width - 60,30) ; bkd_g.dispose() ; Graphics bg = buffer.getGraphics() ; bg.clipRect(0, 0, window_size.width, 40); bg.drawImage(backdrop,0,0,this) ; bg.dispose() ; Graphics g = getGraphics() ; g.clipRect(0, 0, window_size.width, 40); g.drawImage(buffer,0,0,this) ; g.dispose() ; } public void mouseClicked(MouseEvent e) { } public void mousePressed(MouseEvent e) { clickPoint.x = e.getX();//+25; clickPoint.y = 650-e.getY();//+25; // System.out.println("clicked: "+clickPoint.x+","+clickPoint.y); gunSound.play(); } public void mouseReleased(MouseEvent e) { } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -