Link to home
Start Free TrialLog in
Avatar of k1ngp1n99
k1ngp1n99

asked on

Reading files when entered in parameter box

I use a program called JPad pro when running a program it has a parameter box. My program currently runs a file called graphics which contains the data to create some shapes in a GUI. I would like a method to modify this, so that i can enter the file i.e "graphics.txt" etc. or other data files straight into the parameter box when running the program. Any other suggestions are also welcome.

import java.io.*;
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
import javax.swing.border.LineBorder;
import java.util.*;

public class Proto1 extends JComponent{

private static final int WIDTH=400;
private static final int HEIGHT=300;

private ArrayList shapelist = new ArrayList();
public Proto1(){

this.setPreferredSize(new Dimension(WIDTH,HEIGHT));
this.setBorder(new LineBorder(Color.red,10));

try{
String s;
BufferedReader in = new BufferedReader(new FileReader ("graphics.txt"));
while(!(s=in.readLine()).equals("END"))
{
StringTokenizer st = new StringTokenizer(s);

String tok;
tok = st.nextToken();

if(tok.equals("LINE"))
{
shapelist.add(new Line2D.Float(
Integer.parseInt(st.nextToken()),
Integer.parseInt(st.nextToken()),
Integer.parseInt(st.nextToken()),
Integer.parseInt(st.nextToken())));
}
else if(tok.equals("CIRCLE"))
{
shapelist.add(new Ellipse2D.Double(
Integer.parseInt(st.nextToken()),
Integer.parseInt(st.nextToken()),
Integer.parseInt(st.nextToken()),
Integer.parseInt(st.nextToken())));
}
}

in.close();
}catch(IOException e){ System.out.println(e.getMessage());}
}

/* The paintComponent method is called whenever the frame is refreshed. */
public void paintComponent(Graphics g){

super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g; // g is a Graphics2D object
for(int i=0;i< shapelist.size();i++)
{
Shape shape = (Shape) shapelist.get(i);
g2.setPaint(Color.blue);
g2.draw(shape);
g2.fill(shape);
repaint();
}

}

public static void main( String [] args ){

JFrame jWindow = new JFrame("Prototype 1");
jWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jWindow.getContentPane().add(new Proto1(),BorderLayout.CENTER);
jWindow.pack();
jWindow.setVisible(true);

}

}
Avatar of k1ngp1n99
k1ngp1n99

ASKER

graphics file contains.

LINE 250 500 100 100
CIRCLE 50 100 75
END

Avatar of Mick Barry
String filename = JOptionPane.showInputDialog(jWindow, "Enter filename:");
or u could use a JFileChooser:

JFileChooser Chooser = new JFileChooser();
if (JFileChooser.APPROVE_OPTION==Chooser.showOpenDialog(frame))
{
   // open file
}
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for the solutions objects.