gpt4 book ai didi

java - Java Swing 谜题 : What doesn't the first iteration display on the JPanel?

转载 作者:行者123 更新时间:2023-12-01 07:09:32 24 4
gpt4 key购买 nike

这是我差一点就能弄清楚的一个问题,但从未真正做到过。

下面列出的代码应该在第一次单击时绘制一个绿色圆圈。事实并非如此。后续单击将绘制连接当前单击点与前一个单击点的线(红色)。该代码在第一次单击时失败,但在所有后续单击中都有效。为什么第一次点击不显示?它运行了!

我做错了什么?

代码应该在任何当前的 JDE 上编译。

TIA

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;

class Demo extends JFrame
implements ActionListener, ListSelectionListener, MouseListener {

int clkCt = 0, // Count of the number of clicks we've done.
oldX, // Penultimate X value
oldY, // Penultimate X value
scrH, // Height of the drawing canvas.
scrW; // Width of the drawing canvas.

JFrame f; // Holder for the drawing canvas.

JLabel ctL; // Displays the number of clicks we've done.

JPanel canvas; // The drawing canvas.

public void demoLines() {

Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
scrH = (int) ((double) d.height * 0.75);
scrW = (int) ((double) d.width * 0.75);

oldX = scrH / 2;
oldY = oldX;

// Create and set up the window.
f = new JFrame("Multi Click Demo");
f.getContentPane().setLayout(null);
int h = scrH / 5;
f.setBounds(h, h, scrW, scrH);

// Create a panel
canvas = new JPanel();
canvas.setBackground(Color.black);
canvas.setForeground(Color.red);
canvas.setLayout(null);
canvas.setBounds(0, 0, scrW, scrH);
canvas.setPreferredSize(new Dimension(scrW, scrH));
canvas.addMouseListener(this);
f.getContentPane().add(canvas);

// Create the exit button.
JButton exit = new JButton("Exit");
exit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
goAway();
}
});
exit.setBackground(Color.black);
exit.setForeground(Color.red);
exit.setBounds(0, 0, (scrW / 15), (scrH / 15));
canvas.add(exit); //*/

// Create the label for the click count.
ctL = new JLabel("None Yet");
ctL.setBackground(Color.black);
ctL.setForeground(Color.red);
ctL.setBounds((scrH / 15), (scrH * 13 / 15), (scrW / 15), (scrH / 15));
canvas.add(ctL);

f.getContentPane().add(canvas);
f.setVisible(true);

Graphics g = canvas.getGraphics();
if (g == null) {
System.out.println("No graphics for canvas!");
} else {
canvas.revalidate(); // This didn't help.
paintComponent(g, (oldX + oldX / 2), (oldY + oldY / 2));
}
}

void goAway() {
f.setVisible(false);
f.dispose();
}

public void mouseClicked(MouseEvent m) {
// Where was the mouse clicked?
int clkdBtn = m.getButton(),
x = m.getX(),
y = m.getY();
Graphics g = canvas.getGraphics();
paintComponent(g, x, y);
}

public void paintComponent(Graphics g,
int x,
int y) {

// This always runs.
ctL.setText(clkCt + "");

if (clkCt == 0) {
// This never displays!
g.setColor(Color.green);
int r = scrH * 4 / 5;
g.drawOval((scrH / 10), (scrH / 10), r, r);
}

g.setColor(Color.red);
g.drawLine(oldX, oldY, x, y);
oldX = x;
oldY = y;
clkCt++;
}


public void actionPerformed(ActionEvent event) { }
public void valueChanged(ListSelectionEvent event) { }

public void mouseEntered(MouseEvent e) { }
public void mouseExited(MouseEvent e) { }
public void mousePressed(MouseEvent e) { }
public void mouseReleased(MouseEvent e) { }

public static void main(String[] s) {

Demo m = new Demo();
m.demoLines();
}
}

最佳答案

What am I doing wrong?

您正在使用getGraphics用于定制绘画。当 repaint 时,任何以前的绘画都将丢失。叫做。相反,将所有自定义绘画功能移动到基于 JComponent 的新类中。或JPanel并覆盖paintComponent那里。记得调用super.paintComponent(g) .

参见Custom Painting Approaches用于从内部绘制多个组件的解决方案paintComponent 。您可以构建一个 List<Shape>自定义可绘制组件,从方法内迭代列表,并使用 drawLinedrawOval视情况而定。

关于java - Java Swing 谜题 : What doesn't the first iteration display on the JPanel?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17137171/

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