2012-01-05 3 views
1

J'ai un problème avec java Graphics, je suis en train d'écrire un programme qui lit un fichier texte et affiche des résultats.Java dessiner des chaînes à l'aide des données de ArrayList

Par exemple:

fichier texte

print("Text",20,100) 
print("Hello",135,50) 

2 résultat souhaité chaînes affichées à l'écran. Mais je prends seulement le dernier.

Un échantillon de mon code:

ArrayList<String[]> StringsToDraw = new ArrayList<String[]>(); 

//Add some data to the List 
StringsToDraw.add(new String[] {"Hello","20","35"}); 
StringsToDraw.add(new String[] {"World","100","100"}); 

@Override 
public void paint(Graphics g){ 
    Graphics2D g2d = (Graphics2D) g; 
    for(String[] printMe : StringsToDraw){ 
    drawString(g2d, printMe[0], printMe[1], printMe[2]) 
    } 
} 

public void drawString(Graphics g2d, String text, String xString, String yString){ 
    int x = Integer.parseInt(xString); 
    int y = Integer.parseInt(yString); 
    g2d.drawString(text, x, y); 
} 

Comment puis-je changer de sorte qu'il peut afficher les deux?

+0

Etes-vous sûr de ne pas sortir des limites des limites de votre graphique? – rurouni

Répondre

0

Votre boîte englobante est peut-être trop petite. Essayez ceci et voyez si cela fonctionne pour vous:

public class Graphics2DTest extends JFrame { 
    private static final long serialVersionUID = 1L; 

    public static void main(String[] args) { 
     Graphics2DTest test = new Graphics2DTest(); 
     System.out.println(test); 
    } 

    private List<String[]> StringsToDraw = new ArrayList<String[]>(4); 

    public Graphics2DTest() { 
     super(); 

     StringsToDraw.add(new String[] { "Hello", "20", "35" }); 
     StringsToDraw.add(new String[] { "World", "100", "100" }); 

     setSize(400, 400); 
     setBackground(Color.YELLOW); 
     setForeground(Color.BLUE); 
     setVisible(true); 
    } 

    @Override 
    public void paint(Graphics g) { 
     Graphics2D g2d = (Graphics2D) g; 
     for (String[] printMe : StringsToDraw) { 
      drawString(g2d, printMe[0], printMe[1], printMe[2]); 
     } 
    } 

    public void drawString(Graphics g2d, String text, String xString, 
      String yString) { 
     int x = Integer.parseInt(xString); 
     int y = Integer.parseInt(yString); 
     g2d.drawString(text, x, y); 
    } 
} 
+0

Votre code fonctionne. Mais j'oublie de mentionner que j'utilise une classe qui étend Canvas et la taille est (480,272), et enfin un Jpanel dans un autre calss affiche la toile. J'ai essayé de le changer en JFrame, j'ai fait tous les changements requis mais la réutilisation était une fenêtre de couleur blanche. (Les arrière-plans ont été mis en jaune dans le constructeur) – nask00s

+0

@ nask00s - d'accord, avez-vous confirmé le positionnement et la taille de votre toile? Vous pouvez le faire en définissant l'arrière-plan sur quelque chose qui n'est pas utilisé dans vos autres composants de l'interface utilisateur (par exemple, le vert citron), puis en exécutant votre programme. Si la position et la taille apparaissent après cela, vous savez qu'il s'agit d'un problème de gestionnaire de disposition. – Perception