Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The issue here is three-fold:</p> <ol> <li>There needs to be a way to display a background image.</li> <li>One must be able to find the point at which the mouse was clicked.</li> <li>There must be a way to draw the point on the panel.</li> </ol> <p>One way to accomplish the above points would be to subclass a <a href="http://java.sun.com/javase/6/docs/api/javax/swing/JPanel.html" rel="noreferrer"><code>JPanel</code></a> and provide those functionalities.</p> <p><strong>1. Display a background image in a panel.</strong></p> <p>Firstly, as a <code>JPanel</code> does not have a way of displaying a background image by default, there must be a way to hold an image in the <code>JPanel</code>, and then draw that on the panel itself, which can be performed via the <a href="http://java.sun.com/javase/6/docs/api/javax/swing/JComponent.html#paintComponent(java.awt.Graphics)" rel="noreferrer"><code>paintComponent</code></a> method.</p> <p>One way to accomplish this is to have a field in the class which holds on to an <code>Image</code> to draw:</p> <pre><code>class MyPanel extends JPanel { // Background image. Initialize appropriately. Image backgroundImage; public void paintComponent(Graphics g) { super.paintComponent(g); // Draw background image each time the panel is repainted. g.drawImage(backgroundImage, 0, 0, null); } } </code></pre> <p>The <a href="http://java.sun.com/javase/6/docs/api/java/awt/Graphics.html" rel="noreferrer"><code>Graphics</code></a> object in <code>paintComponent</code> is associated with the <code>MyPanel</code> and can be used to perform graphics operations.</p> <p><strong>2. Finding the point at which the mouse was clicked.</strong></p> <p>Secondly, in order to retrieve the point at which the mouse was clicked, one could assign a <a href="http://java.sun.com/javase/6/docs/api/java/awt/event/MouseListener.html" rel="noreferrer"><code>MouseListener</code></a> to the <code>MyPanel</code>. In the following example, an anonymous inner class extending the <a href="http://java.sun.com/javase/6/docs/api/java/awt/event/MouseAdapter.html" rel="noreferrer"><code>MouseAdapter</code></a> is used to minimize writing extra code:</p> <pre><code>class MyPanel extends JPanel { // Background image. Initialize appropriately. Image backgroundImage; public MyPanel() { // Add a MouseListener which processes mouse clicks. this.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { // Process mouse-click. } }) } // paintComponents method here. } </code></pre> <p>Processing that needs to be performed when the mouse is clicked can be included in the <code>mouseClicked</code> method.</p> <p><strong>3. How to draw a point on the panel.</strong></p> <p>Thirdly, in order to find one point at which the mouse was clicked, one can obtain it from the <a href="http://java.sun.com/javase/6/docs/api/java/awt/event/MouseEvent.html" rel="noreferrer"><code>MouseEvent</code></a> object that was passed in from the <a href="http://java.sun.com/javase/6/docs/api/java/awt/event/MouseAdapter.html#mouseClicked(java.awt.event.MouseEvent)" rel="noreferrer"><code>mouseClicked</code></a> method:</p> <pre><code>class MyPanel extends JPanel { // Background image. Initialize appropriately. Image backgroundImage; Point pointClicked; public MyPanel() { // Add a MouseListener which processes mouse clicks. this.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { // Retrieve the point at which the mouse was clicked. pointClicked = e.getPoint(); // Repaint the panel. this.repaint(); } }) } public void paintComponent(Graphics g) { super.paintComponent(g); // Draw background image each time the panel is repainted. g.drawImage(backgroundImage, 0, 0, null); // Draw a little square at where the mouse was clicked. g.fillRect(pointClicked.x, pointClicked.y, 1, 1); } } </code></pre> <p>Although the above code is not tested, it should be a starting point.</p> <p>For example, if multiple points needs to be drawing, perhaps having a <code>List&lt;Point&gt;</code> to hold the points, and drawing each <a href="http://java.sun.com/javase/6/docs/api/java/awt/Point.html" rel="noreferrer"><code>Point</code></a> in the <code>paintComponents</code> method could be done.</p> <p>If additional processing needs to be performed when the mouse is clicked, additional code can be added to the <code>mouseClicked</code> method.</p> <p>Additional resources:</p> <ul> <li><a href="http://java.sun.com/docs/books/tutorial/uiswing/painting/" rel="noreferrer">Lesson: Performing Custom Painting</a></li> <li><a href="http://java.sun.com/products/jfc/tsc/articles/painting/" rel="noreferrer">Painting in AWT and Swing</a></li> <li><a href="http://java.sun.com/docs/books/tutorial/uiswing/events/mouselistener.html" rel="noreferrer">How to Write a Mouse Listener</a></li> </ul> <p><em>Thank you to zedoo for pointing out in the comments that making a call to <code>super.paintComponent</code> should be performed when overriding the <code>paintComponent</code> method.</em></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.
    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