AffineTransform

Alright, I'm writing a game where you are a soldier that runs around shooting stuff. Pretty simple, right? The game's GUI is where you're looking down on top of the map(non-scrollable) that's made up of about 50 or so obstacles and blocks. What you're doing is you're pressingright to rotate right,left to rotate left, and up to move in the direction you're pointed. Well i've made my AffineTransform object and i've been successful in modifying my Image, in having it rotate in a single spot. But when i try totranslate that's where it gets tricky. It seems as though i can do either rotate or translate, but not both of them at once. Here's some sample code:

AffineTransform at = new AffineTransform();

public void run() {

while(1 == 1) {

if(rightPressed) {

angle = 10;

at.rotate(Math.toRadians(angle), soldier.width / 2, soldier.height / 2);

Graphics2D temp = (Graphics2D)getGraphics();

temp.transform(at);

}

if(leftPressed) {

angle = -10;

at.rotate(Math.toRadians(angle), soldier.width / 2, soldier.height / 2);

Graphics2D temp = (Graphics2D)getGraphics();

temp.transform(at);

}

if(upPressed) {

soldier.y -= 3;

at = AffineTransform.getTranslateInstance(soldier.x,soldier.y);

Graphics2D temp = (Graphics2D)getGraphics();

temp.transform(at);

}

repaint();

wait(25);

}

}

if anyone knows how to solve this please post back.

Message was edited by:

PaRlOaGn

[1565 byte] By [PaRlOaGna] at [2007-11-27 6:59:11]
# 1

import java.awt.*;

import java.awt.event.*;

import java.awt.geom.*;

import java.awt.image.BufferedImage;

import java.io.*;

import javax.imageio.ImageIO;

import javax.swing.*;

public class TransformAll extends JPanel {

BufferedImage image;

AffineTransform at;

double theta = 0;

Point2D.Double loc = new Point2D.Double();

int dx = 2;

int dy = 2;

public TransformAll(BufferedImage image) {

this.image = image;

registerKeys();

setFocusable(true);

}

protected void paintComponent(Graphics g) {

super.paintComponent(g);

Graphics2D g2 = (Graphics2D)g;

g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,

RenderingHints.VALUE_INTERPOLATION_BICUBIC);

if(at == null) {

int x = (getWidth() - image.getWidth())/2;

int y = (getHeight() - image.getHeight())/2;

loc.setLocation(x, y);

at = new AffineTransform();

setTransform();

}

g2.drawRenderedImage(image, at);

}

public void moveAhead() {

setTransform();

repaint();

}

private void setTransform() {

at.setToTranslation(loc.x, loc.y);

at.rotate(theta, image.getWidth()/2, image.getHeight()/2);

}

private void registerKeys() {

int c = JComponent.WHEN_IN_FOCUSED_WINDOW;

getInputMap(c).put(KeyStroke.getKeyStroke("LEFT"), "LEFT");

getActionMap().put("LEFT", left);

getInputMap(c).put(KeyStroke.getKeyStroke("RIGHT"), "RIGHT");

getActionMap().put("RIGHT", right);

getInputMap(c).put(KeyStroke.getKeyStroke("UP"), "UP");

getActionMap().put("UP", up);

}

public static void main(String[] args) throws IOException {

String path = "images/Bird.gif";

BufferedImage image = ImageIO.read(new File(path));

TransformAll test = new TransformAll(image);

new BirdDriver(test);

JFrame f = new JFrame("Click to start and stop");

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.getContentPane().add(test);;

f.setSize(400,400);

f.setLocation(200,200);

f.setVisible(true);

}

private Action left = new AbstractAction() {

public void actionPerformed(ActionEvent e) {

theta -= Math.toRadians(1);

}

};

private Action right = new AbstractAction() {

public void actionPerformed(ActionEvent e) {

theta += Math.toRadians(1);

}

};

private Action up = new AbstractAction() {

public void actionPerformed(ActionEvent e) {

double x = loc.x + dx*Math.cos(theta);

double y = loc.y + dy*Math.sin(theta);

loc.setLocation(x, y);

}

};

}

class BirdDriver implements Runnable {

TransformAll component;

Thread thread;

boolean running = false;

final int DELAY = 50;

public BirdDriver(TransformAll ta) {

component = ta;

component.addMouseListener(ml);

}

public void run() {

while(running) {

try {

Thread.sleep(DELAY);

} catch(InterruptedException e) {

stop();

}

component.moveAhead();

}

}

private void start() {

if(!running) {

running = true;

thread = new Thread(this);

thread.setPriority(Thread.NORM_PRIORITY);

thread.start();

}

}

private void stop() {

running = false;

if(thread != null)

thread.interrupt();

thread = null;

}

private MouseListener ml = new MouseAdapter() {

public void mousePressed(MouseEvent e) {

if(running)

stop();

else

start();

}

};

}

crwooda at 2007-7-12 18:49:45 > top of Java-index,Security,Cryptography...