Results 1 to 3 of 3
- 01-09-2010, 02:34 PM #1
Member
- Join Date
- Dec 2009
- Posts
- 19
- Rep Power
- 0
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.
Java 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
- 01-09-2010, 09:26 PM #2
Java Code:import java.awt.*; import java.awt.event.*; import java.awt.image.BufferedImage; import javax.swing.*; public class Success2Rx implements Runnable { CirclePanel circlePanel = new CirclePanel(); int width = circlePanel.getPreferredSize().width; CircleModel circleModel = new CircleModel(width); BufferedImage image; JLabel label; Thread thread; long delay = 10; boolean running = false; private boolean advance() { if(!circleModel.isFinished()) { double[] data = circleModel.getData(); double finalx = data[0]; double finaly = data[1]; double finalx2 = data[2]; double finaly2 = data[3]; double finalmittelpunktx = data[4]; double finalmittelpunkty = data[5]; Graphics2D g2 = image.createGraphics(); g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2.setColor(Color.blue); g2.drawLine((int)(finalx),(int)(finaly), (int)(finalx),(int)(finaly)); g2.setColor(Color.red); g2.drawLine((int)(finalx2),(int)(finaly2), (int)(finalx2),(int)(finaly2)); g2.drawLine((int)(finalmittelpunktx),(int)(finalmittelpunkty), (int)(finalmittelpunktx),(int)(finalmittelpunkty)); g2.dispose(); label.repaint(); return false; } return true; } public void run() { long start = System.currentTimeMillis(); while(running) { try { Thread.sleep(delay); } catch(InterruptedException e) { System.out.println("interrupt"); } if(advance()) { stop(); } } long end = System.currentTimeMillis(); System.out.printf("Actual run time: %f seconds.%n", (end - start)/1000.0); } private void start() { double deltat = circleModel.deltat; double cycles = circleModel.tend; double frames = cycles/deltat; long framesPerSecond = 1000/delay; double totalTime = frames/framesPerSecond; System.out.printf("Expected run time for deltat: %.3f, " + "tend: %.0f and delay: %d is %.3f seconds.%n", deltat, cycles, delay, totalTime); if(!running) { running = true; thread = new Thread(this); thread.setPriority(Thread.NORM_PRIORITY); thread.start(); } } public void stop() { running = false; if(thread != null) { thread.interrupt(); } thread = null; } private void eraseImage() { Graphics2D g2 = image.createGraphics(); g2.setBackground(Color.white); g2.clearRect(0,0,image.getWidth(),image.getHeight()); g2.dispose(); } private JPanel getCenterContent() { JPanel panel = new JPanel(new GridLayout(1,0,2,0)); panel.add(circlePanel); Dimension d = circlePanel.getPreferredSize(); image = getCompatibleImage(d.width, d.height); eraseImage(); panel.add(label = new JLabel(new ImageIcon(image))); return panel; } private BufferedImage getCompatibleImage(int w, int h) { return GraphicsEnvironment.getLocalGraphicsEnvironment() .getDefaultScreenDevice() .getDefaultConfiguration() .createCompatibleImage(w, h); } private JPanel getButtonPanel() { String[] ids = { "Change m1", "Start", "Stop", "Erase" }; ActionListener al = new ActionListener() { public void actionPerformed(ActionEvent e) { String id = e.getActionCommand(); if(id.startsWith("Change")) { circlePanel.setM1(1500); circlePanel.repaint(); if(!circleModel.isFinished()) { stop(); } circleModel.reset(); circleModel.setM1(1500); ((JButton)e.getSource()).setEnabled(false); } if(id.equals("Start")) { start(); } if(id.equals("Stop")) { stop(); } if(id.equals("Erase")) { eraseImage(); label.repaint(); } } }; JPanel panel = new JPanel(); for(int i = 0; i < ids.length; i++) { JButton button = new JButton(ids[i]); button.setActionCommand(ids[i]); button.addActionListener(al); panel.add(button); } return panel; } public static void main(String[] args) { Success2Rx app = new Success2Rx(); JFrame frame = new JFrame("Success2Rx"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(app.getCenterContent()); frame.add(app.getButtonPanel(), "Last"); frame.pack(); frame.setVisible(true); } } class CircleModel { int width; int m1=6000; public CircleModel(int width) { this.width = width; } int m = 200000; 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; 0.1; // double result = 0; public double[] getData() { 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); alpha2 = winkel(x2, y2); alpha3 = winkel(poswechselx, poswechsely); 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); return new double[] { finalx, finaly, finalx2, finaly2, finalmittelpunktx, finalmittelpunkty }; } public boolean isFinished() { return t > tend; } public void reset() { t = 0; r = 15; r2 = 20; alpha = alpha2 = alpha3 = alpha4 = 0; x = r; x2 = r2; y = y2 = 0; vx = vx2 = 0; vy = 0.0001 * Math.sqrt(1000 * gamma * m / r); vy2 = 0.0001 * Math.sqrt(1000* gamma * m / r2); } public void setM1(int n) { m1 = n; } private int getWidth() { return width; } private double winkel(double xposition, double yposition){ double 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; } private double skalierenx(double posx){ double minPointX = 508.0; double maxPointX = 366.594; double screenX = (posx - minPointX) * getWidth() / (maxPointX - minPointX); return screenX; } private double skaliereny(double posy){ double minPointY = 497.66; double maxPointY = 337.468; double screenY = (posy - minPointY) * getWidth() / (maxPointY - minPointY); return screenY; } private double skalierenx2(double posx2){ double minPointX2 = 508.0; double maxPointX2 = 366.594; double screenX2 = (posx2 - minPointX2) * getWidth() / (maxPointX2 - minPointX2); return screenX2; } private double skaliereny2(double posy2){ double minPointY2 = 497.66; double maxPointY2 = 337.468; double screenY2 = (posy2 - minPointY2) * getWidth() / (maxPointY2 - minPointY2); return screenY2; } } class CirclePanel extends JPanel { int m1=6000; @Override protected 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); alpha2 = winkel(x2, y2); alpha3 = winkel(poswechselx, poswechsely); 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 Dimension getPreferredSize() { return new Dimension(500,400); } public void setM1(int n) { m1 = n; repaint(); } public double winkel(double xposition, double yposition){ double 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; } }
- 01-10-2010, 06:04 PM #3
Member
- Join Date
- Dec 2009
- Posts
- 19
- Rep Power
- 0
Similar Threads
-
Using BufferedImage
By timkd127 in forum New To JavaReplies: 5Last Post: 12-19-2009, 09:17 PM -
BufferedImage imageScaling
By MINGxDOG in forum New To JavaReplies: 2Last Post: 11-17-2009, 03:04 PM -
BufferedImage through FTP
By dudejonne in forum New To JavaReplies: 7Last Post: 11-05-2009, 05:36 PM -
Convert Byte [] to BufferedImage
By Smily in forum Advanced JavaReplies: 3Last Post: 04-28-2008, 05:54 PM -
BufferedImage to Byte
By Java Tip in forum Java TipReplies: 0Last Post: 01-22-2008, 08:17 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks