Displaying an image and capturing mouse clicks doesn't work at the same time“cannot find symbol method drawImage(java.awt.image.BufferedImage,<nulltype>,int,int)”Why can't I draw an ellipse with this code?how to perform mouse on click action using threadsDrag and Drop nodes in JTreeJScrollPane adding JPanels at the top and keeping current scroll viewadd KeyListener to JLabelError when trying to update a picture in JPanelJPanel added but not displayed “in time”Drawing an image in JScrollPane within scaleJComboBox mistake with repaint() method

How can a Lich look like a human without magic?

When a land becomes a creature, is it untapped?

Why do Thanos's punches not kill Captain America or at least cause some mortal injuries?

Smallest Guaranteed hash collision cycle length

Can I make ravioli dough with only all-purpose flour or do I NEED semolina flour?

Run script for 10 times until meets the condition, but break the loop if it meets the condition during iteration

What's special about a Bunsen burner?

Speculative Biology of a Haplodiploid Humanoid Species

What are the implications of the new alleged key recovery attack preprint on SIMON?

Proof that the inverse image of a single element is a discrete space

Why does my circuit work on a breadboard, but not on a perfboard? I am new to soldering

What is Plautus’s pun about frustum and frustrum?

Word for being out at night during curfew

Setting the major mode of a new buffer interactively

Why can't RGB or bicolour LEDs produce a decent yellow?

On studying Computer Science vs. Software Engineering to become a proficient coder

Extrude the faces of a cube symmetrically along XYZ

How can I answer high-school writing prompts without sounding weird and fake?

Do atomic orbitals "pulse" in time?

Why does getw return -1 when trying to read a character?

Why in a Ethernet LAN, a packet sniffer can obtain all packets sent over the LAN?

Can the sorting of a list be verified without comparing neighbors?

Was there ever any real use for a 6800-based Apple I?

Understanding integration over Orthogonal Group



Displaying an image and capturing mouse clicks doesn't work at the same time


“cannot find symbol method drawImage(java.awt.image.BufferedImage,<nulltype>,int,int)”Why can't I draw an ellipse with this code?how to perform mouse on click action using threadsDrag and Drop nodes in JTreeJScrollPane adding JPanels at the top and keeping current scroll viewadd KeyListener to JLabelError when trying to update a picture in JPanelJPanel added but not displayed “in time”Drawing an image in JScrollPane within scaleJComboBox mistake with repaint() method






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















What I want to do:



I want to write a small application that can display an image. The user has to be able to zoom in and out of the image, move it around, and mark points on the image. Further down the line I want to analyze the points clicked, but I'm not there yet.



What I have so far:



In order to track down my problem I wrote a MVCE:



GUI class for handling the JFrame (and other UI elements later):



import javax.swing.*;
import java.net.MalformedURLException;
import java.net.URL;

public class MCVE_GUI

public static void main(String[] args) throws MalformedURLException
MCVE_ZoomPane zp = new MCVE_ZoomPane(new URL("https://fiji.sc/site/logo.png"));

JFrame f = new JFrame("PictureMeasurement");
f.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
f.setContentPane(zp);
f.pack();
f.setLocationRelativeTo(null);
f.revalidate();
f.repaint();
f.setVisible(true);





ZoomPanel for handling the image and zooming:



import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.URL;

class MCVE_ZoomPane extends JPanel implements MouseMotionListener

MCVE_ZoomPane(URL url)
JLabel image = new JLabel();
JScrollPane jsp = new JScrollPane(image);

//image.setIcon(new ImageIcon(url)); // picture, no input
//jsp.setPreferredSize(new Dimension(300,300)); //picture, no input
jsp.setPreferredSize(image.getPreferredSize()); //depends on position of image.setIcon
image.setIcon(new ImageIcon(url)); //no picture, input

this.add(jsp);
this.setPreferredSize(image.getPreferredSize());
this.addMouseMotionListener(this);


@Override
public void paintComponent(Graphics g)
super.paintComponent(g);


public void mouseDragged(MouseEvent e)
System.out.format("Dragged X:%d Y:%dn",e.getX(), e.getY());


public void mouseMoved(MouseEvent e)




The problem:



Depending on where I put the image.setIcon(new ImageIcon(url)) I get either the image displayed or can listen to mouse clicks, but not both together. If I set the JScrollPane to a fixed preferred size without calling image.getPreferredSize() I always get a picture but no input.










share|improve this question




























    0















    What I want to do:



    I want to write a small application that can display an image. The user has to be able to zoom in and out of the image, move it around, and mark points on the image. Further down the line I want to analyze the points clicked, but I'm not there yet.



    What I have so far:



    In order to track down my problem I wrote a MVCE:



    GUI class for handling the JFrame (and other UI elements later):



    import javax.swing.*;
    import java.net.MalformedURLException;
    import java.net.URL;

    public class MCVE_GUI

    public static void main(String[] args) throws MalformedURLException
    MCVE_ZoomPane zp = new MCVE_ZoomPane(new URL("https://fiji.sc/site/logo.png"));

    JFrame f = new JFrame("PictureMeasurement");
    f.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    f.setContentPane(zp);
    f.pack();
    f.setLocationRelativeTo(null);
    f.revalidate();
    f.repaint();
    f.setVisible(true);





    ZoomPanel for handling the image and zooming:



    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.net.URL;

    class MCVE_ZoomPane extends JPanel implements MouseMotionListener

    MCVE_ZoomPane(URL url)
    JLabel image = new JLabel();
    JScrollPane jsp = new JScrollPane(image);

    //image.setIcon(new ImageIcon(url)); // picture, no input
    //jsp.setPreferredSize(new Dimension(300,300)); //picture, no input
    jsp.setPreferredSize(image.getPreferredSize()); //depends on position of image.setIcon
    image.setIcon(new ImageIcon(url)); //no picture, input

    this.add(jsp);
    this.setPreferredSize(image.getPreferredSize());
    this.addMouseMotionListener(this);


    @Override
    public void paintComponent(Graphics g)
    super.paintComponent(g);


    public void mouseDragged(MouseEvent e)
    System.out.format("Dragged X:%d Y:%dn",e.getX(), e.getY());


    public void mouseMoved(MouseEvent e)




    The problem:



    Depending on where I put the image.setIcon(new ImageIcon(url)) I get either the image displayed or can listen to mouse clicks, but not both together. If I set the JScrollPane to a fixed preferred size without calling image.getPreferredSize() I always get a picture but no input.










    share|improve this question
























      0












      0








      0








      What I want to do:



      I want to write a small application that can display an image. The user has to be able to zoom in and out of the image, move it around, and mark points on the image. Further down the line I want to analyze the points clicked, but I'm not there yet.



      What I have so far:



      In order to track down my problem I wrote a MVCE:



      GUI class for handling the JFrame (and other UI elements later):



      import javax.swing.*;
      import java.net.MalformedURLException;
      import java.net.URL;

      public class MCVE_GUI

      public static void main(String[] args) throws MalformedURLException
      MCVE_ZoomPane zp = new MCVE_ZoomPane(new URL("https://fiji.sc/site/logo.png"));

      JFrame f = new JFrame("PictureMeasurement");
      f.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
      f.setContentPane(zp);
      f.pack();
      f.setLocationRelativeTo(null);
      f.revalidate();
      f.repaint();
      f.setVisible(true);





      ZoomPanel for handling the image and zooming:



      import javax.swing.*;
      import java.awt.*;
      import java.awt.event.*;
      import java.net.URL;

      class MCVE_ZoomPane extends JPanel implements MouseMotionListener

      MCVE_ZoomPane(URL url)
      JLabel image = new JLabel();
      JScrollPane jsp = new JScrollPane(image);

      //image.setIcon(new ImageIcon(url)); // picture, no input
      //jsp.setPreferredSize(new Dimension(300,300)); //picture, no input
      jsp.setPreferredSize(image.getPreferredSize()); //depends on position of image.setIcon
      image.setIcon(new ImageIcon(url)); //no picture, input

      this.add(jsp);
      this.setPreferredSize(image.getPreferredSize());
      this.addMouseMotionListener(this);


      @Override
      public void paintComponent(Graphics g)
      super.paintComponent(g);


      public void mouseDragged(MouseEvent e)
      System.out.format("Dragged X:%d Y:%dn",e.getX(), e.getY());


      public void mouseMoved(MouseEvent e)




      The problem:



      Depending on where I put the image.setIcon(new ImageIcon(url)) I get either the image displayed or can listen to mouse clicks, but not both together. If I set the JScrollPane to a fixed preferred size without calling image.getPreferredSize() I always get a picture but no input.










      share|improve this question














      What I want to do:



      I want to write a small application that can display an image. The user has to be able to zoom in and out of the image, move it around, and mark points on the image. Further down the line I want to analyze the points clicked, but I'm not there yet.



      What I have so far:



      In order to track down my problem I wrote a MVCE:



      GUI class for handling the JFrame (and other UI elements later):



      import javax.swing.*;
      import java.net.MalformedURLException;
      import java.net.URL;

      public class MCVE_GUI

      public static void main(String[] args) throws MalformedURLException
      MCVE_ZoomPane zp = new MCVE_ZoomPane(new URL("https://fiji.sc/site/logo.png"));

      JFrame f = new JFrame("PictureMeasurement");
      f.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
      f.setContentPane(zp);
      f.pack();
      f.setLocationRelativeTo(null);
      f.revalidate();
      f.repaint();
      f.setVisible(true);





      ZoomPanel for handling the image and zooming:



      import javax.swing.*;
      import java.awt.*;
      import java.awt.event.*;
      import java.net.URL;

      class MCVE_ZoomPane extends JPanel implements MouseMotionListener

      MCVE_ZoomPane(URL url)
      JLabel image = new JLabel();
      JScrollPane jsp = new JScrollPane(image);

      //image.setIcon(new ImageIcon(url)); // picture, no input
      //jsp.setPreferredSize(new Dimension(300,300)); //picture, no input
      jsp.setPreferredSize(image.getPreferredSize()); //depends on position of image.setIcon
      image.setIcon(new ImageIcon(url)); //no picture, input

      this.add(jsp);
      this.setPreferredSize(image.getPreferredSize());
      this.addMouseMotionListener(this);


      @Override
      public void paintComponent(Graphics g)
      super.paintComponent(g);


      public void mouseDragged(MouseEvent e)
      System.out.format("Dragged X:%d Y:%dn",e.getX(), e.getY());


      public void mouseMoved(MouseEvent e)




      The problem:



      Depending on where I put the image.setIcon(new ImageIcon(url)) I get either the image displayed or can listen to mouse clicks, but not both together. If I set the JScrollPane to a fixed preferred size without calling image.getPreferredSize() I always get a picture but no input.







      java image swing interactive






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 23 at 11:56









      Turun ambartanenTurun ambartanen

      115




      115






















          1 Answer
          1






          active

          oldest

          votes


















          0














          Apparently I'm stupid. The JScrollPane/JLabel covered the JPanel which was the only component which had a MouseMotionListener. The solution is to add the single line of image.addMouseMotionListener(this);.



          I thought about and tried different soltuions to this for at least three hours now. It's a hobby project, so no time constaints, but man do I feel stupid now.






          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55313480%2fdisplaying-an-image-and-capturing-mouse-clicks-doesnt-work-at-the-same-time%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            Apparently I'm stupid. The JScrollPane/JLabel covered the JPanel which was the only component which had a MouseMotionListener. The solution is to add the single line of image.addMouseMotionListener(this);.



            I thought about and tried different soltuions to this for at least three hours now. It's a hobby project, so no time constaints, but man do I feel stupid now.






            share|improve this answer



























              0














              Apparently I'm stupid. The JScrollPane/JLabel covered the JPanel which was the only component which had a MouseMotionListener. The solution is to add the single line of image.addMouseMotionListener(this);.



              I thought about and tried different soltuions to this for at least three hours now. It's a hobby project, so no time constaints, but man do I feel stupid now.






              share|improve this answer

























                0












                0








                0







                Apparently I'm stupid. The JScrollPane/JLabel covered the JPanel which was the only component which had a MouseMotionListener. The solution is to add the single line of image.addMouseMotionListener(this);.



                I thought about and tried different soltuions to this for at least three hours now. It's a hobby project, so no time constaints, but man do I feel stupid now.






                share|improve this answer













                Apparently I'm stupid. The JScrollPane/JLabel covered the JPanel which was the only component which had a MouseMotionListener. The solution is to add the single line of image.addMouseMotionListener(this);.



                I thought about and tried different soltuions to this for at least three hours now. It's a hobby project, so no time constaints, but man do I feel stupid now.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Mar 23 at 12:00









                Turun ambartanenTurun ambartanen

                115




                115





























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid …


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55313480%2fdisplaying-an-image-and-capturing-mouse-clicks-doesnt-work-at-the-same-time%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    How to preserve electronics (computers, tablets and phones) for hundreds of years Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) The network's official Twitter account is up and running again. What content…How might modern humans leave a message for 50,000 years?How would an aquatic race develop computers?How do we make computers care for human life?Keeping supplies for 1000 yearsWhat will cell phones become in 100 years?What body armor protects against a laser?Could a society attain 1800s technology if limited to using copper and bronze?Electricity power (magic) and electronicsScavenging metal resources in post-apocalypse after 100 yearsWhy would precursors create devices that can survive and still work after hundreds of years?

                    155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해

                    Convert JSON to MapSimplest way to convert a string representing JSON array into Java array List of key value pairsHow to take String JSON values into HashMapConvert a JSON String to a HashMapCreating BSON object from JSON stringJSONObject - How to get a value ?Better Map ConstructorReading JSON from Url javaUpdate columns if input values are not null otherwise ignore and keep the existing values of column in databaseHow to store object represented as Json string into Couchbase Lite?Go JSON into MapHow do I efficiently iterate over each entry in a Java Map?Sort a Map<Key, Value> by valuesHow to round a number to n decimal places in JavaCan comments be used in JSON?How do I read / convert an InputStream into a String in Java?How can I pretty-print JSON in a shell script?What is the correct JSON content type?How can I deserialize JSON to a simple Dictionary<string,string> in ASP.NET?Why does Google prepend while(1); to their JSON responses?Parse JSON in JavaScript?