3

我是java新手,我需要实现一个绘画应用程序,我有点卡在开始,我设法画线到我添加到JFrame的JPanel,但每条画线都会重置整个绘图,并且在绘图区域中仅保留最后绘制的线。我希望我让自己理解,这里是他的代码:

class Shapes extends JFrame {

    public JFrame mf = new JFrame("Paint");
    DrawArea da = new DrawArea();

        JToggleButton lineButton = new JToggleButton(new ImageIcon("line.gif"));
        JToggleButton brushButton = new JToggleButton();
        JToggleButton pencilButton = new JToggleButton();
        JToggleButton eraserButton = new JToggleButton(new ImageIcon("eraser_icon.png"));
        JToggleButton rectangleButton = new JToggleButton();
        JToggleButton ovalButton = new JToggleButton();

    Shapes() {


        da.setBounds(120, 50, 500, 350);
        da.setBackground(Color.YELLOW);
        mf.setSize(700, 500);
        mf.setLayout(null);

        lineButton.setBounds(0, 50, 40, 40);
        brushButton.setBounds(40, 50, 40, 40);
        eraserButton.setBounds(0, 90, 40, 40);
        pencilButton.setBounds(40, 90, 40, 40);
        rectangleButton.setBounds(0, 130, 40, 40);
        ovalButton.setBounds(40, 130, 40, 40);

        mf.setBackground(Color.red);
        mf.add(lineButton);
        mf.add(brushButton);
        mf.add(pencilButton);
        mf.add(eraserButton);
        mf.add(rectangleButton);
        mf.add(ovalButton);
        mf.add(da);
        mf.show();
        mf.addWindowListener(new WindowAdapter() {

            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        mf.addMouseListener(new MouseAdapter() {

            public void mouseClicked(MouseEvent e) {
                System.out.println("x:" + e.getX() + "y:" + e.getY() + "\n" + "x2:" + e.getXOnScreen() + "y2:" + e.getYOnScreen());
            }
        });
        eraserButton.addMouseListener(new MouseAdapter() {
        public void mouseClicked(MouseEvent e)
            {
            eraserButton.setSelectedIcon(new ImageIcon("eraser_icon_selected.png"));
        }
        });
        lineButton.addMouseListener(new MouseAdapter() {
        public void mouseClicked(MouseEvent e)
            {
              lineButton.setSelectedIcon(new ImageIcon("line_selected.png"));
        }
        });
        da.addMouseListener(new MouseAdapter() {

            public void mousePressed(MouseEvent e) {
                da.setXvalue(e.getX());
                da.setYvalue(e.getY());



            }

            public void mouseReleased(MouseEvent e) {

                da.setX2value(e.getX());
                da.setY2value(e.getY());
                da.repaint();
            }
        });
        da.addMouseMotionListener(new MouseAdapter() {

            public void mouseDragged(MouseEvent e) {
                da.repaint();
                da.setX2value(e.getX());
                da.setY2value(e.getY());

            }
        });

    }
}



public class DrawArea extends JPanel {
    int x1value,y1value,x2value,y2value;

    public int getX2value() {
        return x2value;
    }

    public void setX2value(int x2value) {
        this.x2value = x2value;
    }

    public int getY2value() {
        return y2value;
    }

    public void setY2value(int y2value) {
        this.y2value = y2value;
    }
    public JPanel dra=new JPanel();

    public int getXvalue() {
        return x1value;
    }

    public void setXvalue(int xvalue) {
        this.x1value = xvalue;
    }

    public int getYvalue() {
        return y1value;
    }

    public void setYvalue(int yvalue) {
        this.y1value = yvalue;
    }

    public void paint(Graphics g)
    {
      super.paint(g);
      g.setColor(Color.red);
      g.drawLine(getXvalue(),getYvalue(),getX2value(),getY2value());

}
}

    class Paint extends JPanel

{ 

    public static void main(String args[])
            {
               Shapes s=new Shapes();

            }

}
4

5 回答 5

3

有关两种解决方案,请参阅自定义绘画方法。示例绘制矩形,但线的概念是相同的。

于 2011-04-06T15:09:17.877 回答
1

覆盖paintComponent(),而不是paint()。阅读本教程。当需要重绘面板时,调用该面板的 repaint( ) 方法。

于 2011-04-06T14:54:41.897 回答
0

每当窗口管理器认为该区域“不新鲜”时,都会调用 Paint。如果您按照现在的方式进行操作,则每次都会绘制最后一条线。

做到这一点的正确方法是BufferedImage在内存中制作并利用它。然后,在绘画方法中,将其涂抹BufferedImage到表面上。这也使得滚动和缩放变得非常容易。

每当您执行此类操作时,使表面无效,以便窗口管理器将为您调用绘制方法。

于 2011-04-06T14:59:08.180 回答
0

您只存储一行,并且每次都覆盖它,因此当重新绘制组件时,旧的将被擦除并重新绘制新的。

诸如此类的期望paintComponent是您的实现将在每次调用时绘制您想要出现的每个图形元素。

您应该创建一个类或类似的类来存储这些值,而不是存储x1, y1, x2, 。然后,当你绘画时,你调用你存储的每个对象(大概在一个或类似的)。然后,当重新绘制组件时,所有的线段都应该出现在屏幕上。y2LineSegmentg.drawLine()LineSegmentArrayList

于 2011-04-06T15:04:34.540 回答
0

有点离题,但我有几分钟不舒服,因为我使用了 update() 而不是 repaint()。我建议所有使用 SWING 的人花一些时间检查哪些方法应该作为线程安全处理,哪些方法必须在 EDT(事件调度线程)上,以确保您不会遇到一些意外错误。 这是一篇很好的文章。

另外,一开始要考虑是否要在您的应用程序中使用撤消/重做系统...如果是这样,则要允许撤消多少步骤。如果您想允许此功能,那么您不能只是绘制并忘记上次绘制的内容。此外,存储到目前为止绘制的所有图像也不会节省内存。我不是专家,我并不是说这是最佳做法,但我会这样做:

我会列出两个清单。

  1. 其中之一将存储应用的绘图动作
  2. 另一个将包含撤回的绘图动作

绘图动作将是一个接口,并且某些类将为每种特定类型的绘图动作(LineDrawAction、CirceDrawAction ...)实现它。

当您画一条新线或其他任何内容时,您将清空撤回的操作列表并将其添加到应用的操作列表中。当有人撤消最后一个动作时,我会从应用列表中删除最后一个绘图动作并添加到撤回列表(等等......)。取决于您是否只想在列表达到此 x 限制时撤消最后一个 x 操作,我将从列表或队列中删除第一个绘图操作并最终绘制到图片 - 这意味着永久绘图,这不能撤消。

即使不是直接回答您的问题,我也希望它清晰有用。

于 2011-04-06T15:42:17.107 回答