//The templates: import java.awt.Color; import java.awt.event.ActionEvent; impor
ID: 3834685 • Letter: #
Question
//The templates:
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.border.Border;
public class ControlPanel extends JPanel {
//ToDO
}
//---------------------------------------------------------------------------------
import java.awt.BorderLayout;
import javax.swing.JFrame;
public class Reversi extends JFrame {
// TODO
}
//---------------------------------------------------------------------------------
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JPanel;
public class ReversiBoard extends JPanel{
//TODO
}
//---------------------------------------------------------------------------------
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import javax.swing.JButton;
public class RoundedButton extends JButton {
private Color currentColor = Color.lightGray;
public RoundedButton() {
super();
setContentAreaFilled(false);
}
public boolean isWhite()
{
return currentColor.equals(Color.white);
}
public boolean isGray()
{
return currentColor.equals(Color.lightGray);
}
public boolean isBlack()
{
return currentColor.equals(Color.darkGray);
}
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D)g;
// Enable antialiasing for shapes
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
// Draw a border
g2d.setBackground(Color.black);
g2d.setColor(Color.black);
g2d.fillOval(4,4,getWidth()-8,getHeight()-8);
// draw the current color
g2d.setBackground(currentColor);
g2d.setColor(currentColor);
g2d.fillOval(4,4,getWidth()-10,
getHeight()-10);
}
public void setWhite()
{
currentColor = Color.white;
}
public void setGray()
{
currentColor = Color.lightGray;
}
public void setBlack()
{
currentColor = Color.darkGray;
}
}
Explanation / Answer
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.border.Border;
public class ControlPanel extends JPanel {
//ToDO
}
//---------------------------------------------------------------------------------
import java.awt.BorderLayout;
import javax.swing.JFrame;
public class Reversi extends JFrame {
// TODO
}
//---------------------------------------------------------------------------------
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JPanel;
public class ReversiBoard extends JPanel{
//TODO
}
//---------------------------------------------------------------------------------
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import javax.swing.JButton;
public class RoundedButton extends JButton {
private Color currentColor = Color.lightGray;
public RoundedButton() {
super();
setContentAreaFilled(false);
}
public boolean isWhite()
{
return currentColor.equals(Color.white);
}
public boolean isGray()
{
return currentColor.equals(Color.lightGray);
}
public boolean isBlack()
{
return currentColor.equals(Color.darkGray);
}
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D)g;
// Enable antialiasing for shapes
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
// Draw a border
g2d.setBackground(Color.black);
g2d.setColor(Color.black);
g2d.fillOval(4,4,getWidth()-8,getHeight()-8);
// draw the current color
g2d.setBackground(currentColor);
g2d.setColor(currentColor);
g2d.fillOval(4,4,getWidth()-10,
getHeight()-10);
}
public void setWhite()
{
currentColor = Color.white;
}
public void setGray()
{
currentColor = Color.lightGray;
}
public void setBlack()
{
currentColor = Color.darkGray;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.