Schrittweises Zeichnen

captainigloo

Mitglied
Hallo,


ich habe hier ein Programm (siehe Code unten) welches zwei Planetenbahnen zeichnet. Problem ist, dass alles auf einmal gezeichnet wird, vorgesehen aber schrittweises Zeichnen der Punkte wäre. Der User "javimka" von diesem Forum hat ein Beispielprogramm hier dazu geschrieben , ich verstehe es leider nicht. Kann mir das eventuell jemand näher erklären, wie ich das in meinem Fall schrittweise Zeichnen kann?

Danke

captainigloo

Java:
 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();  
        }  
    }  
  
}
 
Den "wichtigsten" Aspekt hast du weggelassen: Das BufferedImage.

Damit man das Zeichnen "ablaufen" sieht, muss das in einem eigenen Thread passieren. Das funktioniert in diesem Fall nur vernünftig, wenn man die Linien auf ein BufferedImage malt, und DAS dann zeichnet...
 
Hi und Danke für deine Antwort Marco13,



Kann ich mein Programm so lassen und in ein BufferedImage übergeben oder muss ich alles neu schreiben und das Zeichnen von der Punkterechnung trennen?


Da ich ja jetzt noch kein Image habe müsste ich meine Zeichnung auch noch in ein Image umwandeln und dann zu einem BufferedImage?
 
Ja, so viel muss nicht geändert werden... hab' das gerade mal schnell hingeschrieben
Java:
// From [url]http://www.java-forum.org/java-basics-anfaenger-themen/94342-schrittweises-zeichnen.html#post599308[/url]

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
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);

    private BufferedImage image = new BufferedImage(CIRC_SIZE.width, CIRC_SIZE.height, BufferedImage.TYPE_INT_RGB);

    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

     class Circle extends JPanel {

        @Override
        public void paintComponent(Graphics g) {

            super.paintComponent(g);
            g.drawImage(image,0,0,this);
        }
    }


        int m1=6000;
    private void paintIntoImage()
    {
        Thread t = new Thread(new Runnable()
        {
            public void run()
            {
                doPaintIntoImage();
            }
        });
        t.start();
    }



    private void doPaintIntoImage()
    {

        Graphics g = image.getGraphics();

            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.75;
        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),);


            circ.repaint();
            try
            {
                Thread.sleep(5);
            }
            catch (InterruptedException e)
            {
                Thread.currentThread().interrupt();
                return;
            }


            }
        }
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){
        m1 = 8000;
        paintIntoImage();
        c.repaint();
        }
    }

}
 

Neue Themen


Zurück
Oben