gpt4 book ai didi

java - 形状消失

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:06:43 24 4
gpt4 key购买 nike

好吧,我想出了我得到的一切,但现在我真的被困住了。每次您选择不同的形状时,先前选择的形状都会消失。我该怎么做才能让它们在您退出之前不会消失并一直停留在屏幕上?

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.util.*;
import javax.swing.*;
public class ShapeStamper extends JFrame{
Random rand = new Random();
public int x;
public int y;
private JPanel panel1, panel2;
private JButton button1, button2, button3, button4;
private int option = 0;

public ShapeStamper(){
super("Shape Stamper!");
panel1 = new JPanel();
button1 = new JButton("Circle");
button2 = new JButton("Square");
button3 = new JButton("Rectangle");
button4 = new JButton("Oval");

button1.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent ae){
option = 1;
}
}
);
button2.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent ae){
option = 2;
}
}
);
button3.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent ae){
option = 3;
}
}
);
button4.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent ae){
option = 4;
}
}
);
panel2 = new JPanel();
panel2.setBackground(Color.WHITE);
MouseHandler mouse = new MouseHandler();
setVisible(true);
addMouseListener(mouse);
addMouseMotionListener(mouse);
add(panel2);
panel1.add(button1);
panel1.add(button2);
panel1.add(button3);
panel1.add(button4);

add(panel1, BorderLayout.SOUTH);
setSize(500,500);
setVisible(true);
}
private class MouseHandler extends MouseAdapter implements MouseMotionListener{
@Override
public void mousePressed(MouseEvent e){
x = e.getX();
y = e.getY();

repaint();
}
}
public void paint(Graphics g){
super.paintComponents(g);
Graphics2D g2d = (Graphics2D) g;
if(option == 0){
g.setFont(new Font("Serif", Font.BOLD, 32));
g.drawString("Shape Stamper!", 150, 220);
g.setFont(new Font("Serif", Font.ITALIC, 16));
g.drawString("Programmed by: Chris", 150, 230);
}
if(option == 1){
Color randColor1 = new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256));
g2d.setPaint(randColor1);
g2d.drawOval(50, 50, 100, 100);
}
if(option == 2){
Color randColor2 = new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256));
g2d.setPaint(randColor2);
g2d.drawRect(50, 50, 100, 100);
}
if(option == 3){
Color randColor3 = new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256));
g2d.setPaint(randColor3);
g2d.draw(new Rectangle2D.Double(75,50,150,100));
}
if(option == 4){
Color randColor4 = new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256));
g2d.setPaint(randColor4);
g2d.draw(new Ellipse2D.Double(50, 25, 100, 50));
}
}
public static void main(String[] args) {
ShapeStamper application = new ShapeStamper();
application.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}

最佳答案

你不应该覆盖 paint顶层容器,然后尝试在您已添加的组件的顶部进行绘制。

您将遇到的基本问题是,绘画系统足够聪明,它可能永远不会调用 paint。框架,而是直接更新子组件。

相反,为自己创建一个自定义组件,从类似于 JPanel 的东西扩展而来并覆盖它的 paintComponent方法并在那里执行您的自定义绘画。然后将此组件添加到您的框架中。

您还会发现油漆更新更干净,更新时不会闪烁。

您还应该确保您调用的是 repaint在此自定义组件上,每当您更改一个组件时,它都会提供选项以确保将更改绘制回组件

看看Performing Custom Painting了解更多详情。

此外,为了清楚起见,您不应该调用 super.paintComponents来自 paint (或者实际上除了当你覆盖 paintComponents 之外的任何地方......确实应该需要做......)

关于java - 形状消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20507000/

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