user1924247
user1924247

Reputation: 171

Trying to draw on a JPanel without success

I'm trying to learn drawing with swing. I'm trying to create a circle and position it on a specific position in a JPanel. This is what I've come up with but it does not show any figure:

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

public class Circles extends JPanel {
private static final long serialVersionUID = 1L;

public Circles(){
    setBackground(Color.white);
    setPreferredSize(new Dimension(300, 300));
}

public void paintComponent(Graphics g){
    super.paintComponents(g);
    g.setColor(Color.black);
    g.drawRect(10, 10, 50, 50);
}
}

This is the class that starts the program.

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

public class StartCircles extends JFrame{
private static final long serialVersionUID = 1L;

private Circles circle;
public StartCircles(){
    Container c = getContentPane();
    circle = new Circles();
    c.setBackground(Color.white);
    c.setLayout(new FlowLayout());
    setSize(300, 300);
    c.add(circle);
    setVisible(true);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
}

public static void main(String[] args){
    StartCircles c1 = new StartCircles();
}
}

What am I doing wrong? How do I position the figure where I want?

Upvotes: 1

Views: 98

Answers (3)

Andrew Thompson
Andrew Thompson

Reputation: 168825

Fixed

Try this SSCCE - noting the comments in the source.

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

public class StartCircles extends JFrame{
private static final long serialVersionUID = 1L;

private Circles circle;
public StartCircles(){
    Container c = getContentPane();
    circle = new Circles();
    c.setBackground(Color.white);
    c.setLayout(new FlowLayout());
    //setSize(300, 300);
    c.add(circle);
    setVisible(true);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    pack(); // size the GUI to the components within it
}

public static void main(String[] args){
    StartCircles c1 = new StartCircles();
}
}

class Circles extends JPanel {
private static final long serialVersionUID = 1L;

public Circles(){
    setBackground(Color.white);
    setPreferredSize(new Dimension(300, 300));
}

public void paintComponent(Graphics g){
    //super.paintComponents(g);  // WRONG METHOD!
    super.paintComponent(g);
    g.setColor(Color.black);
    g.drawRect(10, 10, 50, 50);
}
}

Upvotes: 3

An SO User
An SO User

Reputation: 24998

g.drawRect(10, 10, 50, 50); is sure to get you a rectangle. You need to look at the documentation for how to draw a circle.

You need to draw it using drawOval(). It takes 4 parameters:

x - the x coordinate of the upper left corner of the oval to be drawn.
y - the y coordinate of the upper left corner of the oval to be drawn.
width - the width of the oval to be drawn.
height - the height of the oval to be drawn.

So, replace your drawRect() with drawOval()

Upvotes: 2

HackerGK
HackerGK

Reputation: 370

Your code running perfectly, but you are drawing Rectangle here. so if you want to draw circle, you have to use

one of below methods

g.fillOval(10, 10, 50, 50);

g.drawOval(10, 10, 50, 50);

Upvotes: 3

Related Questions