how to dock javaplot window to a JPanel in java - java

I am using Javaplot for plotting graph.
import com.panayotis.gnuplot.JavaPlot;
public class test {
public static void main(String[] args) {
JavaPlot p = new JavaPlot();
p.addPlot("sin(x)");
p.plot();
}
}
The above code plots sin(x) on a dedicated window. I have a custom application window in my java swing project. How do I dock the java plot output graph inside a JPanel in my application window?

I haven't tried it, but it looks like you can wrap your JavaPlot in a JPlot, a subclass of JPanel, and add the panel to your custom application window:
customApplicationWindow.add(new JPlot(p));
For reference, interposing a call to JPlot#plot() worked:
JPlot jplot = new JPlot(p);
jplot.plot();
customApplicationWindow.add(jplot);

Related

Why is my JFreeChart rendering improperly?

I am trying to build a simple JFreeChart XYLineChart object, and embed it into a ChartPanel object.
For some unknown reason, the plot area doesn't look properly: You can see how gridlines are inconsistent in thickness, and the edges of the plot have these thick black markings in random places. What could be the cause of this?
public class Main extends JFrame() {
public static void main (String [] args) {
ECGPanel myECGPanel = new ECGPanel();
this.add(myECGPanel);
}
}
public class ECGPanel extends Jpanel {
lineChart= ChartFactory.createXYLineChart("ECG", "Time(ms)", "Voltage(mV)", dataset,
PlotOrientation.VERTICAL, true, false, false);
chartPanel=new ChartPanel(lineChart);
chartPanel.setPreferredSize(new Dimension(1000,400));
this.add(chartPanel);
}
As it was pointed out in comments, the problem relates to Windows and its Display settings. I have found the discussion about how Swing behaves on higher DPI systems here
Interestingly, I don't have this problem when I create a JFreeChart in JavaFX (both using a SwingNode and when using a ChartViewer). You can also see read about that here

Embed PApplet intp JApplet

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.

Embed processing 3 into swing

I'm trying to integrate Processing 3 into a swing application, but because PApplet doesn't extend Applet anymore I can't just add it as a component right away.
Is there anyway of embeding a Processing 3 sketch into Swing, it would be enough if I could just open the sketch in a seperate window without the PDE.
You can run a sketch from Java by extending PApplet and then using the runSketch() function to run that PApplet. It'll look something like this:
String[] args = {"MyPapplet "};
MyPapplet mp = new MyPapplet ();
PApplet.runSketch(args, mp);
public class MyPapplet extends PApplet {
public void settings() {
size(200, 100);
}
public void draw() {
background(255);
fill(0);
ellipse(100, 50, 10, 10);
}
}
Then if you want to get at the underlying component, you have to write code that depends on which renderer you're using. Here's how you'd do it with the standard renderer:
PSurfaceAWT awtSurface = (PSurfaceAWT)mp.surface;
PSurfaceAWT.SmoothCanvas smoothCanvas = (PSurfaceAWT.SmoothCanvas)awtSurface.getNative();
Once you have the SmoothCanvas, you can remove it from its frame and add it to yours.

Create a video using processing opengl

I am creating a REST API to generate video dynamically based on user input and provided animation types. So, I am using processing 2.2.1 for this.
I want to generate 3D animation using OPENGL. But OPENGL requires Window object. Since I am using processing in background to just generate frame, how can I use processing with OPENGL to generate animated frames without interactive means without displaying the window.
My sample code
import com.hamoid.VideoExport;
import processing.core.*;
public class CircleSketch extends PApplet {
private VideoExport videoExport;
public void setup() {
size(400, 400,OPENGL);
videoExport = new VideoExport(this, "F:/work/tmp.mp4");
background(0);
}
public void draw() {
background(0);
fill(200);
rotateX(radians(50));
rectMode(CENTER);
rect(width/2,height/2, 100, 100);
videoExport.saveFrame();
}
}
DisplayFrame Class
public class DisplayFrame extends javax.swing.JFrame {
public DisplayFrame(){
this.setSize(600, 600); //The window Dimensions
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
javax.swing.JPanel panel = new javax.swing.JPanel();
panel.setBounds(20, 20, 600, 600);
processing.core.PApplet sketch = new CircleSketch();
panel.add(sketch);
this.add(panel);
this.setVisible(false);
//this is the function used to start the execution of the sketch
sketch.init();
}
public static void main(String[] args) {
new DisplayFrame().setVisible(false);
}
}
Any suggestion please???
how can I use processing with OPENGL to generate animated frames without interactive means without displaying the window.
In short terms: Given the current OpenGL driver models you can't (as long as you want to use a GPU for rendering). Period, that's how it is.
If you can live with software rendering (slow), then there's OSMesa. Also in the forseeable future there should be changes in the driver models, which allow to use OpenGL in a headless environment. Until then, you need a window of some sort on a graphics environment that actually actively feeds a display output (so it doesn't suffice to start a X11 server and background it).

Trying to run Program in Ecplise as a class × 259989

I am a newbie in the Java world and this is among my first programs (Hello World, obviously!). The problem is when I try to run this program as a class from the menu with a play icon on it, a blank window shows up with no "Hello World" on it (just white colour filling the window). At the bottom border of this window there is a black thick line. But when I run this program as an applet from the same menu everything is okay and the "Hello World" shows at the right position and everything is fine. But how can I make the program run regularly as a class???
This is my code...
import acm.graphics.*;
import acm.program.*;
import java.awt.*;
public class HelloProgram extends GraphicsProgram {
private static final long serialVersionUID = 1L;
public void run() {
GLabel label = new GLabel("hello, world", 100, 75);
label.setFont("SansSerif-36");
label.setColor(Color.RED);
add(label);
}
}
I have no idea about acm, but looked in this page: http://jtf.acm.org/tutorial/Introduction.html
As you see there GraphicsProgram is a subclass of JApplet and because of that it's supposed to be run as applet, not desktop application.
Try this. It will be a separate class, but you will be able to run your program from this one. Please let me know if you are confused.
public class Runner{
public static void main(String[] args){
HelloProgram p = new HelloProgram();
p.run();
}
}
I agree with publ1c_stat1c, your program is missing the "main" method for it to be considered a standalone application.
Create an instance of your application
HelloProgram hello = new HelloProgram();
call the run method of the instance
hello.run();
the main method doesn't have to be in a different class, try adding below codes in-between "run method" and "private static final long serialVersionUID = 1L;"
public static void main(String[] args){
HelloProgram hello = new HelloProgram();
hello.run();
}

Categories