java - How to put JFrame with jmathplot graph in JPanel -
note: question follow question this question.
i have learned following last question:
jpanel
has flowlayout
(with same output nulllayout
! on resize), accepting preferredsize
, child aren't resizable container, required use borderlayout
/gridlayout
simple graph
a demonstration of principle given:
import java.awt.borderlayout; import java.awt.dimension; import javax.swing.jframe; import javax.swing.jpanel; import javax.swing.swingutilities; import org.math.plot.plot2dpanel; import java.awt.borderlayout; import java.awt.dimension; import javax.swing.jframe; import javax.swing.jpanel; import javax.swing.swingutilities; import org.math.plot.plot2dpanel; public class myplot { private jframe frame = new jframe("jmathplot library in swing application."); private jpanel panel = new jpanel(); public myplot() { double[] x = new double[]{0, 1, 2, 3, 4, 5}; double[] y = new double[]{10, 11, 12, 14, 15, 16}; plot2dpanel plot = new plot2dpanel() { @override public dimension getpreferredsize() { return new dimension(400, 200); } }; plot.addlineplot("my plot", x, y); // add line plot plotpanel panel.setlayout(new borderlayout()); panel.add(plot); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.add(panel); frame.pack(); frame.setlocation(150, 150); frame.setvisible(true); } public static void main(string[] args) { swingutilities.invokelater(new runnable() { @override public void run() { new myplot(); } }); } }
now want achieve integrate graph parent jpanel
use in swing application.
a visualisation of be:
how implement graph in 'parent' jpanel without violating constraints making work properly?
(i looked jinternalframe couldn't come correct implementation problem)
if jinternalframe indeed solution, how should use it? (please supply code)
i don't know if that's you're looking give source code, maybe you:
public class dz extends jframe { private jpanel contentpane; /** * launch application. */ public static void main(string[] args) { eventqueue.invokelater(new runnable() { public void run() { try { dz frame = new dz(); frame.setvisible(true); } catch (exception e) { e.printstacktrace(); } } }); } /** * create frame. */ public dz() { setdefaultcloseoperation(jframe.exit_on_close); setbounds(100, 100, 917, 788); contentpane = new jpanel(); contentpane.setborder(new emptyborder(5, 5, 5, 5)); setcontentpane(contentpane); springlayout sl_contentpane = new springlayout(); contentpane.setlayout(sl_contentpane); // define data double[] x = { 0, 1, 2, 3, 4, 5 }; double[] y = { 45, 89, 6, 32, 63, 12 }; // create plotpanel (you can use jpanel) plot2dpanel plot = new plot2dpanel(); // define legend position plot.addlegend("south"); // add line plot plotpanel plot.addlineplot("my plot", x, y); sl_contentpane.putconstraint(springlayout.east, plot, 600, springlayout.west, contentpane); sl_contentpane.putconstraint(springlayout.north, plot, 15, springlayout.north, contentpane); sl_contentpane.putconstraint(springlayout.west, plot, 15, springlayout.west, contentpane); sl_contentpane.putconstraint(springlayout.south, plot, 600, springlayout.north, contentpane); /* panel.setlayout(new gridbaglayout()); panel.add(plot); plot.validate(); plot.repaint(); plot.setbounds(50,50,100,100);*/ contentpane.add(plot); jpanel panel = new jpanel(); panel.setbackground(color.black); sl_contentpane.putconstraint(springlayout.north, panel, 20, springlayout.north, contentpane); sl_contentpane.putconstraint(springlayout.west, panel, 16, springlayout.east, plot); sl_contentpane.putconstraint(springlayout.south, panel, 408, springlayout.north, contentpane); sl_contentpane.putconstraint(springlayout.east, panel, 251, springlayout.east, plot); contentpane.add(panel); }
}
Comments
Post a Comment