line Drawing in java

后端 未结 3 762
花落未央
花落未央 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 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.

提交回复
热议问题