gpt4 book ai didi

java - JApplets 和调用paint/paintComponent 的问题

转载 作者:行者123 更新时间:2023-12-01 15:00:33 25 4
gpt4 key购买 nike

所以我只是在这里为期末考试做一些学习,但我很难让它发挥作用。我以前一直做的是调用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);
}
}

最佳答案

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

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

JComponent 定义了两个绘画方法(实际上它还定义了一些方法,但让我们保持简单),paintpaintComponent

您遇到的问题与您从 JComponent 继承但似乎正在执行自己的绘画有关。当您想要开发自己的“画家”类,但与 Swing 组件发生冲突时,这很好。 Swing 组件除了简单地绘制它之外还有更多功能。

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

尝试删除 extends JComponent 并将 paint 方法移至 LineDraw 类中。

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

您可能会发现:

有帮助

已更新

作为 Swing 组件进行绘制

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 方法是一个非常复杂的方法,从长远来看,规避它只会给你带来问题,而且它不是双缓冲的;)

关于java - JApplets 和调用paint/paintComponent 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13716900/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com