Simple program to sketch graph in JAVA

In summary, the problem is that the ptolemy.plot.* package does not exist, and so the compiler cannot recognize statements like PlotFrame.
  • #1
CAF123
Gold Member
2,948
88

Homework Statement


See code below:

The Attempt at a Solution


I am simply trying to get the code to work: When I try to compile, the terminal says that apparently the package ptolemy.plot.* package does not exist and so does not recognise statements like PlotFrame etc.. Why is this?

Code:
import java.io.Console;
import java.io.*;
import java.lang.Math;
import java.lang.Boolean;
import ptolemy.plot.*;

public class Edisc {
	public static void main (String args[]) {
	
	Console.myConsole = System.console();

	Plot disc = new Plot();
	disc.setTitle("Graph of the axial E field from a charged disc");
	disc.setXLabel("distance along z");
	disc.setYLabel("E field");

	PlotFrame myFrame = new PlotFrame("E vs z", disc);
	myFrame.setSize(800,600);

	int dataSet = 0;
	
	double q = 6.0e-9;
	double R  = 10;
	double eps = 8.85e-12;
	
	
	
	int points = 100;

	double E[] = new double[points];
	double z[] = new double[points];
	
	for(int i = 0; i < (points-1); i++) {
	z[i] = (100*(int)i/points); 
	
	double Eqn = q*((2*(Math.PI())*eps*(Math.pow(R,2))))^(-1)*(1 - z[i]*((Math.sqrt((Math.pow(R,2)) + (Math.pow(z[i],2))))))^(-1);

	disc.addPoint(dataSet, z[i], Eqn, true);

	myFrame.setVisible(true);
	}

} 

}
 
Physics news on Phys.org
  • #2
The problem is that I didn't realize I had to install the package. I have downloaded the compressed zip file (ptplot5.8) however, I am having some difficulties configuring the path name.

I have put this in the jdk file, but I am not sure where to put it from here. Are there any necessities as to where this file should go? I feel there must be since the path names I have tried still give me errors and there seems to be no help on the internet.

Many thanks.
 
  • #3
It's been more than 15 years since I did anything with Java, so I'm not very up-to-date on this language. As a guess, I would say that the Ptolemy package should go into the same directory as your other Java files, such as the ones you're already importing.

If you do a search on "configure java" that might bring up some information that you can use.
 
  • #4
Mark44 said:
It's been more than 15 years since I did anything with Java, so I'm not very up-to-date on this language. As a guess, I would say that the Ptolemy package should go into the same directory as your other Java files, such as the ones you're already importing.

If you do a search on "configure java" that might bring up some information that you can use.

Thanks Mark44, I tried this, but apparently when I tried to move the file there 'File not found or no read permission'
 
  • #5
if the ptolemy package is in a jar file then you can run your program:

java -cp ptolemy.jar;. Edisc

the -cp option adds the ptolemy.jar and current directory to the classpath

In general all my programs are part of a package so I'd suggest you do the same something like as the first line (and you need to move your program to src/my/cool/app/Edisc.java or however your code is setup):

package my.cool.app;
 
  • #6
One other note on your program:

In Java, its bad style to use caps for variables. Programmers use all caps for CONSTANTS, and CamelCase for class names with variables and methods always starting with a lowerCase letter.
 
  • #7
@jedishrfu:
So, I have the following classpath written at runtime:
javac -classpath .;C:\ProgramFiles\Java\jdk1.7.0_21\ptplot5.8\plot\plot.jar Edisc.java

Can you tell me what is wrong with this? I have spent the whole morning today trying to configure this package and the compiler just won't except it. I have spoken to another person as well and they are lost too (after trying things for most of the morning)

Many thanks.
 
  • #8
The first thing I see is the javac for runtime it should be just java

java -cp .;"C:\Program Files\Java\jdk1.7.0_21\ptplot5.8\plot\plot.jar" Edisc

the javac command is used for compiling and java for running.

On Windows doesn't "Program Files" have an embedded space in it? This always causes problems with commandlines as they are parsed using spaces as the argument separator unless quotes are used.

One other caveat, on Linux and other Un*x machines single quotes and double quotes have very different behaviors that you should be aware of. Basically double quotes allow you to use environment parameters within the quotes whereas single quotes doesn't.

so for example:

echo "Hello $USER" would echo the user account id whereas echo 'Hello $USER" would echo the string "Hello $USER"
 
Last edited:
  • #9
I managed to get it compiled and it is now running. One problem I am having now is that the graph is being instantaneously created and then immediately disappearing altogether.

Can you see any indications in the code below which might be the cause of this? I put in a System.exit(0) statement because I was losing control of the terminal after running the program. However, this seems to make the graph disappear (see comment in code at end).

Code:
import java.io.Console;
import java.io.*;
import java.lang.Math;
import java.lang.Boolean;
import ptolemy.plot.*;

public class Edisc1 {
	public static void main (String args[]) {
	
	Console myConsole = System.console();

	Plot disc = new Plot();
	disc.setTitle("Graph of the axial E field from a charged disc");
	disc.setXLabel("distance along z");
	disc.setYLabel("E field");

	PlotFrame myFrame = new PlotFrame("E vs z", disc);
	myFrame.setSize(10,20);

	int dataSet = 0;
	
	double q = 6.0e-6;
	double R  = 10;
	double eps = 8.85e-12;
	
	
	
	int points = 500;

	double E[] = new double[points];
	double z[] = new double[points];
	
	for(int i = 0; i < (points-1); i++) {
	z[i] = (100*(int)i/points); 
	
	double Eqn = (q/((2*(Math.PI)*eps*(Math.pow(R,2))))*(1 - (z[i]/(Math.sqrt((Math.pow(R,2)) + (Math.pow(z[i],2)))))));

	disc.addPoint(dataSet, z[i], Eqn, true);
	
	
	}
	System.out.println("Graph created");
	myFrame.setVisible(true);

	System.exit(0); //With this, I get no graph but access to terminal.  Without, I get graph, but no access.

	

	} 

}
 
  • #10
When you exit your program of course your graph will disappear.

Why not run it as a background process with the & at the (works for macos and linux)
 
  • #11
As you note, the graph is being created, and then quickly disappears as the program ends. One way to keep this from happening is to have the program wait for some input, using System.in.read(). This will prevent the program from ending until you enter something. It doesn't matter what you enter - all you're doing is making the program wait until you hit a key.
 
  • #12
jedishrfu said:
Why not run it as a background process with the & at the (works for macos and linux)

I am not sure I understand what you mean here. Could you elaborate?
EDIT: Do you mean write for the run command: java -cp... Edisc1 & ?

Mark44 said:
One way to keep this from happening is to have the program wait for some input, using System.in.read(). This will prevent the program from ending until you enter something. It doesn't matter what you enter - all you're doing is making the program wait until you hit a key.

Do you mean in place of 'System.exit(0);' I put in 'System.in.read();'? If so, I tried this and I have the compilation error: Unreported exception IOException; must be caught or declared to be thrown.

What does this mean?
 
Last edited:
  • #13
With respect the the ampersand, yes add it to the end and in Macos or Linux it will run your app in a separate process allowing you to use the command session. For windows use the start command as in:

start java -cp ...

and the windows prompt should come back

The trick Java programmers use is this code in the main method:

javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
Edisc1 app = new Edisc1();
}
});
 
Last edited:
  • Like
Likes 1 person
  • #14
jedishrfu said:
With respect the the ampersand, yes add it to the end and in Macos or Linux it will run your app in a separate process allowing you to use the command session. Not sure of the equivalent trick in windows.
I am familiar with what you describe regarding putting the & at the end since we use Linux in my university. However, I am using Windows at home. I tried what you describe and it doesn't appear to work on Windows. Is there any way to find out what the 'equivalent trick' is in Windows?

Edit: I just caught your edit - I'll try it out and get back. Thanks
 
  • #15
I used the command: start java -cp... and this created another window, (but I was unable to type in it) then...it disappears too. (as well as the graph).
What is going on?
(I didn't type in the code that you write in your previous post, starting from javax.swing...)
(Maybe I should also mention that I am having no problem with regaining access to the terminal - that's fine, provided I have the system.exit. It is when I remove this that I lose access to the terminal)

EDIT: Never mind, got it thanks!
 
Last edited:
  • #16
CAF123 said:
Do you mean in place of 'System.exit(0);' I put in 'System.in.read();'?
No, not in place of - before.

What you want to do is to keep the console window open long enough to be able to look at the graph. When you're done looking, press any key.

The code would look something like this:
Code:
char tmp = (char) System.in.read();
This statement reads all of the keys pressed until the Enter key is pressed, and will store the first character read into tmp. You don't really care about the character - the code just causes your Main function to wait for user input.
CAF123 said:
If so, I tried this and I have the compilation error: Unreported exception IOException; must be caught or declared to be thrown.

What does this mean?
 
  • #17
Mark44 said:
No, not in place of - before.

What you want to do is to keep the console window open long enough to be able to look at the graph. When you're done looking, press any key.

The code would look something like this:
Code:
char tmp = (char) System.in.read();
This statement reads all of the keys pressed until the Enter key is pressed, and will store the first character read into tmp. You don't really care about the character - the code just causes your Main function to wait for user input.

Actually this is the old fashioned way to keep a window open, Java programmers now use the swing technique I mentioned which has the added advantage of delayed window opening to let the program initialization quiesce first.
 
  • #18
It's been a good 15+ years since I did any Java programming...
 
  • #19
Mark44 said:
It's been a good 15+ years since I did any Java programming...

I like that. I first learned Java at work when it first came out using Java 1.0 December of 1995.
 
Last edited:
  • #20
jedishrfu said:
I like that. I first learned Java at work when it first came out using Java 1.0 December of 1995.
Right about then is when I was doing my Java coding. As I recall, there wasn't much in the way of debugging capabilities at the time, and there weren't any of the extension libraries, like Swing and such.

I wrote about a half-dozen applets that ran in web pages, that created images based on user input.
 
  • #21
Mark44 said:
Right about then is when I was doing my Java coding. As I recall, there wasn't much in the way of debugging capabilities at the time, and there weren't any of the extension libraries, like Swing and such.

I wrote about a half-dozen applets that ran in web pages, that created images based on user input.

There was one feature of Java I really liked: println statements would appear in the console window while the GUI app ran in a separate window and that's how I would debug apps.

I also liked the simplicity of applets but then the push for server-side apps took off and applets were largely sidelined because each browser might implement the applet framework differently.

The processing IDE brings back some of that simplicity (see processing.org) as applications that run natively.
 

FAQ: Simple program to sketch graph in JAVA

What is a simple program to sketch graph in JAVA?

A simple program to sketch graph in JAVA is a computer program that uses the JAVA programming language to create a visual representation of data in the form of a graph.

2. What are the benefits of using JAVA to sketch a graph?

JAVA is a widely used programming language that is known for its versatility and ease of use. By using JAVA, you can create dynamic and interactive graphs with various customization options. Additionally, JAVA is platform-independent, which means that the program can be run on any device that has a JAVA Virtual Machine installed.

3. What are the key components of a graph in a JAVA program?

The key components of a graph in a JAVA program are the data points, axes, labels, and any additional elements such as legends or annotations. The data points represent the values being plotted, the axes provide the scale and orientation of the graph, and the labels help to identify the data points and provide context for the graph.

4. Can a simple program to sketch graph in JAVA be customized?

Yes, a simple program to sketch graph in JAVA can be customized to suit your specific needs. You can change the color, size, and style of the graph elements, add different types of graphs (such as bar or pie charts), and modify the data points and labels. The level of customization will depend on your programming skills and the capabilities of the JAVA library you are using.

5. Are there any libraries or resources available for creating graphs in JAVA?

Yes, there are several libraries and resources available for creating graphs in JAVA. Some popular options include JFreeChart, Chart.js, and JavaFX Charts. These libraries offer a wide range of features and customization options for creating high-quality graphs in JAVA. There are also many online tutorials and forums available to help you learn and troubleshoot any issues you may encounter while creating your graph.

Similar threads

Replies
12
Views
2K
Replies
1
Views
1K
Replies
1
Views
2K
Replies
4
Views
2K
Replies
5
Views
2K
Replies
2
Views
2K
Replies
3
Views
2K
Replies
16
Views
13K
Back
Top