1

This is for my university project and I am busting my brain around why my Java GUI does not work. This is the situation: the code compiles and executes without an issue.

This code should create 300 X 300 frame center the desktop and create circle and it print my name underneath.

I got it working until the frame, but no circle

package gui;
import javax.swing.*;
import java.awt.*;
import javax.swing.JFrame;

public class GUI  extends JFrame{



     public void Paint (Graphics g){
      super.paintComponents(g);
      g.setColor(Color.yellow);
      g.fillOval(50, 50, 200, 200);
      g.setColor(Color.BLACK);
       g.drawArc(75, 60, 150, 150, -25, -125);
  g.fillOval(100, 100, 25, 25);
  g.fillOval(175, 100, 25, 25);
  g.setColor(Color.BLUE);   
  g.setFont(new Font("Serif", Font.BOLD,18));
  g.drawString("My Nanme is BOB", 33, 275);


}



/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    GUI GUI = new GUI() ;
    GUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    GUI.setSize(300,300);
    GUI.setTitle("BOB's GUI App");
    GUI.setVisible(true);
    GUI.setLocationRelativeTo(null);

enter image description here

I really appreciate your output. also please give me a hint why it does not work

azurefrog
  • 10,785
  • 7
  • 42
  • 56
user3214631
  • 43
  • 1
  • 5
  • 1
    While this is just a small typo with capitalization causing the issue, I'd recommend adding an overridden JPanel to the frame and putting your painting methods in `JPanel.paintComponent(Graphics g)` – phflack Dec 30 '15 at 15:35
  • In Java, camel case is followed while naming a method or class. So, your "P" in "paint" must be in small case letters – Ashish Ani Dec 30 '15 at 16:12
  • You can take help from these tutorials : http://www.herongyang.com/Swing/JFrame-Draw-Graphics-paint-on-Frame.html – Ashish Ani Dec 30 '15 at 16:14

3 Answers3

1

Java is case-sensitive :

public void Paint (Graphics g)

Will never override

public void paint (Graphics g)
Arnaud
  • 17,229
  • 3
  • 31
  • 44
0

Your paint() method should have a lowercase 'P'. As you currently have it, you're not overriding the existing JFrame method.

To avoid such issues in the future, I would investigate the @Overrides annotation. If you use this on the above, it will tell you you're not overriding anything!

Community
  • 1
  • 1
Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
0

In your main, you have written

GUI GUI = new GUI() ;

You should make the second GUI something else. For example,

GUI gui = new GUI();
Bernhard Colby
  • 311
  • 5
  • 17