形状消失了

时间:2013-12-10 22:48:47

标签: java swing java-2d

好吧,我想出了我得到的一切,但现在我真的被卡住了。每次选择不同的形状时,之前选择的形状都会消失。如何制作它以便它们不会消失并保持在屏幕上直到您退出?

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);
    }
    }

1 个答案:

答案 0 :(得分:1)

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

您将遇到的基本问题是,绘图系统非常聪明,可能无法调用框架的paint,而只是直接更新子组件。

相反,创建一个自定义组件,从JPanel扩展并覆盖它的paintComponent方法并在那里执行自定义绘制。然后将此组件添加到您的框架中。

您还会发现绘画更新更清晰,更新时不会闪烁。

您还应确保在更改其自定义组件时调用repaint,以确保将更改绘制回组件

请查看Performing Custom Painting了解详情。

另外,为了清楚起见,您不应该从super.paintComponents致电paint(或者实际上除了覆盖paintComponents之外的任何地方......实际应该是需要做...)