Solved

Reading files when entered in parameter box

Posted on 2003-12-03
6
303 Views
Last Modified: 2010-03-31
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
Comment
Question by:k1ngp1n99
  • 4
  • 2
6 Comments
 

Author Comment

by:k1ngp1n99
ID: 9870070
graphics file contains.

LINE 250 500 100 100
CIRCLE 50 100 75
END

0
 
LVL 92

Expert Comment

by:objects
ID: 9870090
String filename = JOptionPane.showInputDialog(jWindow, "Enter filename:");
0
 
LVL 92

Expert Comment

by:objects
ID: 9870101
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!

 
LVL 92

Accepted Solution

by:
objects earned 125 total points
ID: 9870119
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
 

Author Comment

by:k1ngp1n99
ID: 9873427
Thanks for the solutions objects.
0
 
LVL 92

Expert Comment

by:objects
ID: 9877102
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
hibernate example issues from command prompt 10 63
Unhandled exception type Exception 18 52
Detect Closed Loops (circles, figure-8s, etc) in PNG Images 6 74
Java Eclipse Loop 3 28
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question