Lesson 5 Changing parameters

Points and Activities

  1. How to move figures interactively
  2. Rounding off numbers

Example 5

Screen of the Applets

Exm5.java
import java.awt.*;                 
import java.applet.*;              
import java.awt.event.*;

public class Exm5 extends Applet implements ActionListener  
{
	Button button1 = new Button();
	Button button2 = new Button();
	Label label1 = new Label();
    
	int vxmin,vymin,vxmax,vymax;  //screen coordinates
	double xmin,ymin,xmax,ymax;   //imaginary coordinates
	double dx;                    //interval of x
	double a;                     // parameter
	double da;                    //interval of a
    
	/* initializing */
	public void init(){
		String str;
        
		xmin=-5;xmax=5;ymin=-3;ymax=7;
		vxmin=0;vxmax=300;vymin=0;vymax=300;
		dx=0.1;
		a=1;da=0.1;
        
		setLayout(null);  //free layout
		setSize(400,300); //size of this applet
		
		add(button1);
		button1.setLabel("+");
		button1.setFont(new Font("Dialog", Font.PLAIN, 16));
		button1.setBounds(320,40,20,20);
		button1.addActionListener(this);
	    
		add(button2);
		button2.setLabel("-");
		button2.setFont(new Font("Dialog", Font.PLAIN, 16));
		button2.setBounds(350,40,20,20);
		button2.addActionListener(this);
        
		add(label1);
		str="a = "+Double.toString(a);
		label1.setText(str);
		label1.setFont(new Font("Dialog", Font.PLAIN, 16));
		label1.setBounds(320,60,60,40);
		
      
    	}
    
	public void paint(Graphics g)  
	{             
	    	double x1,y1,x2,y2;           
	   
	   	 //axes and scale
	    	scale(g);
	    
	    	//graph
	    	g.setColor(Color.blue);     
	    	x2=xmin;y2=func(x2);
		for(x1=xmin;x1<=xmax;x1=x1+dx){
			y1=func(x1);
		    	Line(g,x1,y1,x2,y2);
		    	x2=x1;y2=y1;
		}
	}
	
	/* Convert imaginary coordinates to screen cordinates. */
	public int mapX(double x){
	    	int sx;
	sx=vxmin+(int)((x-xmin)/(xmax-xmin)*(double)(vxmax-vxmin)) ;
		return sx;
    	}
    	public int mapY(double y){
		int sy;
	   sy=vymin+(int)((ymax-y)/(ymax-ymin)*(double)(vymax-vymin));
	    	return sy;
    	}
    
    	/* Drawing Line */
    	public void Line(Graphics g,double x1,double y1,double x2,double y2){
		g.drawLine(mapX(x1),mapY(y1),mapX(x2),mapY(y2));
	}
    
    	/* Defining function */
    	public double func(double x){
		double y;
		y=a*x*x;
		return y;
    	}
    
    	/* axes and lattice */
    	public void scale(Graphics g){
		double x,y;
		//back color
	    	g.setColor(new Color(255, 255, 192)); 
	    	g.fillRect(vxmin,vymin,vxmax-vxmin,vymax-vymin);
	     
	    	// scale
	    	g.setColor(Color.lightGray);
	    	for (x=xmin;x<=xmax;x=x+1){
			Line(g,x,ymin,x,ymax);
	    	}
	    	for (y=ymin;y<=ymax;y=y+1){
			Line(g,xmin,y,xmax,y);
	    	}
	    
	    	// xy-axes
	    	g.setColor(Color.black);
	    	Line(g,0,ymin,0,ymax);
	    	Line(g,xmin,0,xmax,0);     
    	}
    
    	public void actionPerformed(ActionEvent event)
	{
	    	String str;
		Object object = event.getSource();
		if (object == button1){
			a=a+da;
		    	a=(double)(Math.round(10*a))/10;
		    	repaint();
		    	str="a = "+Double.toString(a);
		    	label1.setText(str);
	    	}
	    	if (object == button2){
			a=a-da;
			a=(double)(Math.round(10*a))/10;
		    	repaint();
		    	str="a = "+Double.toString(a);
		    	label1.setText(str);
	        
	    	}
	}
}


Exm5.html
<HTML>
<HEAD>
<TITLE>Exm5</TITLE>
</HEAD>
<BODY>
<APPLET CODE="Exm5.class" WIDTH=400 HEIGHT=300></APPLET>
</BODY>
</HTML>


Excercise 5a Modify the applet of example 5 to draw the graph of y = a x2 + bx +c, and add four buttons to change b and c.

Excercise 5b Make the applet drawing the graph of y = a sin (bx+c). Add 6 buttons to change the three parameters.