JFrame Testapplikation mit drei JPanels. Ich möchte gerne die Inhalte der zwei Panels in den dritten Panel kopieren. Ich nehme dabei die Klasse BufferedImage zur Hilfe. Leider platziert er den zweiten Panel nicht bei den Koordinaten 0,0 des dritten Panels, obwohl explizit angegeben. Wer kann mir auf die Sprünge helfen.
anbei die vier Klassen JPanel1, JPanel2, JPanel3 und PrintPanels mit der main-Methode
public class JPanel1 extends JPanel {
BufferedImage buffPanel = new BufferedImage(200,255,BufferedImage.TYPE_INT_ARGB);
/**
* This is the default constructor
*/
public JPanel1() {
super();
initialize();
repaint();
}
/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setBounds(new java.awt.Rectangle(25,25,200,225));
this.setBackground(java.awt.Color.white);
}
public void paint(Graphics g) {
Graphics2D g2 = buffPanel.createGraphics();
g2.setColor(Color.WHITE);
g2.fillRect(0,0,200,225);
g2.setColor(Color.RED);
g2.fillOval(50,62,100,100);
g.drawImage(buffPanel, 0,0,this);
}
public BufferedImage getBuffPanel() {
return buffPanel;
}
}
public class JPanel2 extends JPanel {
BufferedImage buffPanel = new BufferedImage(200,255,BufferedImage.TYPE_INT_ARGB);
/**
* This is the default constructor
*/
public JPanel2() {
super();
initialize();
repaint();
}
/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setBounds(new java.awt.Rectangle(25,250,200,225));
this.setBackground(java.awt.Color.white);
}
public void paint(Graphics g) {
Graphics2D g2 = buffPanel.createGraphics();
g2.setColor(Color.WHITE);
g2.fillRect(0,0,200,225);
g2.setColor(Color.BLUE);
g2.fillOval(50,62,100,100);
g.drawImage(buffPanel, 0,0,this);
}
public BufferedImage getBuffPanel() {
return buffPanel;
}
}
public class JPanel3 extends JPanel {
BufferedImage buffPanel3 = new BufferedImage(200,450,BufferedImage.TYPE_INT_ARGB);
private JPanel1 jP1;
private JPanel2 jP2;
/**
* This is the default constructor
*/
public JPanel3(JPanel1 jP1, JPanel2 jP2) {
super();
this.jP1 = jP1;
this.jP2 = jP2;
initialize();
repaint();
}
/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setBounds(new java.awt.Rectangle(250,25,200,450));
this.setBackground(java.awt.Color.white);
}
public void paint(Graphics g) {
Graphics2D g2 = buffPanel3.createGraphics();
g2.translate(0.0,0.0);
g2.drawImage(jP2.getBuffPanel(),0,0,this);
g2.drawImage(jP1.getBuffPanel(),0,112,this);
g.drawImage(buffPanel3,0,112,this);
}
}
public class PrintPanels extends JFrame {
private JPanel jContentPane = null;
private JPanel1 jPanel1 = null;
private JPanel2 jPanel2 = null;
private JPanel3 jPanel3 = null;
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
PrintPanels application = new PrintPanels();
}
/**
* This is the default constructor
*/
public PrintPanels() {
super();
initialize();
}
/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
this.setSize(500, 575);
this.setContentPane(getJContentPane());
this.setTitle("Application");
this.setVisible(true);
}
/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
jPanel1 = new JPanel1();
jPanel2 = new JPanel2();
jPanel3 = new JPanel3(jPanel1,jPanel2);
if (jContentPane == null) {
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.add(jPanel1);
jContentPane.add(jPanel2);
jContentPane.add(jPanel3);
}
return jContentPane;
}
}
vielen Dank schon mal.
Gruß xonauga
anbei die vier Klassen JPanel1, JPanel2, JPanel3 und PrintPanels mit der main-Methode
public class JPanel1 extends JPanel {
BufferedImage buffPanel = new BufferedImage(200,255,BufferedImage.TYPE_INT_ARGB);
/**
* This is the default constructor
*/
public JPanel1() {
super();
initialize();
repaint();
}
/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setBounds(new java.awt.Rectangle(25,25,200,225));
this.setBackground(java.awt.Color.white);
}
public void paint(Graphics g) {
Graphics2D g2 = buffPanel.createGraphics();
g2.setColor(Color.WHITE);
g2.fillRect(0,0,200,225);
g2.setColor(Color.RED);
g2.fillOval(50,62,100,100);
g.drawImage(buffPanel, 0,0,this);
}
public BufferedImage getBuffPanel() {
return buffPanel;
}
}
public class JPanel2 extends JPanel {
BufferedImage buffPanel = new BufferedImage(200,255,BufferedImage.TYPE_INT_ARGB);
/**
* This is the default constructor
*/
public JPanel2() {
super();
initialize();
repaint();
}
/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setBounds(new java.awt.Rectangle(25,250,200,225));
this.setBackground(java.awt.Color.white);
}
public void paint(Graphics g) {
Graphics2D g2 = buffPanel.createGraphics();
g2.setColor(Color.WHITE);
g2.fillRect(0,0,200,225);
g2.setColor(Color.BLUE);
g2.fillOval(50,62,100,100);
g.drawImage(buffPanel, 0,0,this);
}
public BufferedImage getBuffPanel() {
return buffPanel;
}
}
public class JPanel3 extends JPanel {
BufferedImage buffPanel3 = new BufferedImage(200,450,BufferedImage.TYPE_INT_ARGB);
private JPanel1 jP1;
private JPanel2 jP2;
/**
* This is the default constructor
*/
public JPanel3(JPanel1 jP1, JPanel2 jP2) {
super();
this.jP1 = jP1;
this.jP2 = jP2;
initialize();
repaint();
}
/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setBounds(new java.awt.Rectangle(250,25,200,450));
this.setBackground(java.awt.Color.white);
}
public void paint(Graphics g) {
Graphics2D g2 = buffPanel3.createGraphics();
g2.translate(0.0,0.0);
g2.drawImage(jP2.getBuffPanel(),0,0,this);
g2.drawImage(jP1.getBuffPanel(),0,112,this);
g.drawImage(buffPanel3,0,112,this);
}
}
public class PrintPanels extends JFrame {
private JPanel jContentPane = null;
private JPanel1 jPanel1 = null;
private JPanel2 jPanel2 = null;
private JPanel3 jPanel3 = null;
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
PrintPanels application = new PrintPanels();
}
/**
* This is the default constructor
*/
public PrintPanels() {
super();
initialize();
}
/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
this.setSize(500, 575);
this.setContentPane(getJContentPane());
this.setTitle("Application");
this.setVisible(true);
}
/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
jPanel1 = new JPanel1();
jPanel2 = new JPanel2();
jPanel3 = new JPanel3(jPanel1,jPanel2);
if (jContentPane == null) {
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.add(jPanel1);
jContentPane.add(jPanel2);
jContentPane.add(jPanel3);
}
return jContentPane;
}
}
vielen Dank schon mal.
Gruß xonauga