Grpahics & Transformations
Firstly i'm sorry as i'm asking a load of very basic questions today. Been over 2 years since i touched java and can't remeber a thing! (probably not a great idea to do a java graphics course at college eh!) Anyways, hope some of you can help with these...
1. How do i rotate an object from the centre? I can rotate (as well as any other shape i make) from the origin point but thats not much help when i'm trying to create the impression of a wheel spinning! All i want is a circle with a cross through it spinning on its central point!
2. As i'm trying to program a very basic game can i create the objects/shapes I want in seperate classes and then call then and draw them in the main method? I'm pretty sure you can but just can't work out the right way to do it. I guess i'll need to pass co-ordinates into the class as well when i call it to make the object appear where i want it too on the screen.
3. Lastly (for now anyway) how do i get keyboard interaction to work? I've read some stuff about key listeners (or similar...i'm just confused now) but can't quite work out how to implement it. What i plan on doing is having a rectangle and allowing the user to press the left and right cursor keys to move it left and right.
Any help and suggestions would be greatly appreciated! I know this sounds like im asking for all the answers without working on it myself but i have two weeks to get something working and have spent 5 hours today and all i can get is basic shapes and not much more!
Cheers!Chris
[1558 byte] By [
Mr.Ca] at [2007-11-26 19:32:28]

# 2
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.AffineTransform;
import javax.swing.*;
public class Refresher extends JPanel {
Shape shape;
AffineTransform at;
int dx = 2;
int dy = 2;
public Refresher() {
shape = new Rectangle(100, 100, 175, 70);
at = new AffineTransform();
registerKeys();
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setPaint(Color.red);
g2.draw(shape);
}
private void registerKeys() {
getInputMap().put(KeyStroke.getKeyStroke("UP"), "UP");
getActionMap().put("UP", upAction);
getInputMap().put(KeyStroke.getKeyStroke("LEFT"), "LEFT");
getActionMap().put("LEFT", leftAction);
getInputMap().put(KeyStroke.getKeyStroke("DOWN"), "DOWN");
getActionMap().put("DOWN", downAction);
getInputMap().put(KeyStroke.getKeyStroke("RIGHT"), "RIGHT");
getActionMap().put("RIGHT", rightAction);
getInputMap().put(KeyStroke.getKeyStroke("X"), "SPIN");
getActionMap().put("SPIN", spinAction);
}
public static void main(String[] args) {
Refresher test = new Refresher();
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(test);
f.setSize(400,400);
f.setLocation(200,200);
f.setVisible(true);
}
Action upAction = new AbstractAction() {
public void actionPerformed(ActionEvent e) {
Rectangle r = shape.getBounds();
at.setToTranslation(0, -dy);
shape = at.createTransformedShape(shape);
repaint();
}
};
Action leftAction = new AbstractAction() {
public void actionPerformed(ActionEvent e) {
Rectangle r = shape.getBounds();
at.setToTranslation(-dx, 0);
shape = at.createTransformedShape(shape);
repaint();
}
};
Action downAction = new AbstractAction() {
public void actionPerformed(ActionEvent e) {
Rectangle r = shape.getBounds();
at.setToTranslation(0, dy);
shape = at.createTransformedShape(shape);
repaint();
}
};
Action rightAction = new AbstractAction() {
public void actionPerformed(ActionEvent e) {
Rectangle r = shape.getBounds();
at.setToTranslation(dx, 0);
shape = at.createTransformedShape(shape);
repaint();
}
};
Action spinAction = new AbstractAction() {
public void actionPerformed(ActionEvent e) {
Rectangle r = shape.getBounds();
double cx = r.getCenterX();
double cy = r.getCenterY();
at.setToRotation(Math.PI/2, cx, cy);
shape = at.createTransformedShape(shape);
repaint();
}
};
}