/*
* RotatingRectangleDemo.java
* Kleiner vereinfachter Auszug aus dem Einsteiger Tutorial von Beni und Roar
* (siehe FAQ im java-forum.org)
*/
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
public class RotatingRectangleDemo extends JFrame {
private JButton btCenter;
private JButton btColor;
private JButton btHeight;
private JButton btRotation;
private JButton btWidth;
private JToolBar jToolBar1;
private RotatingRectangle rotatingRectangle;
private Random r = new Random();
private JComponent p;
public RotatingRectangleDemo() {
super("RotatingRectangleDemo");
initComponents();
rotatingRectangle = new RotatingRectangle();
rotatingRectangle.setColor(new Color(r.nextInt(256), r.nextInt(256), r.nextInt(256)));
rotatingRectangle.setCenterX(r.nextInt(400));
rotatingRectangle.setCenterY(r.nextInt(300));
rotatingRectangle.setWidth(r.nextInt(150));
rotatingRectangle.setHeight(r.nextInt(100));
rotatingRectangle.setRotation(r.nextDouble());
p = new JComponent() {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
rotatingRectangle.paint(g);
}
};
add(p);
}
private void initComponents() {
jToolBar1 = new JToolBar();
btColor = new JButton("Color");
btCenter = new JButton("Center");
btWidth = new JButton("Width");
btHeight = new JButton("Height");
btRotation = new JButton("Rotation");
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
btColor.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
rotatingRectangle.setColor(new Color(r.nextInt(256), r.nextInt(256), r.nextInt(256)));
p.repaint();
}
});
jToolBar1.add(btColor);
btCenter.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
rotatingRectangle.setCenterX(r.nextInt(400));
rotatingRectangle.setCenterY(r.nextInt(300));
p.repaint();
}
});
jToolBar1.add(btCenter);
btWidth.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
rotatingRectangle.setWidth(r.nextInt(150));
p.repaint();
}
});
jToolBar1.add(btWidth);
btHeight.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
rotatingRectangle.setHeight(r.nextInt(100));
p.repaint();
}
});
jToolBar1.add(btHeight);
btRotation.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
rotatingRectangle.setRotation(r.nextDouble());
p.repaint();
}
});
jToolBar1.add(btRotation);
getContentPane().add(jToolBar1, java.awt.BorderLayout.PAGE_START);
setSize(400, 300);
setLocationRelativeTo(null);
}
public static void main(final String[] args) {
Runnable gui = new Runnable() {
public void run() {
new RotatingRectangleDemo().setVisible(true);
}
};
//GUI must start on EventDispatchThread:
SwingUtilities.invokeLater(gui);
}
}
/**
* Diese Figure ist ein gefuelltes Rechteck, welches um ein Zentrum
* gedreht ist.
*/
class RotatingRectangle {
/** Die Farbe der Figure */
private Color color;
/** Zentrum der Figur */
private int centerX, centerY;
/** Breite und Hoehe der Figure */
private int width, height;
/** Der Winkel, um den Rotiert wird */
private double rotation;
/**
* Standardkonstruktor
*/
public RotatingRectangle() {
}
/**
* Standardkonstruktor
* @param color Die Farbe der Figur
*/
public RotatingRectangle(Color color) {
setColor(color);
}
public void paint(Graphics g) {
g.setColor(color);
// Die Rotation von Punkten kann man einfach mit
// Sinus und Cosinus beschreiben
int[] x = new int[4];
int[] y = new int[4];
double sin = Math.sin(rotation);
double cos = Math.cos(rotation);
x[0] = (int) (centerX + cos * width / 2.0 - sin * height / 2.0);
y[0] = (int) (centerY + sin * width / 2.0 + cos * height / 2.0);
x[1] = (int) (centerX + cos * width / 2.0 + sin * height / 2.0);
y[1] = (int) (centerY + sin * width / 2.0 - cos * height / 2.0);
x[2] = (int) (centerX - cos * width / 2.0 + sin * height / 2.0);
y[2] = (int) (centerY - sin * width / 2.0 - cos * height / 2.0);
x[3] = (int) (centerX - cos * width / 2.0 - sin * height / 2.0);
y[3] = (int) (centerY - sin * width / 2.0 + cos * height / 2.0);
g.fillPolygon(x, y, 4);
}
public Color getColor() {
return color;
}
public void setColor(Color color) {
this.color = color;
}
public int getCenterX() {
return centerX;
}
public void setCenterX(int centerX) {
this.centerX = centerX;
}
public int getCenterY() {
return centerY;
}
public void setCenterY(int centerY) {
this.centerY = centerY;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public double getRotation() {
return rotation;
}
public void setRotation(double rotation) {
this.rotation = rotation;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
}