I have main JFrame which contains a JPanel and all component on it. I've drawn a diagram on my main Jframe, I've used getGraphics() method to draw shapes on JPanel.I want to convert it into another diagram so the new diagram display on new Jframe but I am not able to draw any shapes on panel of newly created frame .(I've also try to use same getGraphics() method to draw as I've used in main Jframe).
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Line2D;
import javax.swing.JButton;
import javax.swing.JPanel;
public class Transformation extends javax.swing.JFrame {
private JPanel myPanel;
public Transformation() {
this.setSize(500, 500);
this.setVisible(true);
myPanel = new JPanel(new FlowLayout());
myPanel.add(new JButton("Hello"));
this.getContentPane().add(myPanel);
Graphics2D g = (Graphics2D) myPanel.getGraphics();
g.draw(new Line2D.Double(20, 50, 100, 200) );
}
}
in main Jframe I've called following line in a Jbutton's action listener.
Transformation tfm = new Transformation();