Here's my code:
for (int count = 0; count < 7; count++) {
int z=0;
z++;
label = new JLabel(Integer.toString(z));
label.setLocation(12, 32);
add(label,Color.white);
}
I have two problems. One is with location. It is at upward center while I want (12,32). Other is with increment of z; it's not increasing and remains fixed at one. Output.
Alternatively, tell me an easy way to perform this task. I want labels in bullseye from 1-10 in each oval.
This is the full code if you not understand:
package bullseye;
import javax.swing.JPanel; import java.awt.*;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class BullsEye extends JPanel {
private JLabel label;
public BullsEye() {
setBackground(Color.white);
setPreferredSize(new Dimension(600,600));
for (int count = 0; count < 7; count++) {
int z=0;
z++;
label = new JLabel(Integer.toString(z));
label.setLocation(12, 32);
add(label,Color.white);
}
}
public void paintComponent(Graphics page) {
super.paintComponent(page);
Graphics2D g2 = (Graphics2D) page;
g2.setStroke(new BasicStroke(3));
//Graphics 2D is used to increase stroke of white ovals.
//As we place white ovals as draw (not fill), and not use draw command for black ovals.
//So, stroke of white ovals only increase to 3 from 1 (default value).
int x = 150, y = 150, diameter = 300;
for (int count = 0; count < 7; count++) {
page.setColor(Color.white);
page.drawOval(x, y, diameter, diameter);
page.setColor(Color.black);
page.fillOval(x, y, diameter, diameter);
diameter = diameter-50;
x = x+25;
y = y+25;
}
page.setColor(Color.white);
page.fillOval(300-(10/2), 300-(10/2) , 10 , 10);
//Centre of frame is 300, 300. But oval starts its first point from 300, 300.
//As dimensions of oval are 10, 10. So, it means they have diametre of 10, 10 and radius of 5.
//So, from this we know centre is at 300-5, 300-5.
x = 250/2; y = 250/2; diameter = 350;
for (int count = 0; count < 3; count++){
page.setColor(Color.black);
page.drawOval(x, y, diameter, diameter);
diameter = diameter+50;
x = x-25;
y=y-25;
}
}
public static void main(String[] args) {
JFrame frame = new JFrame("Bullseye");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
BullsEye panel = new BullsEye();
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true); }
}