Graphe de courbe

Contenu du snippet

il permet de rentrer une fonction et de l'afficher a l'écran. Je dois encore travailler sur le fait que la courbe ne va pas au bout de l'écran. Vous pouvez m'envoyer vos conseils. merci

Source / Exemple :


import java.awt.*;

public class Graphe1 extends java.applet.Applet
{
	Label L = new Label ("F(x) :");
	TextField Fonction = new TextField("",10);
	Button graphique = new Button("Graphe");
	Button Zoom1 = new Button("Zoom 5:5");
	Button Zoom2 = new Button("Zoom 10:10");
	Button Remise = new Button("Ré-initialise");	
	String S1 = "0", S2, S3, S4, S5;
	int  j;
	int index, S, max;
	int y[] = new int[101];
	int x[] = new int[101];
	char Caract;
	int a, b, c, incr;
	int i, I;
	int sens, zoom;
	double g;
	
	public void init()
	{
		add (L);
		add (Fonction);
		add (graphique);
		add (Zoom1);
		add (Zoom2);
		add (Remise);
	}

	public boolean keyDown(Event evt, int key)
	{
		Caract = (char) key;
		if (Caract == '0')
		{
			zero();
		}
		if (Caract == '1')
		{
			un();
		}
		if (Caract == '2')
		{
			deux();
		}
		if (Caract == '3')
		{
			trois();
		}
		if (Caract == '4')
		{
			quatre();
		}
		if (Caract == '5')
		{
			cinq();
		}
		if (Caract == '6')
		{
			six();
		}
		if (Caract == '7')
		{
			sept();
		}
		if (Caract == '8')
		{
			huit();
		}
		if (Caract == '9')
		{
			neuf();
		}
		if (Caract == '+')
		{
			plus();
		}
		if (Caract == 'x')
		{
			Var();
		}
		if (Caract == '-')
		{
			moins();
		}
		if (Caract == '.')
		{
			point();
		}
		return true;
	}

	public boolean action (Event evt, Object arg)
	{
		if (evt.target == graphique)
			Courbe_Texte();
		if (evt.target == Zoom1)
			Zoom1_Texte();
		if (evt.target == Zoom2)
			Zoom2_Texte();
		if (evt.target == Remise)
			Remise_Texte();
		return true;
	}

	public void Zoom1_Texte()
	{
		zoom=1;
		int position = S2.indexOf('x');
		max = S2.length();
		max = max - 1;
		if (position == -1)
		{
			S3 = S2.substring(0);
			b = Integer.valueOf(S3).intValue();
			x[index] = 10;
			index = 0;
			for (i = -5; i <= 5; i++)
			{
				j = (int) b;
				if((j >= -5) && (j <= 5))
				{
					x[index] =(11 + i*2) * 10;
					y[index] = 260 - ((10 + j*2) * 10);
					index++;
				}
			}
			repaint(); 
		}
		if (position == 0) 
		{
			if (max == position)
			{
				x[index] = 10;
				index = 0;
				for (i = -5; i <= 5; i++)
				{
					j = (int) (i);
					if((j >= -5) && (j <= 5))
					{
						x[index] =(11 + i*2) * 10;
						y[index] = 260 - ((10 + j*2) * 10);
						index++;
					}
				}
			}
			else
			{
				if (incr == 1)
				{
					S3 = S2.substring(position+1);
					b = Integer.valueOf(S3).intValue();
					x[index] = 10;
					index = 0;
					for (i = -5; i<=5; i++)
					{
						j = (int) (i + b);
						if((j >= -5) && (j<= 5))
						{
							x[index] =(11 + i*2) * 10;
							y[index] = 260 - ((10 + j*2) * 10);
							index++;
						}
					}
				}
				if(incr == 0)
				{			
					S3 = S2.substring(position+2);
					b = Integer.valueOf(S3).intValue();
					x[index] = 10;
					index = 0;
					for (i = -5; i<=5; i++)
					{
						j = (int) (i + b);
						if((j >= -5) && (j<= 5))
						{
							x[index] =(11 + i*2) * 10;
							y[index] = 260 - ((10 + j*2) * 10);
							index++;
						}
					}
				}
			}
		}
		else
		{
			if (max == position)
			{
				S4 = S2.substring(0,position);
				a = Integer.valueOf(S4).intValue();
				x[index] = 10;
				index = 0;
				for (i = -5; i <= 5; i++)
				{
					j = (int) (a * i);
					if((j >= -5) && (j <= 5))
					{
						x[index] =(11 + i*2) * 10;
						y[index] = 260 - ((10 + j*2) * 10);
						index++;
					}
				}
			}
			else
			{
				S4 = S2.substring(0,position);
				a = Integer.valueOf(S4).intValue();
				if (incr == 1)
				{
	 				S3 = S2.substring(position+1);
					b = Integer.valueOf(S3).intValue();
					x[index] = 10;
					index = 0;
					for (i = -5; i <= 5; i++)
					{
						j = (int) (a * i + b);
						if((j >= -5) && (j <= 5))
						{
							x[index] = (11 + i*2) * 10;
							y[index] = 260 - ((10 + j*2) * 10);
							index++;
						}
					}
				}
				if (incr == 0)
				{
	 				S3 = S2.substring(position+2);
					b = Integer.valueOf(S3).intValue();
					x[index] = 10;
					index = 0;
					for (i = -5; i <= 5; i++)
					{
						j = (int) (a * i + b);
						if((j >= -5) && (j <= 5))
						{
							x[index] =(11 + i*2) * 10;
							y[index] = 260 - ((10 + j*2) * 10);
							index++;
						}
					}
				}
			}
		}
		repaint();
	}	

	public void Zoom2_Texte()
	{
		zoom = 0;
		Courbe_Texte();
		repaint();
	}

	public void Courbe_Texte()
	{
		if(zoom == 1)
		{
			Zoom1_Texte();
		}
		else
		{
			int position = S2.indexOf('x');
			max = S2.length();
			max = max - 1;
			if (position == -1)
			{
				S3 = S2.substring(0);
				b = Integer.valueOf(S3).intValue();
				x[index] = 10;
				index = 0;
				for (i= -10; i<=10 ; i++)
				{
					j = (int) b;
						if((j >=-10 ) && (j <= 10))
						{
							x[index] = (11-i)*10;
							y[index] = 260 -((10-j)*10) ;
							index++;
						}
				}
				repaint(); 
			}
			if (position == 0) 
			{
				if (max == position)
				{
					index = 0;
					for (i= -10; i<=10 ; i++)
					{
						j = i;
						if((j <= 10) && (j >= -10))
						{
							x[index] = (11-i)*10;
							y[index] = 260 -((10-j)*10);
							index++;
						}
					}
				}
				else
				{
					if (incr == 1)
					{
						S3 = S2.substring(position+1);
						b = Integer.valueOf(S3).intValue();
						x[index] = 10;
						index = 0;
						for (i = -10; i<=10; i++)
						{
							j = (int) (i + b);
							if((j >= -10) && (j<= 10))
							{
								x[index] =(11 + i) * 10;
								y[index] = 260 - ((10 + j) * 10);
								index++;
							}
						}
					}
					if(incr == 0)
					{			
						S3 = S2.substring(position+2);
						b = Integer.valueOf(S3).intValue();
						x[index] = 10;
						index = 0;
						for (i = -10; i<=10; i++)
						{
							j = (int) (i + b);
							if((j >= -10) && (j<= 10))
							{
								x[index] =(11 + i) * 10;
								y[index] = 260 - ((10 + j) * 10);
								index++;
							}
						}
					}
				}
			}
			else
			{
				if (max == position)
				{
					S4 = S2.substring(0,position);
					a = Integer.valueOf(S4).intValue();
					x[index] = 10;
					index = 0;
					for (i = -10; i <= 10; i++)
					{
						j = (int) (a * i);
						if((j >= -10) && (j <= 10))
						{
							x[index] =(11 + i) * 10;
							y[index] = 260 - ((10 + j) * 10);
							index++;
						}
					}
				}
				else
				{
					S4 = S2.substring(0,position);
					a = Integer.valueOf(S4).intValue();
					if (incr == 1)
					{
	 					S3 = S2.substring(position+1);
						b = Integer.valueOf(S3).intValue();
						x[index] = 10;
						index = 0;
						for (i = -10; i <= 10; i++)
						{
							j = (int) (a * i + b);
							if((j >= -10) && (j <= 10))
							{
								x[index] = (11 + i) * 10;
								y[index] = 260 - ((10 + j) * 10);
								index++;
							}
						}
					}
					if (incr == 0)
					{
	 					S3 = S2.substring(position+2);
						b = Integer.valueOf(S3).intValue();
						x[index] = 10;
						index = 0;
						for (i = -10; i <= 10; i++)
					{
							j = (int) (a * i + b);
							if((j >= -10) && (j <= 10))
							{
								x[index] =(11 + i) * 10;
								y[index] = 260 - ((10 + j) * 10);
								index++;
							}
						}
					}
				}
			}
		}
		repaint();
	}

	public void Remise_Texte()
	{
		Fonction.setText("");
		S2 = "0";
		S1="";
		index = 0;
		S = 0;
		repaint();
	}

	public void paint (Graphics Visuelle)
	{

		if (zoom == 0)
		{
			Visuelle.drawRect(10, 60, 200, 200);
			Visuelle.drawLine(10, 160, 209, 160);
			Visuelle.drawLine(110, 60, 110, 260);
			
			Visuelle.drawLine(20, 160, 20, 155);
			Visuelle.drawLine(30, 160, 30, 155);
			Visuelle.drawLine(40, 160, 40, 155);
			Visuelle.drawLine(50, 160, 50, 155);
			Visuelle.drawString("-5", 57, 150);
			Visuelle.drawLine(60, 160, 60, 155);
			Visuelle.drawLine(70, 160, 70, 155);
			Visuelle.drawLine(80, 160, 80, 155);
			Visuelle.drawLine(90, 160, 90, 155);
			Visuelle.drawLine(100, 160, 100, 155);			

			Visuelle.drawLine(120, 160, 120, 155);
			Visuelle.drawLine(130, 160, 130, 155);
			Visuelle.drawLine(140, 160, 140, 155);
			Visuelle.drawLine(150, 160, 150, 155);
			Visuelle.drawString("5", 158, 150);
			Visuelle.drawLine(160, 160, 160, 155);
			Visuelle.drawLine(170, 160, 170, 155);
			Visuelle.drawLine(180, 160, 180, 155);
			Visuelle.drawLine(190, 160, 190, 155);
			Visuelle.drawLine(200, 160, 200, 155);
	
			Visuelle.drawLine(110, 70, 115, 70);
			Visuelle.drawLine(110, 80, 115, 80);
			Visuelle.drawLine(110, 90, 115, 90);
			Visuelle.drawLine(110, 100, 115, 100);
			Visuelle.drawString("5", 120, 114);
			Visuelle.drawLine(110, 110, 115, 110);
			Visuelle.drawLine(110, 120, 115, 120);
			Visuelle.drawLine(110, 130, 115, 130);
			Visuelle.drawLine(110, 140, 115, 140);
			Visuelle.drawLine(110, 150, 115, 150);	

			Visuelle.drawLine(110, 170, 115, 170);
			Visuelle.drawLine(110, 180, 115, 180);
			Visuelle.drawLine(110, 190, 115, 190);
			Visuelle.drawLine(110, 200, 115, 200);
			Visuelle.drawString("-5", 120, 214);
			Visuelle.drawLine(110, 210, 115, 210);
			Visuelle.drawLine(110, 220, 115, 220);
			Visuelle.drawLine(110, 230, 115, 230);
			Visuelle.drawLine(110, 240, 115, 240);
			Visuelle.drawLine(110, 250, 115, 250);			
			Visuelle.drawPolygon(x, y, index);
		}
		else
		{
			Visuelle.drawRect(10, 60, 200, 200);
			Visuelle.drawLine(10, 160, 209, 160);
			Visuelle.drawLine(110, 60, 110, 260);
			
			Visuelle.drawLine(30, 160, 30, 155);
			Visuelle.drawString("-4",26,150);
			Visuelle.drawLine(50, 160, 50, 155);
			Visuelle.drawLine(70, 160, 70, 155);
			Visuelle.drawString("-2",66,150);
			Visuelle.drawLine(90, 160, 90, 155);
			
			Visuelle.drawLine(130, 160, 130, 155);
			Visuelle.drawLine(150, 160, 150, 155);
			Visuelle.drawString("2",147,150);
			Visuelle.drawLine(170, 160, 170, 155);
			Visuelle.drawLine(190, 160, 190, 155);
			Visuelle.drawString("4",187,150);
			
			Visuelle.drawLine(110, 80, 115, 80);
			Visuelle.drawString("4",120,84);
			Visuelle.drawLine(110, 100, 115, 100);
			Visuelle.drawLine(110, 120, 115, 120);
			Visuelle.drawString("2",120,124);
			Visuelle.drawLine(110, 140, 115, 140);
			
			Visuelle.drawLine(110, 180, 115, 180);
			Visuelle.drawLine(110, 200, 115, 200);
			Visuelle.drawString("-2",120,204);
			Visuelle.drawLine(110, 220, 115, 220);
			Visuelle.drawLine(110, 240, 115, 240);
			Visuelle.drawString("-4",120,244);
			Visuelle.drawPolygon(x, y, index);
		}
	}

	public void un()
	{
		if (S == 2)
		{
			S1 = S1 + "1";
			S2 = S1 + "1";
			Fonction.setText(String.valueOf(S1));
			S = 1;
		}
		else
		{
			S1 = "1";
			S2 = S1;
			Fonction.setText(String.valueOf(S1));
			S = 1;
		}
	}

	public void deux()
	{
		if (S == 2)
		{
			S1 = S1 + "2";
			S2 = S2 + "2";
			Fonction.setText(String.valueOf(S1));
			S = 1;
		}
		else
		{
			S1 = "2";
			S2 = S1;
			Fonction.setText(String.valueOf(S1));
			S = 1;
		}
	}
	
	public void trois()
	{
		if (S == 2)
		{
			S1 = S1 + "3";
			S2 = S2 + "3";
			Fonction.setText(String.valueOf(S1));
			S = 1;
		}
		else
		{
			S1 = "3";
			S2 = S1;
			Fonction.setText(String.valueOf(S1));
			S = 1;
		}
	}
	
	public void quatre()
	{
		if (S == 2)
		{
			S1 = S1 + "4";
			S2 = S2 + "4";
			Fonction.setText(String.valueOf(S1));
			S = 1;
		}
		else
		{
			S1 = "4";
			S2 = S1;
			Fonction.setText(String.valueOf(S1));
			S = 1;
		}
	}

	public void cinq()
	{
		if (S == 2)
		{
			S1 = S1 + "5";
			S2 = S2 + "5";
			Fonction.setText(String.valueOf(S1));
			S = 1;
		}
		else
		{
			S1 = "5";
			S2 = S1;
			Fonction.setText(String.valueOf(S1));
			S = 1;
		}
	}

	public void six()
	{
		if (S == 2)
		{
			S1 = S1 + "6";
			S2 = S2 + "6";
			Fonction.setText(String.valueOf(S1));
			S = 1;
		}
		else
		{
			S1 = "6";
			S2 = S1;
			Fonction.setText(String.valueOf(S1));
			S = 1;
		}
	}

	public void sept()
	{
		if (S == 2)
		{
			S1 = S1 +"7";
			S2 = S2 +"7";
			Fonction.setText(String.valueOf(S1));
			S = 1;
		}
		else
		{
			S1 = "7";
			S2 = S1;
			Fonction.setText(String.valueOf(S1));
			S = 1;
		}
	}

	public void huit()
	{
		if (S == 2)
		{
			S1 = S1 + "8";
			S2 = S2 + "8";
			Fonction.setText(String.valueOf(S1));
			S = 1;
		}
		else
		{
			S1 = "8";
			S2 = S1;
			Fonction.setText(String.valueOf(S1));
			S = 1;
		}
	}

	public void neuf()
	{
		if (S == 2)
		{
			S1 = S1 + "9";
			S2 = S2 + "9";
			Fonction.setText(String.valueOf(S1));
			S = 1;
		}
		else
		{
			S1 = "9";
			S2 = S1;
			Fonction.setText(String.valueOf(S1));
			S = 1;
		}
	}

	public void plus()
	{
		if (S == 1)
		{
			S1 = S1 + "+";
			S2 = S1;
			Fonction.setText(String.valueOf(S1));
			S = 2;
			incr = 0;
		}
		if (S == 3)
		{
			S1 = S1 + "+";
			S2 = S2 + "+";
			Fonction.setText(String.valueOf(S1));
			S = 2;
			incr = 0;
		}
	}

	public void Var()
	{
		if (S == 1)
		{
			S1 = S1 + "x";
			S2 = S2 + "x";
			Fonction.setText(String.valueOf(S1));
			S = 1;
		}
		else
		{
			if (S == 2)
			{
				S1 = S1 + "x";
				S2 = "-" + "1" + "x";
				Fonction.setText(String.valueOf(S1));
				S = 3;
			}
			else
			{
				S1 = "x";
				S2 = S1;
				Fonction.setText(String.valueOf(S1));
				S = 1;
			}
		}
	}

	public void zero()
	{
		if (S == 1)
		{
			S1 = S1 + "0";
			S2 = S2 + "0";
			Fonction.setText(String.valueOf(S1));
			S = 1;
		}
		
	}

	public void point()
	{
		if (S == 1)
		{
			S1 = S1 + ".";
			S2 = S2 + ".";
			Fonction.setText(String.valueOf(S1));
			S = 2;
		}
		else
		{
			S1 = "0" + ".";
			S2 = S1;
			Fonction.setText(String.valueOf(S1));
			S = 2;
		}
	}

	public void moins()
	{
		
		if (S == 1)
		{
			S1 = S1 + "-";
			S2 = S2 + "-";
			Fonction.setText(String.valueOf(S1));
			S = 2;
			incr = 1;
		}
		else
		{
			if (S == 3)
			{
				S1 = S1 + "-";
				S2 = S2 + "-";
				Fonction.setText(String.valueOf(S1));
				S = 2;
				incr = 1;
			}
			else
			{
				S1 = "-";
				S2 = "-";
				Fonction.setText(String.valueOf(S1));
				S = 2;
			}
		}
	}

	public static void main(String args[])
	{
		Graphe1 Test = new Graphe1();
		Test.show();
	}
}

Conclusion :


<html>
voici le html a appliquer pour etre sur que sa fonctionne
<head>
<title>
Essai de la calculatrice
</title>
<body>
<applet code="Graphe1.class" width = 300, height = 300>
votre navigateur n'est pas compatible java
</applet>
</body>
</html>

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.