gpt4 book ai didi

java - PaintComponent() 绘制图形,但即使调用 repaint() 也不使用更新的值

转载 作者:行者123 更新时间:2023-11-30 07:03:55 24 4
gpt4 key购买 nike

我的程序旨在创建一个 GUI,允许用户通过在 JPanel 上单击并释放鼠标来在其上绘制形状。其他选项包括更改颜色以及是否填充形状。绘制形状后,其他选项应使用任何新的修饰符重新绘制相同的形状。例如,如果我绘制一个红色矩形,如果我选择蓝色,则该矩形应该只改变颜色。到目前为止,我只实现了“矩形”选项。

我的 mouseListner 正确捕获和存储值,当值被硬编码时,paintComponent() 绘制一个矩形(我已经注释掉了 PaintComponent() 中的部分,但为了简单起见,我正在初始化我的坐标变量),以及一系列 System .out.println() 显示程序经历了捕获鼠标按下、鼠标释放、调用paintComponent(),一直到实际的g.drawRect() 方法的顺序。我百分百肯定这些值不会被破坏,因为我可以在尝试绘制形状之前打印正确的值。我怀疑问题在于绘制多个形状,但即便如此,当我硬编码值并更改形状的颜色时,我实际上是在绘制一个新形状,而不是更改旧形状。

帮助。

public class WardA4 extends JFrame implements ActionListener{
private String[] chooseShapeOptions = {"Rectangle", "Square", "Oval", "Circle", "Line",
"Rounded Rectangle", "3D Rectangle"};
private JCheckBox chooseFill;
private JComboBox chooseShape;
private JButton chooseColor;
private JPanel userInterface, displayPanel;
private JLabel chooseFillLabel, chooseColorLabel;
private Color color = Color.WHITE;
private int shapeIndex = 0;
private double xStart = 100, yStart = 100, xEnd = 200, yEnd = 200;
private boolean isFilled;

public WardA4(){
super("Sandbox");
chooseShape = new JComboBox(chooseShapeOptions);
chooseFill = new JCheckBox();
chooseColor = new JButton();
chooseFillLabel = new JLabel("Fill");
chooseColorLabel = new JLabel ("Color");

userInterface = new JPanel(new FlowLayout());
userInterface.add(chooseShape);
userInterface.add(chooseFillLabel);
userInterface.add(chooseFill);
userInterface.add(chooseColorLabel);
userInterface.add(chooseColor);

displayPanel = new JPanel(){
public void paintComponent (Graphics g){
super.paintComponent(g);
System.out.println("Entering paint component");
System.out.println("starting coordinates are (" + xStart + "," + yStart + ")\n width is " + (int)Math.abs(xStart-xEnd) + "\n height is " + (int)Math.abs(yStart-yEnd));
g.setColor(color);
//System.out.println("" + (int)xStart + " " + (int)yStart + " " + (int)Math.abs(xStart-xEnd) + " " + (int)Math.abs(yStart-yEnd));
switch(shapeIndex){
case 0:
if(isFilled){
System.out.println("Entering is filled");
g.fillRect((int)xStart, (int)yStart, (int)Math.abs(xStart-xEnd), (int)Math.abs(yStart-yEnd));
//g.fillRect(100,100,100,100);
}
else{
System.out.println("Entering is not filled");
g.drawRect((int)xStart, (int)yStart, (int)Math.abs(xStart-xEnd), (int)Math.abs(yStart-yEnd));
//g.drawRect(100,100,100,100);
}
break;
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
break;
case 6:
break;
}
}
};

displayPanel.setBackground(Color.BLACK);

add(displayPanel, BorderLayout.CENTER);
add(userInterface, BorderLayout.SOUTH);

chooseShape.addActionListener(this);
chooseFill.addActionListener(this);
chooseColor.addActionListener(this);

displayPanel.addMouseListener(new MouseAdapter(){
public void mousePressed (MouseEvent me){
System.out.println("Entering mouse pressed");
xStart = MouseInfo.getPointerInfo().getLocation().getX();
yStart = MouseInfo.getPointerInfo().getLocation().getY();
System.out.println("mouse pressed at (" + xStart + "," + yStart + ")");
}
public void mouseReleased (MouseEvent me){
System.out.println("Entering mouse released");
xEnd = MouseInfo.getPointerInfo().getLocation().getX();
yEnd = MouseInfo.getPointerInfo().getLocation().getY();
System.out.println("mouse released at (" + xEnd + "," + yEnd + ")");
repaint();
}
});
}

public void actionPerformed(ActionEvent e){
if (e.getSource() == chooseShape){
shapeIndex = chooseShape.getSelectedIndex();
}
else if (e.getSource() == chooseFill){
isFilled = chooseFill.isSelected();
}
else if (e.getSource() == chooseColor){
color = JColorChooser.showDialog(null, "Choose color", color);
if (color == null)
color = Color.WHITE;
}
repaint();
}

public static void main(String[] args) {
WardA4 frame = new WardA4();
frame.setSize(400,300);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
}

示例输出,我在面板上按了几次:

Entering paint component
starting coordinates are (100.0,100.0)
width is 100
height is 100
Entering is not filled
Entering paint component
starting coordinates are (100.0,100.0)
width is 100
height is 100
Entering is not filled

Entering mouse pressed
mouse pressed at (906.0,449.0)
Entering mouse released
mouse released at (1092.0,612.0)
Entering paint component
starting coordinates are (906.0,449.0)
width is 186
height is 163
Entering is not filled

Entering mouse pressed
mouse pressed at (1092.0,612.0)
Entering mouse released
mouse released at (1092.0,612.0)
Entering paint component
starting coordinates are (1092.0,612.0)
width is 0
height is 0
Entering is not filled

最佳答案

正在查询的事件有错误(使用了错误的坐标)。例如

xStart = MouseInfo.getPointerInfo().getLocation().getX(); // gets location ON SCREEN
yStart = MouseInfo.getPointerInfo().getLocation().getY();

应该是:

xStart = me.getX(); // gets location relative TO PANEL
yStart = me.getY();

关于java - PaintComponent() 绘制图形,但即使调用 repaint() 也不使用更新的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40451895/

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