lundi 6 novembre 2017

adding random shapes Rectangles, Ellipses and lines in JAVA GUI

Hey there i'm trying to design a GUI program ShapeFactory that has Buttons to produce :

a. Random Rectangles with random colors.

b. Random Ellipses with random colors.

c. Random Lines with random colors.

Whenever user clicks on a button, the corresponding shape will be added to the already drawn shapes in the JFrame.

and i'm stuck at trying to connect the ActionListener with the paint methode

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

public class ShapeFactory extends JFrame implements ActionListener{

    public static void main(String[] args) {
        new ShapeFactory();
}

    JButton ranRects = new JButton("Random Rectangles");
    JButton ranElli = new JButton("Random Ellipses");
    JButton ranLines = new JButton("Random Lines");
    Graphics g;
    Color c;

public ShapeFactory(){
    super("Shape Factory");
    setLayout(new FlowLayout());
    setSize(600,400);
    setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    add(ranRects);
    add(ranElli);
    add(ranLines);

    JPanel nPanel = new JPanel (new GridLayout(1,3));
    nPanel.add(ranRects);
    nPanel.add(ranElli);
    nPanel.add(ranLines);

    add(nPanel);


    ranRects.addActionListener(this);

    ranElli.addActionListener(this);        

    ranLines.addActionListener(this);


    setVisible(true);
}

    void paint (Graphics g) {
        super.paint(g);
        paintRect(g);
        paintOval(g);
        paintLine(g);
    }

    public void paintRect(Graphics g) {

        int rectX1 = (int)(Math.random() * getWidth() / 4.0);
        int rectY1 = (int)(Math.random() * getHeight()/ 4.0);
        int rectX2 = (int)(Math.random() * getWidth());
        int rectY2 = (int)(Math.random() * getHeight());
        c = new Color( (int) (Math.random() * 255) , (int) (Math.random() * 255) , (int) (Math.random() * 255));
        g.setColor(c);
        g.fillRect(rectX1,rectY2,rectX2,rectY2);
    }
    public void paintOval(Graphics g) {

        int ciX1 = (int)(Math.random() * getWidth() / 4.0);
        int ciY1 = (int)(Math.random() * getHeight()/ 4.0);
        int ciX2 = (int)(Math.random() * getWidth());
        int ciY2 = (int)(Math.random() * getHeight());
        c = new Color( (int) (Math.random() * 255) , (int) (Math.random() * 255) , (int) (Math.random() * 255));
        g.setColor(c);
        g.fillOval(ciX1,ciY1,ciX2,ciY2);
    }
    public void paintLine(Graphics g) {

        int lineX1 = (int)(Math.random() * getWidth() / 4.0);
        int lineY1 = (int)(Math.random() * getHeight()/ 4.0);
        int lineX2 = (int)(Math.random() * getWidth());
        int lineY2 = (int)(Math.random() * getHeight());
        c = new Color( (int) (Math.random() * 255) , (int) (Math.random() * 255) , (int) (Math.random() * 255));
        g.setColor(c);
        g.drawLine(lineX1,lineY1,lineX2,lineY2);
    }

    public void actionPerformed(ActionEvent e) {
        Object source = e.getSource();
        if (source == ranRects) {
            repaint();

        }

        else if (source == ranElli) {
            repaint(); 

        }
        else {
            repaint();

        }


    }

}




Aucun commentaire:

Enregistrer un commentaire