Java 使用 ArrayList 中的数据绘制字符串

发布于 2024-12-25 07:02:43 字数 854 浏览 2 评论 0原文

我有一个 java Graphics 问题,我正在编写一个程序,它读取一个文本文件并显示一些结果。

例如:

文本文件

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

所需结果 2 屏幕上显示的字符串。 但我只拿最后一张。

我的代码示例:

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); 
}

如何更改它以便它可以同时显示它们?

I have a problem with java Graphics, I m writting a program which takes reads a text file and displays some results.

For example:

Text File

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

Desired result 2 Strings displayed on the screen.
But I only take the last one.

A sample of my 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); 
}

How can I change it so it can display both of them?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

酒中人 2025-01-01 07:02:43

您的边界框可能太小。试试这个,看看它是否适合你:

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);
    }
}

You're bounding box might be too small. Try this and see if it works for you:

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);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文