This program is supposed to output some shapes using the GUI. I can't get it to actually output the shapes, but the panel shows up fine. Could someone show me what I am doing wrong?

This one does the Panel and all that other stuff.

package pregui;
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Graphics;



public class PreGUI {

    
    public static void main(String[] args) {
        // TODO code application logic here
        JFrame myFrame = new JFrame();
        DrawShape myshape = new DrawShape();
        
        myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        myFrame.add(myshape);
        myFrame.setSize(200, 200);
        myFrame.setVisible(true);
        
    }
    
}
class DrawShape extends JPanel
{
    public void paintComponent (Graphics g)
    {
        g.setColor(Color. red);
        g.drawOval(70, 70, 25, 25);
        
    }
}

This one is where the shapes are made. I am not sure about the repaint() but it doesn't work without it either.

package guiproject;
import java.awt.*;
import javax.swing.*;

public class DrawClass extends JPanel {
   
    public void drawing ()
    {
        repaint();
    }
}

class Moon extends JPanel{

public void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(Color.RED);
g.fillOval(50,50,100,100);
g.setColor(Color.white);
g.fillArc(50, 50, 100, 100, 0, 180);
g.setColor(Color.black);
g.drawLine(50, 100, 150, 100);
}
}

If you tell me where in your PreGUI you call DrawClass and I will tell you why it is not drawing.

Oh sorry. I put in the wrong codes. The pregui codes aren't supposed to be there.
GUIProject is where the Panel and so on are made.

This class is where the shapes are drawn.

package guiproject;
import java.awt.*;
import javax.swing.*;
/**
 *
 * @author Adrian_King
 */
public class GUIProject {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        JFrame frame = new JFrame("Java GUI Project");
        frame.setVisible(true);
        frame.setSize(500, 500);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        DrawClass object = new DrawClass();
        JPanel panel = new JPanel();
        frame.add(object);
        frame.add(panel);
        
        
        object.drawing();
        
    }
}

You add a drawclass and call its method, but that draws nothing. The drawing code is in class Moon.

Oh, I see. How would I call the Moon class? Also, if you could tell me if the repaint() is needed, or a webpage that explains what it is. I was under the impression that repaint would end up calling Moon.

You need to create an instance of Moon and add it to your main window somewhere.
repaint() tells Java that you want your panel to be repainted. It will result in Java calling your class's paintComponent method, which is where & when the actual repaint will happen. If you don't call repaint() then Java won't know that you need to repaint the panel, and your paintComponent may not get called.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.