我是java中的新手。我正在从事一项任务,我正在从文件中读取命令并在jpanel上绘制图形。 的文件就像java中的自定义绘制函数
FRAME 200 100 // open a frame, note: parser must ignore any comments
COLOR 255 0 0 // set color to red
RECTANGLE 20 30 40 20 // draw a red rectangle
COLOR 128 128 128 // set color to gray
CIRCLE 100 50 25 // draw a gray circle
FRAME 100 100 // open a second frame
COLOR 0 0 255 // set color to blue
ELLIPSE 50 50 30 20 // draw a blue ellipse
COLOR 0 255 0 // set color to green
LINE 10 20 90 80 // draw a green line
我写了下面的代码。
JFrame frame = new JFrame("JFrame Source Demo");
frame.setTitle("Drawing Graphics in Frames");
frame.setBounds(100,50,500,300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
///////////////////
File file = new File("Paint_instruction.txt");
// Get data from this file using a file reader.
FileReader fr = new FileReader(file);
// To store the contents read via File Reader
BufferedReader br = new BufferedReader(fr);
//writer to write in file
while((data = br.readLine()) != null)
{
/*String[] tokens = data.split("\\s{1,}");
for(int i=0;i<tokens.length;i++)
{System.out.println(tokens[i]);}*/
String []tokens = data.split("\\s{1,}");
if(tokens[0].equals("FRAME"))
{JFrame frame2 = new JFrame("JFrame Source Demo");
frame2.setBounds(0, 0, 40, 40);
frame2.setVisible(true);
}
else if(tokens[0].equals("RECTANGLE"))
{
//draw circle
}
else if(tokens[0].equals("CIRCLE"))
{
//draw circle
}
else if(tokens[0].equals("LINE"))
{
//draw Line
}
else if(tokens[0].equals("ELLIPSE"))
{
//draw ELLIPSE
}
我不知道如何使用绘图函数来绘制这些形状。我试图将数组传递给绘制函数。但它不起作用。 我有它的镜头,但没有找到任何合适的帮助
你应该尽量让你自己你的任务看official tutorial for paint mechanism。这样你会好很多。 – skuntsel
我建议,你不应该从文件中读取,而任何paint方法中,只要你想,这些是一样快,你可以把它 – MadProgrammer