Java swing 实现angrybird
来源:互联网 发布:管家婆软件多少钱 编辑:程序博客网 时间:2024/06/06 14:28
[java] view plain copy print?
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package AngryBirdsApplication;
- import java.awt.event.KeyEvent;
- import java.awt.event.MouseAdapter;
- import java.awt.event.MouseEvent;
- import java.awt.event.MouseMotionListener;
- import java.io.IOException;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import org.jbox2d.callbacks.ContactImpulse;
- import org.jbox2d.callbacks.ContactListener;
- import org.jbox2d.collision.Manifold;
- import org.jbox2d.common.Vec2;
- import org.jbox2d.dynamics.Fixture;
- import org.jbox2d.dynamics.contacts.Contact;
- /**
- *
- * @author Sumomoxiao
- */
- public class AngryBirdsController extends MouseAdapter implements Runnable, MouseMotionListener, ContactListener {
- private AngryBirdsArea m_stage;
- public AngryBirdsPanel m_view;
- private final AngryBirdsDraw drawer;
- // private final MusicController music;
- Thread gamethread;
- boolean stop = true;
- AngryBirdsController(AngryBirdsArea m, AngryBirdsPanel v){//, MusicController mc) {
- this.m_stage = m;
- m_view = v;
- drawer = v.getSDDraw();
- v.setStageController(this);
- m.initStage();
- //music = mc;
- drawer.setStage(m);
- this.addListener();
- }
- @Override
- public void run() {
- while (true) {
- try {
- while (!stop) {
- m_stage.update();
- drawer.drawStage();
- try {
- Thread.sleep(5);
- } catch (InterruptedException ex) {
- }
- }
- drawer.drawStage();
- Thread.sleep(12);
- } catch (InterruptedException ex) {
- Logger.getLogger(AngryBirdsController.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- }
- public void start() {
- if (gamethread == null) {
- stop = false;
- gamethread = new Thread(this);
- gamethread.start();
- }
- }
- public void pause() {
- if (isPainting()) {
- stop = true;
- }
- }
- public void resume() {
- if (!isPainting()) {
- stop = false;
- }
- }
- public boolean isPainting() {
- return m_view.isPainting();
- }
- /*
- * synchronized public void setRunthread(boolean runthread) {
- * if(this.stop!=runthread){ this.stop = runthread; }
- *
- *
- * }
- *
- */
- public void addListener() {
- m_view.addMouseListener(new MouseAdapter() {
- @Override
- public void mousePressed(MouseEvent e) {
- if (m_stage != null) {
- Vec2 pos = new Vec2(e.getX(), e.getY());
- if (e.getButton() == MouseEvent.BUTTON1) {
- drawer.getScreenToWorldToOut(pos, pos);
- m_stage.queueMouseDown(pos);
- }
- }
- }
- @Override
- public void mouseReleased(MouseEvent e) {
- if (m_stage != null) {
- Vec2 pos = new Vec2(e.getX(), e.getY());
- drawer.getScreenToWorldToOut(pos, pos);
- m_stage.queueMouseUp(pos);
- }
- }
- });
- m_view.addMouseMotionListener(new MouseMotionListener() {
- final Vec2 posDif = new Vec2();
- final Vec2 pos = new Vec2();
- final Vec2 pos2 = new Vec2();
- public void mouseDragged(MouseEvent e) {
- pos.set(e.getX(), e.getY());
- if (m_stage != null) {
- drawer.getScreenToWorldToOut(pos, pos);
- m_stage.queueMouseMove(pos);
- }
- }
- @Override
- public void mouseMoved(MouseEvent e) {
- pos2.set(e.getX(), e.getY());
- if (m_stage != null) {
- drawer.getScreenToWorldToOut(pos2, pos2);
- m_stage.queueMouseMove(pos2);
- }
- }
- });
- }
- @Override
- public synchronized void beginContact(Contact contact) {
- // drawer.drawContact();
- // System.out.print("listener thread !!!\n");
- }
- @Override
- public void endContact(Contact contact) {
- }
- @Override
- public void preSolve(Contact contact, Manifold oldManifold) {
- // throw new UnsupportedOperationException("Not supported yet.");
- }
- Fixture fix ;
- @Override
- public void postSolve(Contact contact, ContactImpulse impulse) {
- if (contact.m_fixtureA.m_filter.groupIndex == -1 || contact.m_fixtureB.m_filter.groupIndex == -1) {
- fix = contact.m_fixtureA.m_filter.groupIndex == -1?contact.m_fixtureA:contact.m_fixtureB;
- for (int i = 0; i < contact.getManifold().pointCount; i++) {
- if (impulse.normalImpulses[i] > 0.8) {
- System.out.print("pushing point \n");
- drawer.pushContactPoint(fix.m_body.getPosition());
- /*try {
- //music.birdScream();
- } catch (IOException ex) { }*/
- }
- }
- }
- if (contact.m_fixtureA.m_filter.groupIndex == 1 || contact.m_fixtureB.m_filter.groupIndex == 1) {
- // System.out.print(" impulse point count : "+impulse.normalImpulses.length+"\n");
- for (int i = 0; i < contact.getManifold().pointCount; i++) {
- //System.out.print("wood impulse : "+impulse.normalImpulses[i]+"\n");
- if (impulse.normalImpulses[i] > 3.1f) {
- //System.out.print("wood impulse : "+impulse.normalImpulses[i]+"\n");
- /*try {
- //music.woodCollision();
- } catch (IOException ex) {
- }*/
- return;
- }
- }
- }
- }
- }
[java] view plain copy print?
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package AngryBirdsCharacters;
- import javax.swing.ImageIcon;
- import org.jbox2d.collision.shapes.CircleShape;
- import org.jbox2d.collision.shapes.Shape;
- import org.jbox2d.common.Vec2;
- import org.jbox2d.dynamics.Body;
- import org.jbox2d.dynamics.BodyDef;
- import org.jbox2d.dynamics.FixtureDef;
- import org.jbox2d.dynamics.World;
- /**
- *
- * @author SONY
- */
- public class AngryBirdsModel extends AngryBirdsCharacter{
- public ImageIcon birds1=new ImageIcon("src/AngryBirdsImagePack/birds.png");
- public ImageIcon birds2=new ImageIcon();
- public ImageIcon birds3=new ImageIcon();
- public AngryBirdsModel() {
- super();
- }
- public Body createBirds(World mom,int birdtype,Vec2 pos) {
- this.getCharacterdef().position.set(pos);
- this.getCharacterdef().linearDamping=0.01f;
- this.getCharacterfixdef().filter.groupIndex=-1;
- this.charactershape=new CircleShape();
- Body bird=mom.createBody(characterdef);
- switch(birdtype) {
- case 1:
- this.charactershape.m_radius=0.5f;
- this.getCharacterfixdef().shape= this.charactershape;
- this.getCharacterinfo().setName("Lil Bird");
- this.getCharacterinfo().setHafheight(0.5f);
- this.getCharacterinfo().setHafwidth(0.5f);
- this.getCharacterinfo().setAppearance(birds1.getImage());
- bird.m_userData=this.getCharacterinfo();
- bird.createFixture(characterfixdef);
- break;
- case 2:
- this.charactershape.m_radius=0.7f;
- this.getCharacterfixdef().shape= this.charactershape;
- this.getCharacterinfo().setName("Strong Bird");
- this.getCharacterinfo().setHafheight(0.7f);
- this.getCharacterinfo().setHafwidth(0.7f);
- this.getCharacterinfo().setAppearance(birds2.getImage());
- bird.m_userData=this.getCharacterinfo();
- bird.createFixture(characterfixdef);
- break;
- case 3:
- this.charactershape.m_radius=1f;
- this.getCharacterfixdef().shape= this.charactershape;
- this.getCharacterinfo().setName("Angry Bird");
- this.getCharacterinfo().setHafheight(1f);
- this.getCharacterinfo().setHafwidth(1f);
- this.getCharacterinfo().setAppearance(birds3.getImage());
- bird.m_userData=this.getCharacterinfo();
- bird.createFixture(characterfixdef);
- break;
- }
- return bird;
- }
- }
阅读全文
0 0
- Java swing 实现angrybird
- Java Swing实现俄罗斯方块
- java Swing 实现记事本
- java swing实现Calculator
- JAVA Swing实现的clock
- java swing实现简单计算器
- Java Swing实现仿QQ
- Java使用swing实现调色板
- java swing model实现JComboBox
- Java swing 实现字体滚动
- Java swing 实现图片闪烁
- Java Swing:Nim游戏实现
- JAVA Swing实现感知机
- java swing实现日历显示
- AngryBird简单开发实现(二),zoom in/out的实现以及镜头跟随效果
- AngryBird简单开发实现(一),使用JBox2D-2.1.2简单开发的demo
- Java swing开发|Java实现窗体隐藏
- 利用Java Swing 实现游戏开发(转载)
- 对比线程安全和可重入函数
- Python图像处理库(2)
- 搜索总结
- 淘淘商城系列——全局异常处理
- 每天学一点Swift----面向对象下(五)
- Java swing 实现angrybird
- PAT (Advanced Level) Practise 1110 Complete Binary Tree (25)
- Sum of Different Primes——素数表+dp
- Java_log2000_框架概念介绍
- 前端之页面加速
- noauth authentication required redis
- cookie技术
- SpringMVC DispatcherServlet重写、自定义拦截器拦截器(二)
- 进程与线程的区别关系