-1

i am a newbie in java. i am working on an assignment in which i am reading commands from file and drawing the shapes on jpanel. the file is like

 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

i have written the following code.

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
           }

i do not know how to use paint function to draw these shapes. I have tried to pass array to paint function. but it did not work. I have goggled it but did not find any appropriate help

mKorbel
  • 109,525
  • 20
  • 134
  • 319

2 Answers2

4

and drawing the shapes on jpanel.

So where is your JPanel?

I have goggled it but did not find any appropriate help

I find that hard to believe. Links to the Custom Painting tutorial are found all over this and other forums.

It is harder to learn how to do painting when reading commands from a file, so I suggest that you first learn the basics of doing painting before you tackle painting from a command file.

camickr
  • 321,443
  • 19
  • 166
  • 288
1

You should override paintComponent method of JPanel and within that method draw all these shapes using java.awt.Graphics argument passed in paintComponent method. And add that JPanel to the JFrame. Look at the official tutorial for paint mechanism in java

Vishal K
  • 12,976
  • 2
  • 27
  • 38