java graphics classes
I'm reasonably new at java and I'm having trouble getting a handle on how to use objects in some circumstances.
I am strugging with the Graphics and Graphics2D classes at the moment. I can display images and use the rotate and translate methods and stuff like that from within a paintComponent method without too much trouble but I would like to trigger a rotate with a command button.
I have an example of some code I hoped would work but did nothing. However it didn't produce an error message so I'm guessing I just don;t understand how the graphics objects work. Does anyone know anything about this?
/** Creates new form practiceFrame */
public practiceFrame() {
initComponents();
ButtonHandler handler = new ButtonHandler();
jButton1.addActionListener(handler);
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
private void initComponents() {
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new practiceFrame().setVisible(true);
}
});
}
private class ButtonHandler implements ActionListener{
public void actionPerformed(ActionEvent e){
practicePanel1.rotateMethod();
}
}
} //end practiceFrame class
public class practicePanel extends javax.swing.JPanel {
BufferedImage img = null;
private Graphics2D g2;
/** Creates new form puzzlePanel */
public practicePanel() {
initComponents();
System.out.println(this.getSize());
try {
img = ImageIO.read(new File("C:/Documents and Settings/User 1/My Documents/adam/stuff/practicePic.jpg"));
} catch (IOException e) {
}
}
public void setG2(Graphics2D g2) {
this.g2 = g2;
}
public Graphics2D getG2() {
return g2;
}
public void paintComponent(Graphics g){
setG2((Graphics2D) g);
getG2().drawImage(img,0,0,this);
}
public void rotateMethod(){
getG2().rotate(10.0 * Math.PI / 180.0);
repaint();
}

