line Drawing in java

后端 未结 3 718
花落未央
花落未央 2021-01-28 18:04

There are number of problems with this code

public class LineEx extends JFrame implements MouseMotionListener,MouseListener{
    int x1,y1,x2,y2;
    public Line         


        
相关标签:
3条回答
  • 2021-01-28 18:49
    1. The source could be insets of JLabel's border.
    2. You have the visible rect. Add visible rect x and y to your basic coordinates.
    0 讨论(0)
  • 2021-01-28 19:02
    • 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 讨论(0)
  • 2021-01-28 19:07
    1. Don't extend anything (especially not JFrame).
    2. Keep a reference to the original BufferedImage that was loaded.
    3. When a mouse event occurs (on the label, as mentioned by others), paint the line to a copy of the image, which is then used for the label.
    4. Put the label in a panel with GridBagLayout with no constraint, so it is centered.
    5. Drop the panel into a scroll pane that is then added to some constraint (e.g. BorderLayout.CENTER) of a parent component.

    Note that you might also add the line objects to an expandable collection such as an ArrayList or DefaultListModel, then display them in a JList to the WEST of the image scroll pane. This would make it easier to manage (and potentially delete) groups of lines.

    0 讨论(0)
提交回复
热议问题