Parabel zeichnen

Status
Nicht offen für weitere Antworten.

Robel

Mitglied
Ich hab ein programm geschrieben das eine parabel zeichnen soll. es werden a,b und c von der Normalform y=ax²+bx+c angeben.

es wird immer was total falsches angezeigt.
ich bin das folgende programm mehrmahls im kopf durchgegangen konnte aber nichts finden.




Code:
import java.awt.*;


public class Parabel extends Frame  
{
   private int a,b,c;
   public Parabel(int a1,int b1,int c1)
   {
   	setLocation(300,300);
	setSize(500,500);
	setTitle("Parabel");
	addWindowListener(new WindowClosingAdapter());
	setVisible(true);
	a=a1;
	b=b1;
	c=c1;
	/*
	werte zum testen:
	a = 1, b = 0,  c = 2
	a = 2, b = 0,  c = 0
	a = 3, b = -5, c = 2
	a = 0, b = 0,  c = 0
	a = 0, b = 2,  c = 2
	a = 0, b = 0,  c = 5
	*/

}

public static void main(String[] args)
{
	Parabel p1 = new Parabel(3,-5,2);
}
   public void paint(Graphics g) {
     int x0,y0,x1,y1;
     double x,y;
     final int pixel=10;//10 Pixel entsprechen einer Einheit
     x0=250;
     y0=250;
     for(int i=1;i<=100;i++) { 
       x1=x0+1;
       x=(double)x1/pixel;
       y=a*x*x+b+x+c;
       y1=(int)Math.round(y*pixel);
       System.out.print("x0: " + x0 + "   y0: " + y0 + "\nx1: " + x1 + "   y0: " + y1 + "\n");
       g.drawLine(x0,y0,x1,y1);
       x0=x1;
       y0=y1;
     }
   }
 }

für eure hilfe wäre ich sehr dankbar.
mfg Robel
 

Manfred

Bekanntes Mitglied
Es waren ein paar Fehler drinnen, einer z.B., dass a,b,c zugewiesen wurde, nachdem Visible gesetzt worden ist.
Ich habs geändert, dass es funktioniert, schau dir einfach die Unterschiede an bzw. probier herum!

Code:
import java.awt.*;
import javax.swing.*;


public class Parabel extends JFrame  
{ 
   private int a,b,c; 
   public Parabel(int a1,int b1,int c1) 
   { 
       a = a1;
       b = b1;
       c = c1; 
       setLocation(300, 300);
       setSize(500, 500);
       setTitle("Parabel");
       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       setVisible(true);


   }



   public void paint(Graphics g) 
   { 

       double xold=0, yold=0;
       g.setColor(Color.black);
       g.drawLine(0,250,500,250);
       g.drawLine(250,0,250,500);
       g.translate(250,250);
       double y=0;
       final int pixel=50;
       g.setColor(Color.blue);
       for(double x=-50;x<=50;x+=.1) 
       {  

           y=a*x*x+b*x+c; 
           y*=-1;
           if(x==-50)
           {
               xold=x;
               yold=y;
           }
          
           g.drawLine((int)(xold*pixel),(int)(yold*pixel),(int)(x*pixel),(int)(y*pixel)); 
           xold=x; 
           yold=y; 
       } 

   } 
   
   public static void main(String[] args) 
   { 
      Parabel p1 = new Parabel(3,-5,2); 
   } 
}
 
Status
Nicht offen für weitere Antworten.

Neue Themen


Oben