CHENGJIE LIN
CHENGJIE LIN

Reputation: 21

Can't draw a oval in the jpanel when a button is clicked

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

import javax.swing.*;

import reactiontimer.ReactionTimer;

class MyPanel1 extends JPanel
{

    boolean showEllipse;


    protected void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D)g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                            RenderingHints.VALUE_ANTIALIAS_ON);
        g2.setPaint(Color.red);
        g2.drawOval(0, 0, 36, 36);
    }


}

public class DrawOval extends JFrame implements ActionListener
{
    Boolean flag1=false;
    Boolean flag2=false;
    Boolean flag3=false;
    JButton name;
    JButton oval;
    JButton rectangle;
    JPanel centerPanel;
    MyPanel1 leftPanel;
    JPanel rightPanel;
    JLabel centerLabel;
    JPanel botPanel;
    JLabel label1;
    JLabel label2;
    JLabel label3;

    public static void main(String args[])
    {
        // [Add Your Name and section number Below]
        Gui rt = new Gui("Chengjie Lin, Section  012");
        rt.setVisible(true);
    }

    public DrawOval(String title)
    {
        setSize(600, 600);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle(title);
        this.setVisible(true);

        Font buttonFont = new Font(Font.SANS_SERIF, Font.BOLD, 24);
        Container contentPane = this.getContentPane();
        contentPane.setLayout(new BorderLayout());
        botPanel=new JPanel();

        name = new JButton("Name");
        name.setFont(buttonFont);
        botPanel.add(name);
        //
        oval = new JButton("Oval");
        oval.setFont(buttonFont);
        botPanel.add(oval);
        //
        rectangle = new JButton("Rectangle");
        rectangle.setFont(buttonFont);
        botPanel.add(rectangle);

        contentPane.add(botPanel, BorderLayout.SOUTH);

        Font bigLabelFont = new Font(Font.SANS_SERIF, Font.BOLD, 48);
        label1=new JLabel("1");
        label1.setFont(bigLabelFont);
        label1.setHorizontalAlignment(SwingConstants.CENTER);

        label2=new JLabel("2");
        label2.setFont(bigLabelFont);
        label2.setHorizontalAlignment(SwingConstants.CENTER);

        label3=new JLabel("3");
        label3.setFont(bigLabelFont);
        label3.setHorizontalAlignment(SwingConstants.CENTER);

        centerPanel = new JPanel();
        leftPanel=new MyPanel1();
        rightPanel=new JPanel();
        leftPanel.setLayout(new BorderLayout());
        rightPanel.setLayout(new BorderLayout());
        centerPanel.setLayout(new BorderLayout());
    //  centerPanel.add(label1, BorderLayout.EAST);
        centerPanel.add(label2, BorderLayout.CENTER);
    //  centerPanel.add(label3, BorderLayout.WEST);
        contentPane.add(leftPanel, BorderLayout.EAST);
        contentPane.add(centerPanel, BorderLayout.CENTER);
        contentPane.add(rightPanel, BorderLayout.WEST);


        name.addActionListener(this);
        oval.addActionListener(this);
        rectangle.addActionListener(this);

    }

    public void actionPerformed(ActionEvent e)
    {
        Object source = e.getSource();
        String command = e.getActionCommand();
        System.out.println(command);

        // [Add Menu Code 3]
        if (source == name)
        {
            updateView1();
        }
        if (source == oval)
        {
         updateView2();
        }
        if(source==rectangle){


        }


    }

    void updateView1()
    {
        // [ADD Center Panel Code 2]
    if(flag2==false){
        label2.setText("cj");
    }else{
        label2.setText(null);   
    }
        flag2=!flag2;
    repaint();
    }


    void updateView2()
    {
        // [ADD Center Panel Code 2]
        //leftPanel.add(label1);
    if(flag1==false){


    }else{
        //leftPanel.removeAll();

    }
        flag1=!flag1;
    leftPanel.repaint();
    }

}

I am new to the Java.This might be a stupid question to ask.I tried to draw an oval in the leftPanel when the oval button is clicked. But nothing happened when I clicked the Button. Can someone help me out plz?

Upvotes: 0

Views: 77

Answers (1)

camickr
camickr

Reputation: 324088

Your MyPanel1 panel has a size of (0, 0) so there is nothing to paint.

Override the getPreferredSize() method of the class to return the Dimension of your custom painting.

Read the section from the Swing tutorial on Custom Painting for more information and a working example.

Upvotes: 1

Related Questions