Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <ul> <li>Just to justify my comment, I am adding this answer, though a slight change from the comment is here, which being the use of <code>mousePressed(...)</code> instead of <code>mouseClicked(...)</code>.</li> <li><p>One more addition being, since you wanted the <code>Graphics2D</code> object of the <code>BufferedImage</code> so instead of using <code>getGraphics()</code> always use <code>createGraphics()</code> which returns the <code>Graphics2D</code> object, hence you don't really have to worry about the Cast thingy in this.</p> <p>Please do have a look at the example below : </p></li> </ul> <p>======================</p> <pre><code>import java.awt.*; import java.awt.image.BufferedImage; import java.awt.event.*; import java.net.URL; import javax.swing.*; import javax.imageio.ImageIO; public class PaintingExample { private BufferedImage bImage; private ImageIcon image; private JLabel imageLabel; private int xClicked = 0; private int yClicked = 0; private int xDragged = 0; private int yDragged = 0; private MouseAdapter mouseListener = new MouseAdapter() { @Override public void mousePressed(MouseEvent me) { xClicked = me.getX(); yClicked = me.getY(); } @Override public void mouseDragged(MouseEvent me) { xDragged = me.getX(); yDragged = me.getY(); Graphics2D g2 = bImage.createGraphics(); g2.setColor(Color.WHITE); BasicStroke stroke=new BasicStroke(30); g2.setStroke(stroke); g2.drawLine(xClicked, yClicked, xDragged, yDragged); g2.dispose(); imageLabel.setIcon(new ImageIcon(bImage)); } }; public PaintingExample() { try { bImage = ImageIO.read(new URL( "http://i.imgur.com/fHiBMwI.jpg")); image = new ImageIcon(bImage); } catch(Exception e) { e.printStackTrace(); } } private void displayGUI() { JFrame frame = new JFrame("Painting on Image"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel contentPane = new JPanel(); imageLabel = new JLabel(image); imageLabel.addMouseListener(mouseListener); imageLabel.addMouseMotionListener(mouseListener); contentPane.add(imageLabel); frame.setContentPane(contentPane); frame.pack(); frame.setLocationByPlatform(true); frame.setVisible(true); } public static void main(String... args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { new PaintingExample().displayGUI(); } }); } } </code></pre>
    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.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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