0

所以我只是在这里为期末考试做一些学习,但我无法让它发挥作用。我以前一直在做的是调用paintComponent(Graphics g),事情进展顺利。但是现在我在编译时收到“paintComponent has protected access in JComponent”错误。我可以让它编译的唯一方法是告诉它调用paint。但即使它编译,paint 也不会在小程序窗口中被调用。我在这里没有看到什么?

 public class LineDraw extends JComponent
 {
private final Point p1;
private final Point p2;
private final JApplet callingWindow;
private final ArrayList<Line2D.Double> lines;

public LineDraw(JApplet callingWindow)
{
    this.p1 = new Point();
    this.p2 = new Point();
    this.callingWindow = callingWindow;
    this.lines = new ArrayList<Line2D.Double>();
    MouseListener mouse = new MouseHandler();
    callingWindow.addMouseListener(mouse);
}

public class MouseHandler extends MouseAdapter
{
    private boolean firstClick;
    public MouseHandler()
    {
        firstClick = true;
    }
    public void mouseClicked(MouseEvent e)
    {
        if(firstClick)
        {
            p1.setLocation(e.getPoint());
            firstClick = false;
        }
        else    
        {
            p2.setLocation(e.getPoint());
            lines.add(new Line2D.Double(p1, p2));
            p2.setLocation(p1.getLocation());
            callingWindow.repaint();        
        }
    }

    public void paintComponent(Graphics2D g2)
    {
        for(Line2D.Double e: lines)
        {
            g2.draw(e);
        }
    }
}

}

还有小程序类本身

    public class AppletWin extends JApplet
    {
private LineDraw lineDrawer;

public void init()
{
    setBackground(Color.white);
    lineDrawer = new LineDraw(this);
}

public void paint(Graphics h)
{
    Graphics2D g = (Graphics2D)h;
    //Clear background
    g.setColor(getBackground());
    g.fillRect(1, 1, getWidth(), getHeight());
    lineDrawer.paintComponent(g);  
}
    }
4

2 回答 2

1

LineDrawer是一个组件,你应该把它添加到你的小程序中,它会自动绘制。

您似乎陷入了命名冲突问题。

JComponent定义了两种绘画方法(实际上它定义了更多,但让我们保持简单),paintpaintComponent.

JComponent您遇到的问题与您从 a 继承但似乎正在执行您自己的绘画这一事实有关。当您想开发自己的“画家”类但与 Swing 组件发生冲突时,这很好。Swing 组件还有更多内容,然后简单地绘制它。

您遇到的另一个问题是您在 sideMouseHandler方法中定义了一个“paint”方法,并且似乎假设它可以从LineDraw类中调用。问题是,您实际上是在调用JComponent'paint方法,而不是您想要/认为的方法。

尝试删除extends JComponent并将方法移动paintLineDraw类中。

正如 MouseEvent 所说,在大多数情况下,您永远不应该调用paintpaintComponent直接调用。

你可能会发现:

有帮助

更新

绘画作为摆动组件

public class AppletWin extends JApplet {

    private LineDraw lineDrawer;

    public void init() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                setBackground(Color.white);
                lineDrawer = new LineDraw(AppletWin.this);
                setLayout(new BorderLayout());
                add(lineDrawer);
            }
        });
    }

    public class LineDraw extends JComponent {

        private final Point p1;
        private final Point p2;
        private final JApplet callingWindow;
        private final ArrayList<Line2D.Double> lines;

        public LineDraw(JApplet callingWindow) {
            this.p1 = new Point();
            this.p2 = new Point();
            this.callingWindow = callingWindow;
            this.lines = new ArrayList<Line2D.Double>();
            MouseListener mouse = new MouseHandler();
            addMouseListener(mouse);
        }

        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D) g;
            g2.setColor(Color.RED);
            for (Line2D.Double e : lines) {
                g2.draw(e);
            }
        }

        public class MouseHandler extends MouseAdapter {

            private boolean firstClick;

            public MouseHandler() {
                firstClick = true;
            }

            public void mouseClicked(MouseEvent e) {
                if (firstClick) {
                    p1.setLocation(e.getPoint());
                    firstClick = false;
                } else {
                    p2.setLocation(e.getPoint());
                    lines.add(new Line2D.Double(p1, p2));
                    p2.setLocation(p1.getLocation());
                }
                repaint();
            }
        }
    }
}

作为画家

public class AppletWin extends JApplet {

    private LineDraw lineDrawer;

    public void init() {
        setBackground(Color.white);
        lineDrawer = new LineDraw(this);
    }

    public void paint(Graphics h) {
        // The the applet clear the background
        super.paint(h);
        Graphics2D g = (Graphics2D) h;
        lineDrawer.paintComponent(g);
    }
    public class LineDraw {

        private final Point p1;
        private final Point p2;
        private final JApplet callingWindow;
        private final ArrayList<Line2D.Double> lines;

        public LineDraw(JApplet callingWindow) {
            this.p1 = new Point();
            this.p2 = new Point();
            this.callingWindow = callingWindow;
            this.lines = new ArrayList<Line2D.Double>();
            MouseListener mouse = new MouseHandler();
            callingWindow.addMouseListener(mouse);
        }

        public void paintComponent(Graphics2D g2) {
            System.out.println("...");
            for (Line2D.Double e : lines) {
                g2.draw(e);
            }
        }

        public class MouseHandler extends MouseAdapter {

            private boolean firstClick;

            public MouseHandler() {
                firstClick = true;
            }

            public void mouseClicked(MouseEvent e) {
                if (firstClick) {
                    p1.setLocation(e.getPoint());
                    firstClick = false;
                } else {
                    p2.setLocation(e.getPoint());
                    lines.add(new Line2D.Double(p1, p2));
                    p2.setLocation(p1.getLocation());
                }
                callingWindow.repaint();
            }
        }
    }
}

您确实应该避免覆盖paint顶级容器的方法。该paint方法是一种非常复杂的方法,从长远来看,绕过它只会给您带来问题,此外,它不是双缓冲的;)

于 2012-12-05T05:44:15.323 回答
0

这段代码的问题在于 LineDraw 中的 paintComponent 方法位于内部类 MouseHandler 中。

于 2012-12-05T05:49:46.523 回答