Reputation: 111
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?
Upvotes: 0
Views: 1594
Reputation: 1580
you need to offset the text; the coordinates are from the top left "corner" of the circle.
To get some information about the text I think you do something like this:
public void draw(Graphics g) {
FontMetrics metrics = g.getFontMetrics();
Rectangle2D rect = metrics.getStringBounds(text, g);
....
And from that rectangle you can get the width and height of whatever text you put in the method with getWidth() and getHeight(); Then you can can center it horizontally with
x = (diameter - rect.getWidth() ) / 2;
For the height, you can approximate with just taking the height and doing x2 because you have 2 lines, so
y = (diameter - rect.getHeight() * 2) / 2
Upvotes: 2
Reputation: 168835
int radius = diameter/2;
page.drawString(number, x+radius, y+radius);
That should result in the String
being 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 rendered String
. For the latter, use FontMetrics
or TextLayout
.
Upvotes: 3