user6569393
user6569393

Reputation:

java Swing button action

I am new in java and i wanted to work on a simple paint program using java swing. my simple paint program should draw a shape like triangle, circle and square whenever i clicked on buttons. i managed to draw these shapes and print it without buttons but i can not do it using ActionListener?

As you see i have a single button at the moment, i want to draw the oval whenever this button is clicked. This is the code that i am working on it so far:

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


public class PaintProject extends JComponent implements ActionListener{
    public static void main(String[] args) {


        JFrame frame=new JFrame("NEW PAINT PROGRAME!");
        JButton button1=new JButton("ADD");
        PaintProject paint=new PaintProject();

        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setLayout(new FlowLayout());
        frame.add(paint);
        frame.add(button1);

        frame.pack();
        frame.setVisible(true);

    }
    @Override
    public Dimension getPreferredSize(){
        return new Dimension(500,500);

    }
    @Override
    protected void paintComponent(Graphics g){
        super.paintComponent(g);
        g.setColor(Color.red);
        g.fillOval(0,0, 100, 100);

    }

    @Override
    public void actionPerformed(ActionEvent e) {


    }


}

Upvotes: 0

Views: 8899

Answers (1)

Sanjeev Saha
Sanjeev Saha

Reputation: 2652

Could you please take following steps:

Step 1:

Insert button1.addActionListener(paint); just after PaintProject paint=new PaintProject(); in the main method of PaintProject.java

Step 2:

Remove the method named protected void paintComponent(Graphics g). Instead create following method:

 private void drawOval(){
    Graphics g = this.getGraphics();
    g.setColor(Color.red);
    g.fillOval(0,0, 100, 100);        
 }

Step 3:

Call the above method as follows:

@Override
public void actionPerformed(ActionEvent e) {
     drawOval();      
}

EDIT:

Following example demonstrates how to draw two shapes when respective buttons are clicked:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.WindowConstants;

public class PaintProject extends JComponent implements ActionListener {
    public static void main(String[] args) {

        JFrame frame = new JFrame("NEW PAINT PROGRAME!");
        JButton ovalButton = new JButton("Oval");
        ovalButton.setActionCommand("Oval");

        JButton rectangleButton = new JButton("Rectangle");
        rectangleButton.setActionCommand("Rectangle");

        PaintProject paint = new PaintProject();
        ovalButton.addActionListener(paint);
        rectangleButton.addActionListener(paint);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setLayout(new FlowLayout());
        frame.add(paint);
        frame.add(ovalButton);
        frame.add(rectangleButton);

        frame.pack();
        frame.setVisible(true);

    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(500, 500);

    }

    private void drawOval() {
        Graphics g = this.getGraphics();
        g.setColor(Color.red);
        g.fillOval(0, 0, 100, 100);
    }

    private void drawRectangle() {
        Graphics g = this.getGraphics();
        g.setColor(Color.green);
        g.fillRect(150, 150, 100, 100);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        String command = e.getActionCommand();
        if (command.equals("Oval")) {
            drawOval();
        } else if (command.equals("Rectangle")) {
            drawRectangle();
        }

    }

}

Upvotes: 1

Related Questions