java - 无法为程序创建启动画面,还有颜色问题?

标签 java fonts panel frame shapes

这是我的启动代码:

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

public class AddSplash extends JPanel {
    public void paintComponent(Graphics g){
        super.paintComponent(g);
        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: Null", 150, 245);  
    }

}

这是我的主要内容:

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.util.*;

import javax.swing.JComponent;

public class ButtonComponent extends JComponent {

    int temp;
    Color randColor;
    Random generator = new Random();

    public ButtonComponent() {
        this.temp = temp;
        addMouseListener(new MouseHandler());
    }
    private ArrayList<Rectangle2D> arrOfRect = new ArrayList<>();
    private ArrayList<Ellipse2D> arrOfEllipse = new ArrayList<>();

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        for (Rectangle2D e : arrOfRect) {
            g.setColor(getRandomColor());
            g2.draw(e);
        }
        for (Ellipse2D e : arrOfEllipse) {
            g.setColor(getRandomColor());
            g2.draw(e);
        }
    }
    public void add(Point2D p) {
        double x = p.getX();
        double y = p.getY();
        if (Stamper.temp == 1) {
            Ellipse2D ellipse = new Ellipse2D.Double(x, y, 100, 100);
            arrOfEllipse.add(ellipse);
        }
        if (Stamper.temp == 2) {
            Rectangle2D rectangle2 = new Rectangle2D.Double(x, y, 100, 100);
            arrOfRect.add(rectangle2);
        }
        if (Stamper.temp == 3) {
            Rectangle2D rectangle2 = new Rectangle2D.Double(x, y, 150, 100);
            arrOfRect.add(rectangle2);
        }
        if (Stamper.temp == 4) {
            Ellipse2D ellipse = new Ellipse2D.Double(x, y, 100, 50);
            arrOfEllipse.add(ellipse);
        }
    }

    private class MouseHandler extends MouseAdapter {

        public void mousePressed(MouseEvent event) {
            add(event.getPoint());
            repaint();
        }
    }

    private Color getRandomColor() {
        return new Color(generator.nextFloat(), generator.nextFloat(), generator.nextFloat());
    }
}

最后是另一个类:

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

public class Stamper extends JFrame {
    public static int temp = 0;
    public static void main(String[] args) {
        JFrame frame = new JFrame("Shape Stamper!");
        AddSplash splash = new AddSplash();

        JPanel container;
        JPanel splashtext;
        JButton circle = new JButton("Circle");
        JButton square = new JButton("Square");
        JButton rectangle = new JButton("Rectangle");
        JButton oval = new JButton("Oval");

        //splashtext.add(splash);
        container = new JPanel(new GridLayout(1,4));
        container.add(circle);
        container.add(square);
        container.add(rectangle);
        container.add(oval);


        circle.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e){
                temp = 1;
            }
        });
         square.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e){
                temp = 2;
            }
        });
          rectangle.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e){
                temp = 3;
            }
        });
           oval.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e){
                temp = 4;
            }
        });
           ButtonComponent shape = new ButtonComponent();
           frame.setSize(500, 500);

           frame.add(shape, BorderLayout.CENTER);
           frame.add(container, BorderLayout.SOUTH);

           frame.setVisible(true);
           frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

现在,由于某种原因,我无法在创建的形状上保留颜色。我希望它出现然后保持颜色。 我遇到的另一个问题是我无法让我的字体出现,然后在按下按钮时被破坏或失去可见性。

最佳答案

i cannot retain color on the created shapes as they are created

您需要将形状的颜色以及实际形状存储在 ArrayList 中。

请参阅 Custom Painting Approaches 中的 DrawOnComponent 示例有关如何完成此操作的示例。

您的代码会略有不同,因为您的代码支持不同类型的形状,因此您需要创建 ColoredShape 类而不是 ColoredRectangle 类。

关于java - 无法为程序创建启动画面,还有颜色问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20484600/

相关文章:

java - Lambda 捕获与非捕获

Android - unicode 字符显示不正确

fonts - 崇高文本3 : Reset font size to custom font size?

ios - 在 iOS 中创建按钮面板

java - 从 XML 文件中检索两条数据的最佳方法是什么?

java - antlr 词法分析器和解析器的转义字符

java - 给定一个数字 n ,我们必须找出小于或等于 n 且恰好有 3 个约数的数字

github - 如何使用 github 托管网络字体?

Java Swing Card Layout 更改显示的面板?

firefox - 在 Firefox 中创建面板