这是我的Splash代码:
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);
}
}
由于某种原因,我无法在创建的形状上保留颜色。我希望它出现然后保持颜色。 我遇到的另一个问题是我无法显示我的字体然后被按下按钮时被破坏或失去了可见性。
答案 0 :(得分:1)
我无法在创建的形状上保留颜色
您需要在ArrayList中存储形状的颜色以及实际形状。
请参阅Custom Painting Approaches中的DrawOnComponent
示例,了解如何执行此操作的示例。
您的代码会略有不同,因为您需要创建ColoredShape
类而不是ColoredRectangle
类,因为您的代码支持不同类型的形状。