This is my Circle class
import java.awt.*;
public class Circle
{
private int diameter, x, y;
private Color color;
private String name, number;
public Circle(int x, int y, String number, String name, Color color,int diameter)
{
this.x = x;
this.y = y;
this.name = name;
this.color = color;
this.number = number;
this.diameter = diameter;
}
public void draw(Graphics page)
{
page.setColor(color);
page.fillOval(x, y, diameter, diameter);
page.drawString(name, x, y);
page.drawString(number, x, y);
page.drawRect(150, 100, 30, 100); // rectangle
page.fillRect(150, 100, 30, 100);
}
}
This is my TablePanel
import javax.swing.*;
import java.awt.*;
public class TablePanel extends JPanel
{
private Circle circle1, circle2, circle3, circle4, circle5, circle6;
public TablePanel()
{
circle1 = new Circle(150, 60,"1", "Murray", Color.blue, 30);
circle2 = new Circle(210, 100,"2", "Anne", Color.pink, 30);
circle3 = new Circle(210, 190,"3", "Roger", Color.blue, 30);
circle4 = new Circle(150, 220, "4", "Bella", Color.pink, 30);
circle5 = new Circle(90, 190,"5", "Colin", Color.blue, 30);
circle6 = new Circle(90, 100,"6", "Josie", Color.pink, 30);
setPreferredSize (new Dimension(300, 300));
setBackground(Color.white);
}
public void paintComponent (Graphics page)
{
super.paintComponent(page);
circle1.draw(page);
circle2.draw(page);
circle3.draw(page);
circle4.draw(page);
circle5.draw(page);
circle6.draw(page);
}
}
And finally this is my application class
import javax.swing.JFrame;
public class Table
{
public static void main (String[] args)
{
JFrame frame = new JFrame("Table Setting");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new TablePanel());
frame.pack();
frame.setVisible(true);
}
}
It is suppose to show seating arrangements around a table with different colors for different gender, this works perfectly fine when i run however i cannot seem to put the names and the number seat in the middle of the circle it is right on top. Any suggestion how to fix this?
That should result in the
Stringbeing rendered with the lower left corner in the center of the circle. To get it centered, it will be necessary to account for the width & height of the renderedString. For the latter, useFontMetricsorTextLayout.