Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 319
  • Last Modified:

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

}

}
0
k1ngp1n99
Asked:
k1ngp1n99
  • 4
  • 2
1 Solution
 
k1ngp1n99Author Commented:
graphics file contains.

LINE 250 500 100 100
CIRCLE 50 100 75
END

0
 
objectsCommented:
String filename = JOptionPane.showInputDialog(jWindow, "Enter filename:");
0
 
objectsCommented:
or u could use a JFileChooser:

JFileChooser Chooser = new JFileChooser();
if (JFileChooser.APPROVE_OPTION==Chooser.showOpenDialog(frame))
{
   // open file
}
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
objectsCommented:
or to pass the filename as a programe arg use:

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

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

try{
String s;
BufferedReader in = new BufferedReader(new FileReader (filename));

....

public static void main( String [] args ){

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

}

}

0
 
k1ngp1n99Author Commented:
Thanks for the solutions objects.
0
 
objectsCommented:
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now