Solved

Reading files when entered in parameter box

Posted on 2003-12-03
6
291 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
numbers ascending pyramid 101 198
Java Loop 6 62
MySqlDump not dumping triggers 1 33
ejb on wildfly 5 20
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
The viewer will learn how to implement Singleton Design Pattern in Java.

825 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