我遇到了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);
}
如何更改它以便显示它们?
答案 0 :(得分:0)
你的边界框可能太小了。试试这个,看看它是否适合你:
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);
}
}