zey
zey

Reputation: 23

Why can't I paint on the panel

I have made three panels south, east and north, I am trying to draw a circle on the north panel but I just can't figure out why it is not drawing. Here is my code: What I want is a small application that draws circles of different sizes and colors chosen by the user.

import com.sun.prism.shader.DrawCircle_Color_Loader;

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;


public class drawing extends JFrame implements MouseListener{

   private int r = 255, g = 0, b = 0;
   private JSlider colorSlider, redSlider,greenSlider,blueSlider;
   private JLabel colorLabel,redLabel,greenLabel,blueLabel;
   private int x = 50;
   private int y = 50;


public drawing(){
    JFrame frame = new JFrame("Drawing App");
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    frame.setSize(800,800);

    Container contentPane = frame.getContentPane();


    JMenuBar mb = new JMenuBar();
    frame.setJMenuBar(mb);

    JMenu color = new JMenu("Colour");
    JMenu size = new JMenu("Size");

    mb.add(color);
    mb.add(size);
    JMenuItem colorRed = new JMenuItem("Red");
    JMenuItem colorBlue = new JMenuItem("Blue");
    JMenuItem colorGreen = new JMenuItem("Green");
    color.add(colorBlue);
    color.add(colorGreen);
    color.add(colorRed);

    JMenuItem one = new JMenuItem("1");
    JMenuItem two = new JMenuItem("2");
    JMenuItem three = new JMenuItem("3");
    JMenuItem four = new JMenuItem("4");
    JMenuItem five = new JMenuItem("5");
    size.add(one);
    size.add(two);
    size.add(three);
    size.add(four);

    JPanel panel = new JPanel();
    setBackground(Color.WHITE);
    contentPane.add(panel,BorderLayout.NORTH);

    JPanel panel1 = new JPanel();
    contentPane.add(panel1,BorderLayout.SOUTH);

    JPanel panel2 = new JPanel();
    contentPane.add(panel2,BorderLayout.EAST);

    panel1.setLayout(new GridLayout(0,1));

   JColorChooser colors = new JColorChooser();
    panel1.add(colors);

   frame.setVisible(true);

    panel.add(panel);
    }

  public void paint(Graphics g)
   {
     g.setColor(Color.red);
     g.fillOval(x,y,100,100);
   }
  public void mousePressed(MouseEvent e) {}
  public void mouseReleased(MouseEvent e) {}
  public void mouseEntered(MouseEvent e) {}
  public void mouseExited(MouseEvent e) {}

  public void mouseClicked(MouseEvent e) {
     x = e.getX();
     y = e.getY();
     repaint();

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

    drawing Drawing = new drawing();

 }

}

Upvotes: 0

Views: 74

Answers (2)

Bon
Bon

Reputation: 3103

You created another instance of JFrame inside the constructor drawing()

  1. Remove that instance and replace with this for all the initialization inside the constructor.
  2. add super.paint(g); to the first line of your public void paint(Graphics g) method.

Upvotes: 1

Andrew Thompson
Andrew Thompson

Reputation: 168825

A panel with no content has a default size of 0x0, so the only way you might see such a panel is if the layout stretches it in both height and width. It is best to override the getPreferredSize() method of a custom painted panel to return a sensible size, then pack() the top level container.

Upvotes: 1

Related Questions