Note that there are some explanatory texts on larger screens.

plurals
  1. PODraw in an image inside panel
    primarykey
    data
    text
    <p>I have a panel with two buttons. I'm trying to insert an image inside the panel and I want to draw lines inside the image after clicking on a button. I have used the below code but this doesn't seem to work.</p> <pre><code>public class Try_Panel extends JFrame { // start attributes private JPanel jPanel1 = new JPanel(null, true); private JButton jButton1 = new JButton(); private JButton jButton2 = new JButton(); // end attributes public Try_Panel(String title) { // Frame-Init super(title); setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); int frameWidth = 300; int frameHeight = 300; setSize(frameWidth, frameHeight); Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); int x = (d.width - getSize().width) / 2; int y = (d.height - getSize().height) / 2; setLocation(x, y); setResizable(false); Container cp = getContentPane(); cp.setLayout(null); // start components jPanel1.setBounds(48, 24, 209, 145); jPanel1.setOpaque(false); cp.add(jPanel1); jButton1.setBounds(88, 208, 75, 25); jButton1.setText("jButton1"); jButton1.setMargin(new Insets(2, 2, 2, 2)); jButton1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { jButton1_ActionPerformed(evt); } }); cp.add(jButton1); jButton2.setBounds(184, 208, 75, 25); jButton2.setText("jButton2"); jButton2.setMargin(new Insets(2, 2, 2, 2)); jButton2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { jButton2_ActionPerformed(evt); } }); cp.add(jButton2); // end components setVisible(true); } // end of public Try_Panel // start methods public void jButton1_ActionPerformed(ActionEvent evt) { BufferedImage image=new BufferedImage(jPanel1.getWidth(), jPanel1.getHeight(), BufferedImage.TYPE_3BYTE_BGR); JLabel l=new JLabel(new ImageIcon(image)); Graphics graphics = image.getGraphics(); Graphics2D g = (Graphics2D) graphics; g.fillRect(0, 0, image.getWidth(), image.getHeight()); g.setColor(Color.BLUE); g.drawLine(0, 0, 300, 400); jPanel1.add(l); } // end of jButton1_ActionPerformed public void jButton2_ActionPerformed(ActionEvent evt) { // TODO add your code here } // end of jButton2_ActionPerformed // end methods public static void main(String[] args) { new Try_Panel("Try_Panel"); } // end of main } // end of class Try_Panel </code></pre> <p>The biggest problem is the same code worked in my other class. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload