Question

I am working at symbolic derivatives of concrete functions. I have three classes.

fnc.java represnets how to looks like derivatives individual functions. For example:

 public void der_cotg(Graphics g,int x, int y){
    drawBroken_line(g,"-1","sin  (x)",x+23,y); //putting pixels between x and y
    g.setFont(new Font("Arial", Font.BOLD,18));//Graphics g
    g.drawString("2", x+66, y+18);
}

Jmain.java is JFrame form, represents field, where we can see vizualization of derivatives functions. There are two labels

  • f(x) =
  • g(x) = ,

two panels next to them

and button that opens antother JFrame choose.java where you can choose function. It is consists of Jpanel and buttons(which represents concrete function) If you press one of theese button, concrete function will be shown at choose.java 's Panel.

I want to get Graphics from choose.java and set Jmain.java 's Panel. I know how to get and set textfields, but I have not found if it is possible with Panels.

Was it helpful?

Solution

As seen in this answer, You can create a BufferedImage of the JPanel contents. The example has PanelOne (which is a JPanel) being passed to a method of another JPanel (PanelTwo), and this is the method in PanelTwo that creates a BufferedImage from the PanelOne passed to it.

BufferedImage bi;
....
private void setImage(PanelOne panel) {
    Dimension d = panel.getPreferredSize();
    int w = (int)d.getWidth();
    int h =(int)d.getHeight();
    bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
    Graphics2D g = bi.createGraphics();
    panel.paint(g);
    g.dispose();
}
....
@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.drawImage(bi, 0, 0, 250, 250, 0, 0, 250, 250, this);
}

Here's the result. PanelOne is the left and passes itself to PanelTwo on the right

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top