Question

Here is my Splash code:

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

}

Here is my Main:

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

and finally the other class:

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

Now for some reason i cannot retain color on the created shapes as they are created. I would like it to appear and then keep the color. The other issue i am encountering is that I cannot get my font to appear and then be destroyed or lose visibility upon pressing a button.

Was it helpful?

Solution

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

You need to store the color of the shapes as well as the actual shape in your ArrayList.

See the DrawOnComponent example from Custom Painting Approaches for an example of how this might be done.

Your code would be slightly different because you would need to create a ColoredShape class instead of the ColoredRectangle class because your code supports different types of shapes.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top