Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7512405
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T23:51:33+00:00 2026-05-29T23:51:33+00:00

There are number of problems with this code public class LineEx extends JFrame implements

  • 0

There are number of problems with this code

public class LineEx extends JFrame implements MouseMotionListener,MouseListener{
    int x1,y1,x2,y2;
    public LineEx(){
        JLabel image=new JLabel("");
        JFileChooser chooser=new JFileChooser();

        chooser.setCurrentDirectory(new File("."));
        int r=chooser.showOpenDialog(new JFrame());
        File file=chooser.getSelectedFile();
        if(r==JFileChooser.APPROVE_OPTION){
            try {
                BufferedImage bf;
                bf = ImageIO.read(file);
                ImageIcon icon=new ImageIcon(bf);
                image.setIcon(icon);
                image.setHorizontalAlignment(JLabel.CENTER);

            } catch (IOException ex) {
                Logger.getLogger(LineEx.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        JScrollPane jsp=new JScrollPane(image);
        getContentPane().add(jsp);
        image.addMouseListener(this);
        image.addMouseMotionListener(this);
        this.pack();
    }
    public static void main(String ar[]){
        LineEx line=new LineEx();
        line.setVisible(true);
        line.setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public void mouseClicked(MouseEvent e) {

    }

    public void mousePressed(MouseEvent e) {
        x1=e.getX();
        y1=e.getY();
    }

    public void mouseReleased(MouseEvent e) {
        JOptionPane.showMessageDialog(rootPane, "X1="+x1+"  Y1="+y1);
    }

    public void mouseEntered(MouseEvent e) {
    }

    public void mouseExited(MouseEvent e) {
    }
    @Override
    public void paint(Graphics g){
        super.paintComponents(g);
        Graphics2D gd=(Graphics2D)g;
        gd.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        Line2D line=new Line2D.Double(x1,y1,x2,y2);
        gd.draw(line);
    }

    public void mouseDragged(MouseEvent e) {
        x2=e.getX();
        y2=e.getY();
        repaint();
    }

    public void mouseMoved(MouseEvent e) {
    }
}
  1. MouseEvents are not getting exact co-ordinates that means whenever i draw a line it is not on its position. What is the reason behind this?
  2. I want to move line along the image when scrollbar goes up and down, how can i do that?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T23:51:34+00:00Added an answer on May 29, 2026 at 11:51 pm
    • MouseEvents are not getting exact co-ordinates that means whenever i draw a line it is not on its position. What is the reason behind this?
    • I want to move line along the image when scrollbar goes up and down, how can i do that?

    You are getting the correct coordinates from the JLabel but paints on the JFrame. And the frame coordinates begins at the top left point and “includes” the window title/border.

    Override the paintComponent method on the JLabel and it you will get the correct insets and coordinates.


    Example:

    class ImageComponent extends JComponent 
            implements MouseListener, MouseMotionListener {
        private final BufferedImage img;
        private Point p1, p2;
    
        public ImageComponent(URL url) throws IOException {
            img = ImageIO.read(url);
            setPreferredSize(new Dimension(img.getWidth(), img.getHeight()));
            addMouseListener(this);
            addMouseMotionListener(this);
        }
        @Override protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.drawImage(img, 0, 0, img.getWidth(), img.getHeight(), this);
            if (p1 != null && p2 != null)
                g.drawLine(p1.x, p1.y, p2.x, p2.y);
        }
        @Override public void mousePressed(MouseEvent e) {
            p1 = e.getPoint();
        }
        @Override public void mouseDragged(MouseEvent e) {
            mouseReleased(e);
        }
        @Override public void mouseReleased(MouseEvent e) {
            p2 = e.getPoint();
            repaint();
        }
        @Override public void mouseMoved(MouseEvent e) {}
        @Override public void mouseClicked(MouseEvent e) {}
        @Override public void mouseEntered(MouseEvent e) {}
        @Override public void mouseExited(MouseEvent e) {}
    }
    

    Test code (generates this screenshot):

    screenshot

    public static void main(String[] args) throws Exception {
    
        final URL lenna =
            new URL("http://upload.wikimedia.org/wikipedia/en/2/24/Lenna.png");
    
        final ImageComponent image = new ImageComponent(lenna);
    
        JFrame frame = new JFrame("Test");
        frame.add(new JScrollPane(image));
    
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400, 300);
        frame.setVisible(true);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There is this code: #include <iostream> class CleverClass{ public: CleverClass() : number(55){} void cleverOperation(){
I'm working on legacy code that looks very similar to this: public class BaseParser
There have been a number of problems related to IE8 breaking Visual Studio 2005/2008.
There are a number of run-time differences in compatible code between these two versions
Given the following code : public class Game { private Map<String,Coordinate> m_myHashMap; ... //
public class Employee { public static void main(String[] args) { int j=3; staples[] stemp
I don't see any problems with this code, but it feels like I'm missing
There are a couple of problems i'm having with this - the first is
Java Source Code: package n1_problem; /** * * @author nAwS */ public class loop
Assume I have a class like this: public class Foo { public Bar RequiredProperty

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.