creating a BufferedImage from a drawing
Hi,
I have a programme that draws lines. However these lines (on faster machines) are drawed all at once. I would like to create a BufferedImage that I can set a timer with so the process of painting can be reviewed.
I have read the sun documentation ( this and this) on how to create a BufferedImage but I can still not figure out how to do this for my programme.
Any help is greatly appreciated.
Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Success2 extends JPanel {
// the contents of the window
JButton eatMeButton, drinkmeButton;
Circle circ=new Circle();
ActionListener al = new myButtonListener(circ);
final static Dimension BUTTON_SIZE = new Dimension(125,75);
final static Dimension WINDOW_SIZE = new Dimension(1024,768);
final static Dimension CIRC_SIZE = new Dimension(900,900);
public Success2() {
setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
// sandwich the buttonBox between two layers of stretchy glue
Box buttonBox = Box.createHorizontalBox();
add(Box.createVerticalGlue());
add(buttonBox);
add(Box.createVerticalGlue());
eatMeButton = new JButton("m1=1500");
eatMeButton.addActionListener(al);
eatMeButton.setPreferredSize(BUTTON_SIZE);
drinkmeButton = new JButton("drink me");
drinkmeButton.setPreferredSize(BUTTON_SIZE);
circ.setPreferredSize(new Dimension(CIRC_SIZE.height, CIRC_SIZE.height));
buttonBox.add(Box.createHorizontalGlue());
buttonBox.add(circ);
buttonBox.add(Box.createHorizontalGlue());
buttonBox.add(eatMeButton);
buttonBox.add(Box.createHorizontalGlue());
buttonBox.add(drinkmeButton);
buttonBox.add(Box.createHorizontalGlue());
}
// the circle
static class Circle extends JPanel {
int m1=6000;
@Override
public void paintComponent(Graphics g) {
g.clearRect(0, 0, this.getWidth(), this.getHeight());
g.setColor(Color.red);
Graphics2D gg = (Graphics2D)g;
int m = 200000;
//int m1 = 6000;
int m2 = 8000;
int b = 900;
int h = 900;
double gamma = 6.67;
double r = 15;
double r2 = 20;
double vx = 0;
double vx2 = 0;
double alpha = 0;
double alpha2 = 0;
double alpha3 = 0;
double alpha4 = 0;
double x = r;
double x2 = r2;
double y = 0;
double y2 = 0;
double mittelpunktx = b/2;
double mittelpunkty = h/2;
double vy = 0.0001 * Math.sqrt(1000 * gamma * m / r);
double vy2 = 0.0001 * Math.sqrt(1000* gamma * m / r2);
double tend = 500;
double t = 0;
double pi = 4*Math.atan(1);
double deltat = 0.01;
double result = 0;
while (t <= tend) {
t = t + deltat;
r = Math.sqrt(x * x + y * y);
r2 = Math.sqrt(x2 * x2 + y2 * y2);
double posx = mittelpunktx + x;
double posy = mittelpunkty + y;
double posx2 = mittelpunktx + x2;
double posy2 = mittelpunkty + y2;
double poswechselx = posx2 - posx;
double poswechsely = posy2 - posy;
double rwechselq = poswechselx * poswechselx + poswechsely * poswechsely;
double a = 0.00001 * gamma * ((m / r) / r);
double a2 = 0.00001 * gamma * ((m / r2) / r2);
double avon1auf2 = 0.00001 * gamma * (m2 / rwechselq);
double avon2auf1 = 0.00001 * gamma * (m1 / rwechselq);
alpha = winkel(x,y,alpha);
alpha2 = winkel(x2, y2, alpha2);
alpha3 = winkel(poswechselx, poswechsely, alpha3);
alpha4 = alpha3 + pi;
double ax = a * Math.cos(alpha);
double ay = a * Math.sin(alpha);
double ax2 = a2 * Math.cos(alpha2);
double ay2 = a2 * Math.sin(alpha2);
double ax3 = avon1auf2 * Math.cos(alpha3);
double ay3 = avon1auf2 * Math.sin(alpha3);
double ax4 = avon2auf1 * Math.cos(alpha4);
double ay4 = avon2auf1 * Math.sin(alpha4);
ax = ax + ax3;
ay = ay + ay3;
ax2 = ax2 + ax4;
ay2 = ay2 + ay4;
vx = vx - ax * deltat;
vy = vy - ay * deltat;
x = x + vx * deltat;
y = y + vy * deltat;
vx2 = vx2 - ax2 * deltat;
vy2 = vy2 - ay2 * deltat;
x2 = x2 + vx2 * deltat;
y2 = y2 + vy2 * deltat;
double finalx = skalierenx(posx);
double finaly = skaliereny(posy);
double finalx2 = skalierenx2(posx2);
double finaly2 = skaliereny2(posy2);
double finalmittelpunktx = skalierenx(mittelpunktx);
double finalmittelpunkty = skaliereny(mittelpunkty);
g.setColor(Color.blue);
g.drawLine((int)(finalx),(int)(finaly),(int)(finalx),(int)(finaly));
g.setColor(Color.red);
g.drawLine((int)(finalx2),(int)(finaly2),(int)(finalx2),(int)(finaly2));
g.drawLine((int)(finalmittelpunktx),(int)(finalmittelpunkty),(int)(finalmittelpunktx),(int)(finalmittelpunkty));
// g.drawOval((int)(finalx),(int)(finaly),);
}
}
public double winkel(double xposition, double yposition, double result){
result = Math.atan(yposition / xposition);
double pi = 4*Math.atan(1);
if ((xposition < 0) && (yposition < 0)) {
result = result + pi;
}
if ((xposition < 0 ) && (yposition > 0)){
result = result + pi;
}
if ((xposition > 0) && (yposition <0)) {
result = result + 2*pi;
}
return result;
}
public double skalierenx(double posx){
double minPointX = 508.0;
double maxPointX = 366.594;
double screenX = (posx - minPointX) * getWidth() / (maxPointX - minPointX);
return screenX;
}
public double skaliereny(double posy){
double minPointY = 497.66;
double maxPointY = 337.468;
double screenY = (posy - minPointY) * getWidth() / (maxPointY - minPointY);
return screenY;
}
public double skalierenx2(double posx2){
double minPointX2 = 508.0;
double maxPointX2 = 366.594;
double screenX2 = (posx2 - minPointX2) * getWidth() / (maxPointX2 - minPointX2);
return screenX2;
}
public double skaliereny2(double posy2){
double minPointY2 = 497.66;
double maxPointY2 = 337.468;
double screenY2 = (posy2 - minPointY2) * getWidth() / (maxPointY2 - minPointY2);
return screenY2;
}
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("Success2");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JComponent newContentPane = new Success2();
frame.setContentPane(newContentPane);
frame.setPreferredSize(WINDOW_SIZE);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
class myButtonListener implements ActionListener{
Circle c;
myButtonListener(Circle c){
this.c=c;
}
public void actionPerformed(ActionEvent ae){
c.m1 = 8000;
c.repaint();
}
}
}
Thank you
chappa