I am developing using Java SE on NetBeans 7.3.1 on Windows 7.
My java main method has the following calls
static Vector<Point2D> acceptedByFilter, coords;
// Some code to read the coords from a file
// Some code to filter coords and produce a subset called acceptedByFilter
DisplayInputPoints();
DisplayPointsAcceptedByFilter();
These methods are defined as follows
static protected void DisplayPointsAcceptedByFilter(){
Panel panel=new Panel();
panel.DisplayInputPoints(acceptedByFilter, xMin, xMax, yMin, yMax, true, "Points accepted by filter");
}
static void DisplayInputPoints(){
Panel panel=new Panel();
panel.DisplayInputPoints(coords, xMin, xMax, yMin, yMax, true, "Original Points");
}
Panel.DisplayInputPoints is defined as follows
import javax.swing.JFrame;
import javax.swing.JPanel;
public
class Panel extends JPanel
{
public static void DisplayInputPoints(Vector<Point2D> coords, double xMin, double xMax, double yMin,
double yMax, boolean invert, String label){
JFrame frame = new JFrame(label);
Panel panel = new Panel();
panel.setPreferredSize(new Dimension((int)Math.round(xMax-xMin) + 10,
(int)Math.round(yMax-yMin) + 10));
panel.loadPoints(coords);
frame.setContentPane(panel);
frame.pack();
frame.setVisible(true);
frame.repaint();
}
}
When I call
DisplayInputPoints();
the first frame appears and it displays the points in the coords vector. When I call
DisplayPointsAcceptedByFilter();
I get another frame and the points in the acceptedByFilter appear in both frames. That is, the first frame gets overwritten by the display that is in the second frame.
What is the best way to stop the first frame being overwritten by what is supposed to be only in the second frame?
Your code structure appears to be way off, and that is likely your main problem. For instance,
You shouldn't be using static methods or fields for one thing. The only static methods should be the main and associated methods and utility methods. The only static fields should be class fields such as constants.
Your DisplayPointsAcceptedByFilter creates a Panel object which causes the 2nd JFrame to display. Get the display code out of the Panel class, it doesn't belong there and is causing your problem.
Don't name a class with the same name as a Java core class, such as Panel.
Your Panel class isn't even a real OOP class, but nothing but a holder of a static method. This flies against all OOP rules.
If this were my code, I'd scrap it and re-write it using OOP principles from the get-go.
For more detailed and better help, consider supplying more information in your question and perhaps creating and posting an sscce.
Edit
Consider creating:
A class that reads in the coordinates from a file. It can then put the coordinates into an ArrayList. It should have no static methods or fields.
A class for filtering the coordinates. It also should have no static methods or fields and no GUI code.
A GUI class that extends JPanel that is for displaying the data.
The GUI class accepts an array list of coordinaates and then somehow draws the coordinates.
The GUI class accepts new coordinates with a setter method, and then will re-draw the new coordinates.
A class with a main method that creates a JFrame for your GUI class to be displayed in, and that orchestrates all your other classes, gets them started.
Have you thought about making Panel an abstract class, and perhaps altering it specifically for two purposes? I mean i feel like there's more code you should show to properly understand the problem
Related
How can I embed a PApplet into a JApplet ?
I wanted to add it to a JPanel inside the applet, but I couldn't.
If any of you know how I can do this. ??
As of Processing 3, you can no longer do this. PApplet no longer extends Applet, so it can't be treated as a component.
For 95% of users, this is okay. Applets are dead anyway, so you really shouldn't be using them. If at all possible, you should try deploying with Processing.js.
If you need to execute a Processing sketch from Java code, then you should use the PApplet.main("YourSketchNameHere"); function to launch it. Processing will take care of the window for you.
If you really need to treat a PApplet as a component, then you're going to have to go through its PSurface. The PSurface class contains a getNative() function that returns an object that can be treated as a component. But that's overkill for most Processing users.
Prior to Processing 3, this code should have worked for you as #Kevin has explained. So, if your question is directed towards understanding a legacy code here is what you will need to know:
import javax.swing.JFrame;
import javax.swing.JPanel;
class MyPApplet extends PApplet implements ActionListener{
#Override
public void setup() {
super.setup();
// setup
}
#Override
public void draw() {
// my draw code
}
}
public class PAppletDemo {
public static void main(String[] args) {
final JFrame frame = new JFrame("PApplet in Java Application");
JPanel panel = new JPanel();
//create an instance of your processing applet
final MyPApplet applet = new MyPApplet();
applet.init();
panel.add(applet); // From processing 3, this will give you error that applet is not a Component
frame.add(panel);
frame.setSize(applet.getSize().width, applet.getSize().height +200);
frame.setVisible(true);
}
}
To circumvent this, you will need to use PSurface getNative() function. Please refer to the example and discussion given on this link.
Since I'm new I can't post more than two links, but this is an x-post from reddit.com/r/learnprogramming, just for full disclosure.
I'll basically just be pasting what I said there to here. Thanks for your help, if you can help.
I'm writing somewhat of a graphing application. I currently only have it able to graph sin(x), but that's not the point of this question. I am not able to draw to my main panel. Here is what it currently looks like.
I had an overridden paint function in my Window.java class, which drew the sin(x) function and the axes, but when I made an inner class which extended JPanel(), it would no longer draw.
I then tried to make a separate file, but that didn't draw anything either.
What could be preventing it from drawing?
Here are all my files in question.
edit: code in question:
GraphDraw.java:
//import stuff
Public class GraphDraw extends JPanel {
SinX sinx = new SinX();
GraphPanel p = new GraphPanel();
#Override
public void paintComponent(Graphics gc) {
super.paintComponent(gc);
Graphics2D g = gc;
p.paintComponent(g);
sinx.paint(g);
}
}
And in Window.java, I initialize GraphDraw and add it to my main panel, which is underneath the buttons in the picture and above the x/y min/max labels.
GraphDraw drawer = new GraphDraw();
/*
GUI code
*/
mainPanel.add(drawer);
SinX.java
//import stuff
public class SinX extends Component {
public void paint(Graphics g) {
g.setColor(Color.red);
for(double x=-400;x<=400;x=x+0.5) {
double y = 50 * sin(x*((Math.PI)/180));
int Y = (int)y;
int X = (int)x;
g.drawLine(400+X,300-Y,400+X,300-Y);
}
}
}
First, before anything else, do the following:
Change you object from Component to JComponent
Do not ever, ever call paintComponent() or paint() on a graphics object from swing or awt, use object.repaint(); (For reasons I won't go into here, because it's long and complicated)
From there I would try calling setVisible(true); on all your objects. If you are getting this code from a tutorial, then stop and use a different tutorial. You need to learn how swing and the AWT library work before you can start making user interfaces. Nobody uses AWT anymore because Swing is much better. For reasons why, look at the following page. If you are too lazy to do that, its because it's more optimized and more powerful.
What is the difference between Swing and AWT?
I have a weird Problem with my Java GUI.
I can access the Methods in the Main Class from another Class but i cannot access the Swing Components.
Let me show you how i built the whole thing
Main Class:
public class GUI extends JFrame {
static Code c = new Code();
static Draw panel = new Draw();
JTextArea codelog;
JLabel lblFile;
...
...
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
GUI frame = new GUI();
frame.create();
}
});
}
public void create() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(1280,720);
...
...
contentPane = new JPanel();
setContentPane(contentPane);
contentPane.setBackground(Color.DARK_GRAY);
GridBagLayout gbl_contentPane = new GridBagLayout();
setResizable(false);
...
...
panel.setBackground(Color.BLACK);
gbc_panel.fill = GridBagConstraints.BOTH;
gbc_panel.gridx = 1;
gbc_panel.gridy = 1;
contentPane.add(panel, gbc_panel);
codelog = new JTextArea();
codelog.setEditable(true);
JScrollPane scrollPane_1 = new JScrollPane(codelog);
codelog.setLineWrap(true);
scrollPane_1.setVerticalScrollBarPolicy(
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
codelog.setVisible(true);
scrollPane_1.setVisible(true);
GridBagConstraints gbc_scrollPane_1 = new GridBagConstraints();
gbc_scrollPane_1.gridheight = 2;
gbc_scrollPane_1.gridwidth = 4;
gbc_scrollPane_1.fill = GridBagConstraints.BOTH;
gbc_scrollPane_1.gridx = 8;
gbc_scrollPane_1.gridy = 1;
contentPane.add(scrollPane_1, gbc_scrollPane_1);
...
...
}
public void refresh(){
panel.repaint();
}
}
I am using static Code c and static Draw panel to avoid multiple instances as i also have to create Objects of the Main class in other classes.
The other Class named Code
public class Code {
...
...
static GUI g = new GUI();
String test;
...
...
public void hpgl(){
g.codelog.append(test); // gives me nullPointerException !!
g.refresh // works
...
...
}
}
The Problem is that i can access the Methods of the Main Class (GUI) from other classes (such as Code) but i cannot access the Components (such as JTextArea).
The refresh() Method in the Main Class proves it. I can access the Method and in the Main Class the repaint() works. But if i try to repaint from another class using GUI.panel.repaint() it won't work because i would in that case access the panel directly from Code Class.
The Same goes for the JTextArea. I am trying to append codelog from Code but it won't let me do it. If i create a Method in Main Class which appends the Textarea and then call the Method from Code Class it works. But using g.codelog.append(test) gives me a Java null pointer exception
So i can access the Methods but i cannot access the Swing Components.
Can you guys please help me. I don't want to have to write an extra Method in the Main Class for every single Swing Component i want to modify.
Thank You
The UI which is visible on the screen is not the same UI you have created in your Code class. If you want Code to be able to access the UI properties, you will need to pass a reference of the GUI to it.
Having said that, I would be apposed to exposing the UI components directly to any class an instead provide getters and setters (where applicable) to provide access to the information been managed. This prevents rouge classes from making changes to the UI which it should be allowed to do (like remove components)
Depending on what you are doing, an Observer Pattern might be a better choice, where by Code is notified by GUI when something it might be interested in changes. If done through the use of interfaces, this will reduce the coupling between your classes and make it more flexible
Beware static is not a mechanism for providing cross object communication and should be avoid if at all possible, especially in something as dynamic as a GUI.
I was able to solve the Problem following MadProgrammer's Suggestion.
This is what i changed.
I have 3 Classes:
Main Class
Draw
Code
Main Class
public class GUI extends JFrame {
Draw panel = new Draw(this);
Code c = new Code(this);
...
...
}
Code Class
public class Code {
private GUI g;
private Draw b;
public Code(GUI g){
this.g = g;
}
...
...
}
Draw Class
public class Draw extends JPanel{
private GUI x;
private Code c;
public Draw(GUI x){
this.x = x;
}
...
...
}
I removed all the Static declarations. It is now working. I can access the Swing Components in the Main Class now.
Is this the Professional way to do it? or is there still room for improvement. This is the first time i used the passing reference way to do it. Until now i always used static Objects.
Thank You
SEE UPDATE AT THE BOTTOM!!
I've tried to figured out how to do this for a couple of days but so far I have had no luck.
Basically what I want to do is have a combobox, which when an option is selected loads an applet, and passes a value to the applet.
Here is the code for the ComboBox class, which is supposed to open the other class in a new window. The other class is the main class for an applet. They are both in the same project but in different packages. I know that there aren't any errors with the rest of the code.
//where I evaluate the selection and then open SteadyStateFusionDemo
// more selections just showing one code block
combo.addItemListener(new ItemListener(){
public void itemStateChanged(ItemEvent ie){
String str = (String)combo.getSelectedItem();
if (str.equals("NSTX")) {
machine = "A";
JFrame frame = new JFrame ("MyPanel2");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
SteadyStateFusionDemo d = new SteadyStateFusionDemo();
frame.getContentPane().add (new SteadyStateFusionDemo());
d.init();
frame.pack();
frame.setVisible (true);
And just to cover everything here is the beginning of the init() method of SteadyStateFusionDemo as well as the main method in the class. Too much code to post otherwise. There are several different privates before the init method.
//method that initializes Applet or SteadyStateFusionDemo class
public void init() {
//main method of the SteadyStateFusionDemo class
public static void main (String[] args) {
JFrame frame = new JFrame ("MyPanel");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add (new SteadyStateFusionDemo());
frame.pack();
frame.setVisible (true);
What am I doing wrong? Why doesn't my class load?
UPDATED: Changed the code so that a JFrame opens and then the JApplet loads inside. I have successfully done this in a test Java applet but for some odd reason it won't work with this Applet. I even set up the test in a similar way (The code for this is virtually the same, except with different class names, and of course a much, much shorter init() method). Can someone help me figure out why this isn't working? Also, a JFrame will open if I delete the lines referring to SteadyStateFusionDemo, but once I reference it won't work. Why does this happen?
UPDATE:
Based on your feedback, it seems you are trying to achieve the following:
Use the code of an existing Applet (found here) in a Desktop application (i.e. in a JFrame).
Converting an Applet to a Desktop application is an "undertakable" task, the complexity of which depends on how much "Applet-specific" stuff is used by the Applet. It can be as simple as creating a JFrame and adding myFrame.setContentPane(new myApplet().getContentPane()); or as complex as...hell.
This tutorial might be a good place to start.
After taking a look at the Applet at hand, it seems to be fairly easy to convert it. The only complicating factor is the use Applet's methods getCodeBase() and getImage(URL) (somewhere in the code). These two methods result in a NullPointerException if the Applet is not deployed as...an Applet.
So, what you can do is override those two methods in order to return the intended values (without the exception). The code could look like this:
/* Import the necessary Applet entry-point */
import ssfd.SteadyStateFusionDemo;
/* Subclass SSFD to override "problematic" methods */
SteadyStateFusionDemo ssfd = new SteadyStateFusionDemo() {
#Override
public URL getCodeBase() {
/* We don't care about the code-base any more */
return null;
}
#Override
public Image getImage(URL codeBase, String imgPath) {
/* Load and return the specified image */
return Toolkit.getDefaultToolkit().getImage(
this.getClass().getResource("/" + imgPath));
}
};
ssfd.init();
/* Create a JFrame and set the Applet as its ContentPane */
JFrame frame = new JFrame();
frame.setContentPane(ssfd);
/* Configure and show the JFrame */
...
The complete code for an example JFrame Class can be found here.
Of course, you need to have all Classes from the original Applet accessible to your new Class (e.g. put the original Applet in your classpath).
I have a piece of code here and I'm not sure what a couple of commands do:
//this bit is the body of the main method
//lots of stuff omitted
project frame = new project();
frame.createGUI();
private void createGUI() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
panel = new JPanel();
}
So when frame.createGUI(); is called, what happens with the two commands in createGUI();?
Are they
setDefaultCloseOperation(EXIT_ON_CLOSE);
panel = new JPanel();
or
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.panel = new JPanel();
or is it something else entirely?
I'm quite new at Java, started year 12 Computer Science this year, but am a bit ahead of class.
Thanks for your time!
Every non static method in Java has an hidden parameter called this. It's value is the object in front of the dot in the invocation. Inside the body of methods, invocations of other methods implicitly have this. in front of them. So does the use of variables, if they cannot be resolved locally. If the this reference was explicit, your method would look like this:
private void createGUI(project this) {
this.setDefaultCloseOperation(this, EXIT_ON_CLOSE);
this.panel = new JPanel();
}
In the invocation of createGUI in your main method, what is really happening is something like this:
frame.createGUI(frame);
When running createGUI the this reference is a reference to frame.
setDefaultCloseOperation(EXIT_ON_CLOSE);
Sets the operation that happens when the user closed the Window (JFrame). The method is for JFrame. If the class extends JFrame then setDefaultCloseOperation(EXIT_ON_CLOSE); is fine since it will call the method of JFrame if not then it should be frame.setDefaultCloseOperation(EXIT_ON_CLOSE); but this will work if project extends JFrame
panel = new JPanel();
This creates a new panel.
frame.createGUI(); means that your object frame which u defined a line above calls the method createGUI()
setDefaultCloseOperation(EXIT_ON_CLOSE); this means that when the user click X button in the left top corner of the window will close the that window.
you can experiment a bit and delete setDefaultCloseOperation(EXIT_ON_CLOSE); from the createGUI() method and see what happens
if u want to learn more, look here
Your two alternatives are really one and the same. In both cases, you are calling methods of the frame object you created. From within a method of the class, you don't have to specifically say: this.doSomethimg() or this.myField = 123, you can just write doSomething() or myField = 123. From methods outside the class, you have to indicate for which instance you want the method called.