Solved

Reading files when entered in parameter box

Posted on 2003-12-03
6
273 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
Comment Utility
graphics file contains.

LINE 250 500 100 100
CIRCLE 50 100 75
END

0
 
LVL 92

Expert Comment

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

Expert Comment

by:objects
Comment Utility
or u could use a JFileChooser:

JFileChooser Chooser = new JFileChooser();
if (JFileChooser.APPROVE_OPTION==Chooser.showOpenDialog(frame))
{
   // open file
}
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 92

Accepted Solution

by:
objects earned 125 total points
Comment Utility
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
Comment Utility
Thanks for the solutions objects.
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
java continue statement 10 70
countPairs challenge 7 57
where is session ID cache stored 1 38
word0 challenge 3 55
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

762 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now