Home » Tutti gli Articoli » Java » Disegnare una Parabola Applet Java

Ads Link

Menu Soluzioni

Ads

 
Disegnare una Parabola Applet Java PDF Stampa E-mail
Scritto da Administrator   
Martedì 18 Ottobre 2011 19:17

Java Applet - Disegnare una Parabola

package it.soluzionijava.applet;

import java.applet.Applet;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;

public class Parabola extends Applet implements MouseMotionListener  {

    static final String message = "Hello World!";
    static final int S3_LOCATION =508;
    static final int S2_LOCATION =450;
    static final int S1_LOCATION =398;
    static final int SLIDER_LENGTH = 200;
    static final int SLIDER_TOP=90;
    int SLIDER_BOTTOM=SLIDER_TOP+SLIDER_LENGTH;

    private Font font = new Font("sansserif", Font.ITALIC + Font.BOLD, 12);
    int ax, ay, bx, by, cx, cy,j;
    double a,b,c, sx, sy, fociX, fociY;
	double h,k,r1,r2; // parabola vars for vertex and roots
   
    
    public void init() {
        setLayout (null);
        this.setBackground(new Color(0xFFEEAA));
        addMouseMotionListener(this);
        a=1.0; b=4.0; c=-5.0;
        ax=S1_LOCATION;  
        ay=SLIDER_TOP+99;
        bx=S2_LOCATION; 
        by=SLIDER_TOP+99;
        cx=S3_LOCATION; 
        cy=SLIDER_TOP+99;
    }
    private double f(double x){
        return 1.0*a*x*x+b*x+c;
    }
    private int screenX (double x){
        return (int)(160+10*x+20); //converts -15<x<15 to screen coords
    }
    private int screenY (double y){
        return (int)(150-10*y+10); //converts -15<y<15 to screen coords
    }
    
    public void paint (Graphics g) {
        //Draw Grid
        g.setColor(new Color(0x108000));
        g.fillRect(10,10,340,280);
        g.setColor(new Color(0x08602F)); //lines
        for(int i = -16; i< 18; i++){
         g.drawLine( screenX(i),10,screenX(i) ,289); // verticals
        }
        for(int i = -13; i< 16; i++) {
         g.drawLine( 10, screenY(i), 350, screenY(i) ); //horizontals
        }
        g.setColor(Color.black); 
        g.drawLine( 10, screenY(0), 350, screenY(0) ); // x-axis
        g.drawLine( screenX(0),10,screenX(0) ,289);  // y=axis
        g.setFont (new Font("serif", 0, 9));

        for(int i = 2; i< 10; i+=2) g.drawString(i+" ", screenX(i)-1, screenY(0)+9); 
        for(int i = 10; i< 18; i+=2) g.drawString(i+" ", screenX(i)-4, screenY(0)+9);
	for(int i = -8; i< 0; i+=2) g.drawString(i+" ", screenX(i)-4, screenY(0)+9);  
	for(int i = -16; i< -9; i+=2) g.drawString(i+" ", screenX(i)-7, screenY(0)+9); 
	
	for(int i = 2; i< 16; i+=2) g.drawString(i+" ", screenX(0)+2, screenY(i)+4);
	for(int i = -12; i< 0; i+=2) g.drawString(i+" ", screenX(0)+2, screenY(i)+4);
       
        
         
        //Draw Curve
        // if a=0 it is a line y=-c/b
		if (a==0.0 && b!=0.0){
		g.setColor(Color.cyan);
		g.drawLine(10,screenY(-1.0*c/b),350 , screenY(-1.0*c/b));	
		}
		
		// if a!=0 then parabola
		// vertex at (h,k) where 
		// h=-1*b/(2*a) and k=f(h)=a*h*h+b*h+c=(4*a*c-b*b)/4*a
		// axis of symmetry is x=h
		// alternate equation is a(x-h)^2 + k
		if (a!=0.0){
		h=-1.0*b/(2.0*a);
		k=(4.0*a*c-b*b)/(4.0*a);
		double d2=b*b-4.0*a*c; // the discriminate
		g.setFont (new Font("serif", Font.ITALIC+Font.BOLD, 14));
		g.setColor(Color.orange);
		if(d2>=0){
			g.setColor(Color.orange);
			r1=(-1.0*b-Math.sqrt(d2))/(2.0*a);
			r2=(-1.0*b+Math.sqrt(d2))/(2.0*a);
			g.drawString("1st root: "+r1,30,275);
			g.drawString("2nd root: "+r2,25,290);
		}
		if (d2<0){
		   // g.setColor(new Color(0x3F1212));//dark brown
			g.setColor(Color.pink);
			r1=(-1.0*b)/(2.0*a);
			r2=Math.sqrt(-1.0*d2)/(2.0*a);
			g.drawString("Complex roots: ", 30,275);
			g.drawString(r1+" ± "+r2+" i",30,290);
		}
		g.setFont (new Font("sansserif", Font.ITALIC+Font.BOLD, 14));
		g.setColor(new Color(0x108000)); //dark green
		g.drawString("f(x)="+a+"(x-("+h+"))^2 + ("+k+")",360 ,45 );
		//
		//
		//Draw the axis of symmetry
		g.setColor(Color.orange);
		g.drawLine(screenX(h),0 , screenX(h), 300);
		
		//
		// Draw the parabola
		g.setColor(Color.cyan);
		for (double i=-17.0;i<18.0;i=i+.5){
			g.drawLine(screenX(i),screenY(f(i)),screenX(i+.5), screenY(f(i+.5)));
		}
		//
		// Draw vertex 
		g.setColor(Color.yellow);
		g.drawOval(screenX(h)-1,screenY(k)-1,2,2);
		}//end of paraboloa drawing
		
		
       
        
        // Draw Controls
        g.setColor(Color.red);
        g.fillOval(ax-5,ay-5,10,10);
        g.setColor(Color.blue);
        g.fillOval(bx-5,by-5,10,10);
        g.setColor(Color.magenta);
        g.fillOval(cx-5,cy-5,10,10);
        g.setColor(Color.black);
        
        g.setFont (new Font("serif", Font.ITALIC + Font.BOLD, 16));
		g.setColor(Color.black);
		g.drawString("f(x)=", 352, 28);
		if(a!=0)g.drawString("x", 415, 28);
        if (b!=0) g.drawString("x", 478, 28);
        g.setFont (new Font("serif", Font.BOLD, 12));
        if (a!=0)g.drawString("2", 422, 21);
        		
		// Draw the Equation in Standard form
        g.setFont (new Font("sansserif", Font.BOLD, 16));
        g.setColor(Color.red);
		if (a!=0) g.drawString(Math.abs(a)+"", 390, 28);
        g.setColor(Color.blue);
		if (b!=0) g.drawString(Math.abs(b)+"", 445, 28);
        g.setColor(Color.magenta);
		if (c!=0) g.drawString(Math.abs(c)+" ",500, 28);
		g.setColor(Color.black);
         g.setFont (new Font("serif", Font.BOLD, 20));
        if (a<0) {
			g.setColor(Color.red);
			g.drawString("-", 385, 28);
			g.setColor(Color.black);
			}
        if (b<0) g.drawString("-",430, 28);
        if (b>0) g.drawString("+", 430, 30);
		if (c<0) g.drawString("-",488, 28);
        if (c>0) g.drawString("+", 488, 30);

        g.setFont (new Font("serif", Font.ITALIC + Font.BOLD, 12));
        
        g.drawString("a = "+a,S1_LOCATION-15, SLIDER_TOP-12);
        g.drawString("b = "+b,S2_LOCATION-15, SLIDER_TOP-12);
        g.drawString("c = "+c,S3_LOCATION-15, SLIDER_TOP-12);
        g.drawLine(S1_LOCATION,SLIDER_TOP,S1_LOCATION,SLIDER_BOTTOM);
        g.drawLine(S2_LOCATION,SLIDER_TOP,S2_LOCATION,SLIDER_BOTTOM);
        g.drawLine(S3_LOCATION,SLIDER_TOP,S3_LOCATION,SLIDER_BOTTOM);
		for(int j=0;j<21;j=j+5){
		 g.drawLine(S1_LOCATION-6,SLIDER_TOP+j*SLIDER_LENGTH/20,S1_LOCATION+6,SLIDER_TOP+j*SLIDER_LENGTH/20);
		 g.drawLine(S2_LOCATION-6,SLIDER_TOP+j*SLIDER_LENGTH/20,S2_LOCATION+6,SLIDER_TOP+j*SLIDER_LENGTH/20);
		 g.drawLine(S3_LOCATION-6,SLIDER_TOP+j*SLIDER_LENGTH/20,S3_LOCATION+6,SLIDER_TOP+j*SLIDER_LENGTH/20);
		}
    }
    public void mouseMoved(MouseEvent e) {}
    public void mouseDragged(MouseEvent e) {
    int mx=e.getX();
    int my=e.getY();
    // check if inside red or blue dot
    // put in approporiate 
        //if (Math.abs(my-ry)<17 && Math.abs(mx-rx)<17){  
        if (my>SLIDER_TOP-17 && Math.abs(mx-S1_LOCATION)<17){        
		ax = S1_LOCATION;
                if (my>SLIDER_BOTTOM) my=SLIDER_BOTTOM;
                if (my<SLIDER_TOP) my=SLIDER_TOP;
                ay = my;
               	a=(190-ay)/10.0;	
        }
        if (my>=SLIDER_TOP-17 && Math.abs(mx-S2_LOCATION)<17){        
		bx = S2_LOCATION;
                if (my>SLIDER_BOTTOM) my=SLIDER_BOTTOM;
                if (my<SLIDER_TOP) my=SLIDER_TOP;
                by = my;
               	b=(190-by)/2.0;	
        }
        if (my>=SLIDER_TOP-17 && Math.abs(mx-S3_LOCATION)<17){        
		cx = S3_LOCATION;
                if (my>SLIDER_BOTTOM) my=SLIDER_BOTTOM;
                if (my<SLIDER_TOP) my=SLIDER_TOP;
                cy = my;
               	c=(190-cy)/2.0;	
        }
        repaint();

    }
}

 

 

Condividi
Ultimo aggiornamento Martedì 18 Ottobre 2011 19:19
 

Commenti  

 
0 #6 profile 2018-11-01 12:29
Need cheap hosting? Try webhosting1st, just $10 for an year.

Citazione
 
 
0 #5 skin care 2017-02-22 17:51
I like your writing style really loving this site.
Citazione
 
 
0 #4 http:// 2014-04-18 07:14
It's genuinely very complex in this active life to listen news on Television, so I simply use world
wide web for that purpose, and get the most recent news.
Citazione
 
 
0 #3 http://journal- 2014-04-17 06:51
Excellent blog right here! Also your website quite a bit up fast!
What web host are you the use of? Can I am getting
your affiliate link on your host? I want my website loaded up as fast
as yours lol
Citazione
 
 
0 #2 http://iupatdc5.org 2014-04-16 06:59
Good article. I am going through many of these issues as well..
Citazione
 
 
0 #1 http://journal- 2014-04-16 06:32
Hello there, You've done an incredible job. I'll definitely digg
it and personally suggest to my friends. I'm sure they'll be
benefited from this website.
Citazione
 

Aggiungi commento


Codice di sicurezza
Aggiorna

Questo sito utilizza i cookie e tecnologie simili per consentire la corretta navigazione. To find out more about the cookies we use and how to delete them, see our privacy policy.

I accept cookies from this site.

EU Cookie Directive Module Information