Java-Gaming.org    
Featured games (78)
games approved by the League of Dukes
Games in Showcase (427)
Games in Android Showcase (89)
games submitted by our members
Games in WIP (466)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
  JavaGaming.org - Pastebin



Author: v0rtex (posted 2012-10-06 06:18:31, viewed 139 times)

1   2   3   4   5   6   7   8   9   10   11   12   13   14   15   16   17   18   19   20   21   22   23   24   25   26   27   28   29   30   31   32   33   34   35   36   37   38   39   40   41   42   43   44   45   46   47   48   49   50   51   52   53   54   55   56   57   58   59   60   61   62   63   64   65   66   67   68   69   70   71   72   73   74   75   76   77   78   79   80   81   82   83   84   85   86   87   88   89   90   91   92   93   94   95   96   97   98   99   100   101   102   103   104   105   106   107   108   109   110   111   112   113   114   115   116   117   118   119   120   121   122   123   124   125   126   127   128   129   130   131   132   133   134   135   136   137   138   139  
import java.awt.*;
import javax.swing.*;
import java.util.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.geom.AffineTransform;
import java.awt.image.*;
import java.net.*;
public class MainFrame extends JFrame implements Runnable, KeyListener {
static int ScreenWidth = 640;
static int ScreenHeight = 480;
AffineTransform transform = new AffineTransform();
Thread gameloop;
Random rand = new Random();

//double buffer objects
BufferedImage backbuffer;
Graphics2D g2d;
   
//sprite variables
AnimatedSprite sprite;

public MainFrame() {
   super("Animation Demo");
   setSize(ScreenWidth, ScreenHeight);
   setVisible(true);
   setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   
   //create the backbuffer to remove flicker
   backbuffer = new BufferedImage(ScreenWidth, ScreenHeight, BufferedImage.TYPE_INT_RGB);
   g2d=  backbuffer.createGraphics();
   
   //load explosion animation
   sprite = new AnimatedSprite(this,g2d);
   sprite.load("players.png", 4, 8, 48, 48); //4 columns, 8 total frames, each image is 48x48
   
   sprite.setX(300);
   sprite.setY(200);
   sprite.setFrameDelay(60);
   sprite.setTotalFrames(8);
   sprite.setGraphicsObject(g2d);
   //sprite.velX = 1.0;
   //sprite.velY = 1.0;
   addKeyListener(this);
   gameloop = new Thread(this);
   gameloop.start();
}



public void run() {
    Thread t = Thread.currentThread();
    while (t == gameloop) {
        try {
            Thread.sleep(5);
        }
        catch (InterruptedException e) {
            e.printStackTrace();
        }
        gameUpdate();
    }
}
int faceAngle = 0; //test debug

public void gameUpdate() {
   
   //draw background
   g2d.setColor(Color.WHITE);
   g2d.fill(new Rectangle(0,0,ScreenWidth-1,ScreenHeight-1));
//sprite.transform();
sprite.draw();
sprite.drawBounds(Color.RED);

    if (sprite.x < 0 || sprite.x > ScreenWidth - 48) 
        sprite.velX *= -1;
    if (sprite.y < 0 || sprite.y > ScreenHeight - 48) 
        sprite.velY *= -1;


    g2d.setColor(Color.BLACK);
    g2d.drawString("Position: " + sprite.x + "," +
                   sprite.y, 10, 40);
    g2d.drawString("Velocity: " + sprite.x + "," +
                   sprite.y, 10, 60);
    g2d.drawString("Animation: " + sprite.getCurrentFrame(), 10, 80);

    g2d.drawString("Face Angle: " + sprite.getFaceAngle(), 10, 100);
     
    repaint();
}

public void paint(Graphics g) {
    //draw the back buffer to the screen
    g.drawImage(backbuffer, 0, 0, this);
    //sprite.draw();
   // sprite.drawBounds(Color.RED);
}



public static void main(String[] args) {
   new MainFrame();
}

@Override
public void keyPressed(KeyEvent e) {

int keyCode = e.getKeyCode();
     switch (keyCode) {

      case KeyEvent.VK_LEFT:
          //left arrow rotates ship left 5 degrees
          sprite.setFaceAngle((sprite.getFaceAngle()-5));
          if (sprite.getFaceAngle() < 0) sprite.setFaceAngle(360-5);
          break;
     
     
case KeyEvent.VK_RIGHT:
    //left arrow rotates ship left 5 degrees
    sprite.setFaceAngle((sprite.getFaceAngle()+5));
    if (sprite.getFaceAngle() > 360) sprite.setFaceAngle(0+5);
    break;

}

}
@Override
public void keyReleased(KeyEvent arg0) {
   // TODO Auto-generated method stub
   
}

@Override
public void keyTyped(KeyEvent arg0) {
   // TODO Auto-generated method stub
   
}
   
}





Dump your java code here :



Special syntax:
  • To highlight a line (yellow background), prefix it with '@@'
  • To indicate that a line should be removed (red background), prefix it with '-'
  • To indicate that a line should be added (green background), prefix it with '+'
  • To post multiple snippets, seperate them by '~~~~'
  EOF
 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (76 views)
2014-04-15 18:08:23

BurntPizza (69 views)
2014-04-15 03:46:01

UprightPath (81 views)
2014-04-14 17:39:50

UprightPath (66 views)
2014-04-14 17:35:47

Porlus (82 views)
2014-04-14 15:48:38

tom_mai78101 (106 views)
2014-04-10 04:04:31

BurntPizza (165 views)
2014-04-08 23:06:04

tom_mai78101 (261 views)
2014-04-05 13:34:39

trollwarrior1 (211 views)
2014-04-04 12:06:45

CJLetsGame (221 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!