The last Checkbox that was set to true should be true. All others, including those previously set to true, should be false. This behavior isn't very well defined. I've also seen setting more than one checkbox in a group to true simultaneously cause an application crash, though this theoretically shouldn't happen.
This is a tricky problem. Although it seems simple to add the constructors, you also need to rewrite the paint method to take into account the different possible sizes of the button. The version in the book hardcodes a lot of values that really need to be variables.
import java.awt.Graphics;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Event;
import java.awt.Font;
import java.awt.FontMetrics;
public class PanicButton extends Canvas {
int radius;
public PanicButton() {
radius = 50;
}
public PanicButton(int r) {
radius = r;
}
public void paint(Graphics g) {
g.setFont(new Font("Helvetica", Font.BOLD, 24));
FontMetrics fm = g.getFontMetrics();
int w = fm.stringWidth("Panic");
int h = fm.getHeight();
g.setColor(Color.red);
g.fillOval(0, 0, 2*radius, 2*radius);
g.setColor(Color.yellow);
g.drawString("Panic", radius-w/2, radius+h/2);
}
public boolean mouseUp(Event e, int x, int y) {
// Was the click inside the circle??
if (Math.sqrt( (x-radius)*(x-radius) +
(y-radius)*(y-radius)) <= radius) {
postEvent(new Event(this, Event.ACTION_EVENT, "Panic"));
return true;
}
else {
return false;
}
}
public Dimension minimumSize() {
return new Dimension(2*radius,2*radius);
}
public Dimension preferredSize() {
return minimumSize();
}
}
import java.awt.Graphics;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Event;
import java.awt.Font;
import java.awt.FontMetrics;
public class CircleButton extends Canvas {
int radius;
String label;
public CircleButton() {
label = "Button";
radius = 50;
}
public CircleButton(String s) {
label = s;
radius = 50;
}
public CircleButton(String s, int r) {
label = s;
radius = r;
}
public void paint(Graphics g) {
g.setFont(new Font("Helvetica", Font.BOLD, 12));
FontMetrics fm = g.getFontMetrics();
int w = fm.stringWidth(label);
int h = fm.getHeight();
g.setColor(Color.blue);
g.fillOval(0, 0, 2*radius, 2*radius);
g.setColor(Color.black);
g.drawString(label, radius-w/2, radius+h/2);
}
public boolean mouseUp(Event e, int x, int y) {
// Was the click inside the circle??
if (Math.sqrt( (x-radius)*(x-radius) +
(y-radius)*(y-radius)) <= radius) {
postEvent(new Event(this, Event.ACTION_EVENT, label));
return true;
}
else {
return false;
}
}
public Dimension minimumSize() {
return new Dimension(2*radius,2*radius);
}
public Dimension preferredSize() {
return minimumSize();
}
}
public String getLabel() {
return label;
}
public void setLabel(String s) {
label = s;
}
import java.awt.Graphics;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Event;
import java.awt.Font;
import java.awt.FontMetrics;
public class CircleButton extends Canvas {
int radius;
String label;
Color buttonColor;
Color textColor;
Font theFont;
public CircleButton() {
label = "Button";
radius = 50;
}
public CircleButton(String s) {
label = s;
radius = 50;
}
public CircleButton(String s, int r) {
label = s;
radius = r;
}
public String getLabel() {
return label;
}
public void setLabel(String s) {
label = s;
}
public void setFont(Font f) {
theFont = f;
}
public void setFont(String s) {
theFont = new Font(s, Font.BOLD, 12);
}
public void setColor(Color c) {
buttonColor = c;
}
public void setTextColor(Color c) {
textColor = c;
}
public void paint(Graphics g) {
g.setFont(theFont);
FontMetrics fm = g.getFontMetrics();
int w = fm.stringWidth(label);
int h = fm.getHeight();
g.setColor(buttonColor);
g.fillOval(0, 0, 2*radius, 2*radius);
g.setColor(textColor);
g.drawString(label, radius-w/2, radius+h/2);
}
public boolean mouseUp(Event e, int x, int y) {
// Was the click inside the circle??
if (Math.sqrt( (x-radius)*(x-radius) +
(y-radius)*(y-radius)) <= radius) {
postEvent(new Event(this, Event.ACTION_EVENT, label));
return true;
}
else {
return false;
}
}
public Dimension minimumSize() {
return new Dimension(2*radius,2*radius);
}
public Dimension preferredSize() {
return minimumSize();
}
}