Probleme applet Java

chiro2000 Messages postés 19 Date d'inscription mercredi 31 octobre 2007 Statut Membre Dernière intervention 3 octobre 2011 - 11 avril 2011 à 17:38
chiro2000 Messages postés 19 Date d'inscription mercredi 31 octobre 2007 Statut Membre Dernière intervention 3 octobre 2011 - 12 avril 2011 à 20:41
Bonjour a tous

J ai souci avec ma classe suivante. Mon but est dessiner la carte correspondant au fichier passe en parametre(data.tx)

<code type="java">


import java.applet.*;
import java.awt.*;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

public class Drawtree extends Applet {

int width, height;

private int x1;
private int y1;
private int x2;
private int y2;
ArrayList<String> listes = new ArrayList();

public void init() {
width = getSize().width;
height = getSize().height;
setBackground( Color.black );
String filePath = "data.txt";

BufferedReader buff = null;
try {
buff = new BufferedReader(new FileReader(filePath));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

String line;
int cpt;

cpt = 0;
while (cpt < 10)
{
try {
line = buff.readLine();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
cpt++;
}
try {
while ((line = buff.readLine()) != null)
listes.add(line);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}



public ArrayList<String> getListes() {
return listes;
}



public void setListes(ArrayList listes) {
this.listes = listes;
}

public void paint( Graphics g ) {

int width,heigh;
width = getSize().width;
heigh = getSize().height;
setBackground(Color.BLACK);

String line, line2;

String []save;
String []save2;

listes = this.getListes();
g.setColor(Color.green);

for (int i = 0; i < listes.size(); ++i)
{

line = (String)listes.get(i);
g.drawString(line, 0, 0);
save = line.split(" ");
line2 = (String)listes.get(++i);
save2 = line2.split(" ");
x1 = (int)Float.parseFloat(save[1]);
y1 =(int) Float.parseFloat(save[2]);
x2 = (int)Float.parseFloat(save2[1]);
y2 = (int)Float.parseFloat(save2[2]);
g.drawLine(x1,y1,x2, y2);

}
}
}


<code/>


Mon but est de dessiner cette carte:carte en faisant des drawLine . et aussi voici le fichier mon fichier data.txt data

Mais ya rien qui s affiche... si quelqu un a une idee????

2 réponses

Utilisateur anonyme
12 avril 2011 à 00:13
Salut,

Commence par modifier ces 2 lignes comme indiqué <<<<<<<:
    //ArrayList<String> listes = new ArrayList();
      ArrayList<String> listes = new ArrayList<String>(); // <<<<<<<<<<<<<

Et:
    //public void setListes(ArrayList listes) {
      public void setListes(ArrayList<String> listes)  { // <<<<<<<<<<<<<<


Compile, test et tiens-moi au courrant si tu veux...

Cordialement,

Dan


...\ Dan /...
0
chiro2000 Messages postés 19 Date d'inscription mercredi 31 octobre 2007 Statut Membre Dernière intervention 3 octobre 2011
12 avril 2011 à 20:41
J ai pu avance depuis, j ai maintenant mes different point qui s affichent
Voici le code actualise



public class Drawtree extends Applet {

   int width, height;
  
    private int x1;
private int y1;
private int x2;
private int y2;
ArrayList<String> listes = new ArrayList<String>();

   public void init() {
      width = getSize().width;
      height = getSize().height;
      setBackground( Color.black );
      String filePath = "data.txt";
      
   BufferedReader buff = null;
try {
buff = new BufferedReader(new FileReader(filePath));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

String line;
int cpt;

cpt = 0;
while (cpt < 10)
{
try {
line = buff.readLine();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
cpt++;
}
try {
while ((line = buff.readLine()) != null)
listes.add(line);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
   }

   
   
   public ArrayList<String> getListes() {
return listes;
}



public void setListes(ArrayList<String> listes) {
this.listes = listes;
}
   
   public void paint( Graphics g ) {
   
    int width,heigh;
   	width = getSize().width;
   	heigh = getSize().height;
   	setBackground(Color.BLACK);
   	
   	String line, line2;

   	String []save;
   	String []save2;
   
   	listes = this.getListes();
   	g.setColor(Color.green);
   
   	int sizeX=getSize().width;
int sizeY=getSize().height;
int scaleX=sizeX/30;
int scaleY=(sizeY-70)/30;
   	for (int i = 0; i < listes.size(); i++)
   		{
   			line = (String)listes.get(i);
   			g.setColor(Color.green);
   		    //g.drawString(line, 0, 0);
   			save = line.split(" ");
   		    //g.drawString(line, 0, 0);	
   			line2 = (String)listes.get(++i);
   			save2 = line2.split(" ");
   			x1 = (int)Float.parseFloat(save[1])/100;
    	         y1 = (int)Float.parseFloat(save[2])/100;
    	         x2 = (int)Float.parseFloat(save2[1])/100;
   		  	y2 = (int)Float.parseFloat(save2[2])/100;
   			
   			g.translate(30, 20);
   			
   			g.setColor(Color.green);
   			
   			g.drawRect(x1, y1,1,1);

   			g.drawRect(x2, y2,1,1);
   		
      }
   	
   }
}






En fait je suis parti de ce fichier data.txt qui se trouve la data.txt normalement je dois avoir un graphe comme ceci graphe
seulement j ai pas du tout le meme resultat.
Ma question est comment puige modifier mon code histoire d avoir le meme graphe que sur le site.
0
Rejoignez-nous