exercise15-01
package chapter15;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class exercise15_01 extends JFrame {
public exercise15_01() {
add(new newpanel());
}
public static void main(String[] args) {
exercise15_01 frame = new exercise15_01();
frame.setTitle("exercise15-01");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setSize(315, 340);
}
}
@SuppressWarnings("serial")
class newpanel extends JPanel{
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.red);
g.drawLine(100, 0, 100, 300);
g.drawLine(200, 0, 200, 300);
g.setColor(Color.blue);
g.drawLine(0, 100, 300, 100);
g.drawLine(0, 200, 300, 200);
}
}
exercise15-02
package chapter15;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import javax.swing.JButton;
import javax.swing.JFrame;
@SuppressWarnings("serial")
public class exercise15_02 extends JFrame{
private OvalButton button1 = new OvalButton("OK");
private OvalButton button2 = new OvalButton("cancel");
public exercise15_02 (){
setLayout(new FlowLayout());
add(button1);
add(button2);
}
public static void main(String[] args) {
exercise15_02 frame = new exercise15_02();
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setTitle("exercise15.2");
}
}
@SuppressWarnings("serial")
class OvalButton extends JButton{
public OvalButton(){
}
public OvalButton(String string)
{
super(string);
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawOval(5, 5, getWidth()-10, getHeight()-10);
}
//override subclass method
public Dimension getPreferredSize(){
return new Dimension(100,50);//确定所画图中,希望的尺寸的大小
}
}
3.exercise 13 -03
package chapter15;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;
import javax.swing.JFrame;
@SuppressWarnings("serial")
public class exercise15_03 extends JFrame {
public exercise15_03() {
add(new mazz());
}
public static void main(String[] args) {
exercise15_03 frame = new exercise15_03();
frame.setTitle("exercise15.03");
frame.setLocationRelativeTo(null);
frame.setSize(300, 300);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
@SuppressWarnings("serial")
class mazz extends JPanel{
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int width = getWidth()/8;
int height = getHeight()/8;
int x = 0;
int y = 0;
boolean iswhile = true;
for(int i=0;i<8;i++){
x=0;
for(int j=0;j<8;j++){
if(iswhile){
g.setColor(Color.white);
iswhile = false;
}
else{
g.setColor(Color.black);
iswhile = true;
}
g.fillRect(x, y, width, height);
x+=width;
}
if(i%2==1){
iswhile = true;
}
else
iswhile = false;
y+=height;
}
}
}
package chapter15;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class exercise15_04 extends JFrame{
public exercise15_04 (){
add(new mix());
}
public static void main(String[] args) {
exercise15_04 frame = new exercise15_04();
frame.setTitle("exercise15.04");
frame.setLocationRelativeTo(null);
frame.setSize(300, 350);
frame.setVisible(true);
//frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
@SuppressWarnings("serial")
class mix extends JPanel{
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int x = 10;
int y = 50;
g.setColor(Color.red);
g.setFont(new Font("Times",Font.BOLD,20));
g.drawString("multiplication Table", x+60, y);
g.setFont(new Font("Times",Font.BOLD,15));
String s="";
for(int i=1;i<=9;i++){
s=s+ i+" " ;
}
g.drawString(s, x+62, y+20);//60 70
for(int i=1;i<=9;i++){
g.drawString(i+"", x+20, y+30+i*20);
}
x = 60;
y = 100;
g.drawRect(x-10, y-25, 200, 200);
s="";
for(int i=1;i<=9;i++){
for(int j=1;j<=9;j++){
if(i*j<10){
s =s+ " "+i*j;
}
else{
s =s+" "+ i*j;
}
}
g.drawString(s, x, y);
s="";
y+=20;
}
}
}
exercise15-06
package chapter15;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Polygon;
import javax.swing.JFrame;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class exercise15_06 extends JFrame{
public exercise15_06(){
setLayout(new GridLayout(2,2));
add(new FigurePanel2(1,0));
add(new FigurePanel2(2,0));
add(new FigurePanel2(1,1));
add(new FigurePanel2(2,1));
}
public static void main(String[] args) {
exercise15_06 frame = new exercise15_06();
frame.setSize(400, 400);
frame.setTitle("TestFigurePanel+++");
frame.setLocationRelativeTo(null); // Center the frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
@SuppressWarnings("serial")
class FigurePanel2 extends JPanel{
private int isfill = 0;
private int shape = 1;
public FigurePanel2(){
}
public FigurePanel2(int shape){
this.shape = shape;
}
public FigurePanel2(int shape,int isfill){
this.isfill = isfill;
this.shape = shape;
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int width = getWidth();
int height = getHeight();
g.setColor(Color.red);
if(shape==1){
if(isfill==0){
g.drawArc(5, 5, width-10, height-10, 0, 30);
g.drawArc(5, 5, width-10, height-10, 90, 30);
g.drawArc(5, 5, width-10, height-10, 180, 30);
g.drawArc(5, 5, width-10, height-10, 270, 30);
}
if(isfill==1)
{
g.fillArc(5, 5, width-10, height-10, 0, 30);
g.fillArc(5, 5, width-10, height-10, 90, 30);
g.fillArc(5, 5, width-10, height-10, 180, 30);
g.fillArc(5, 5, width-10, height-10, 270, 30);
}
}
if(shape == 2){
if(isfill==0){
int xCenter = getSize().width/2;
int yCenter = getSize().height/2;
// int xCenter = getWidth()/2-30;
// int yCenter = getHeight()/2-10;
int radius =(int)(Math.min(getSize().width, getSize().height)*0.4);
// Create a Polygon object
Polygon polygon = new Polygon();
// Add points to the polygon
polygon.addPoint(xCenter + radius, yCenter);
polygon.addPoint((int)(xCenter + radius*Math.cos(2*Math.PI/6)),
(int)(yCenter - radius*Math.sin(2*Math.PI/6)));
polygon.addPoint((int)(xCenter + radius*Math.cos(2*2*Math.PI/6)),
(int)(yCenter - radius*Math.sin(2*2*Math.PI/6)));
polygon.addPoint((int)(xCenter + radius*Math.cos(3*2*Math.PI/6)),
(int)(yCenter - radius*Math.sin(3*2*Math.PI/6)));
polygon.addPoint((int)(xCenter + radius*Math.cos(4*2*Math.PI/6)),
(int)(yCenter - radius*Math.sin(4*2*Math.PI/6)));
polygon.addPoint((int)(xCenter + radius*Math.cos(5*2*Math.PI/6)),
(int)(yCenter - radius*Math.sin(5*2*Math.PI/6)));
// Draw the polygon
g.drawPolygon(polygon);
}
else{
int xCenter = getSize().width/2;
int yCenter = getSize().height/2;
int radius =
(int)(Math.min(getSize().width, getSize().height)*0.4);
// Create a Polygon object
Polygon polygon = new Polygon();
// Add points to the polygon
polygon.addPoint(xCenter + radius, yCenter);
polygon.addPoint((int)(xCenter + radius*Math.cos(2*Math.PI/6)),
(int)(yCenter - radius*Math.sin(2*Math.PI/6)));
polygon.addPoint((int)(xCenter + radius*Math.cos(2*2*Math.PI/6)),
(int)(yCenter - radius*Math.sin(2*2*Math.PI/6)));
polygon.addPoint((int)(xCenter + radius*Math.cos(3*2*Math.PI/6)),
(int)(yCenter - radius*Math.sin(3*2*Math.PI/6)));
polygon.addPoint((int)(xCenter + radius*Math.cos(4*2*Math.PI/6)),
(int)(yCenter - radius*Math.sin(4*2*Math.PI/6)));
polygon.addPoint((int)(xCenter + radius*Math.cos(5*2*Math.PI/6)),
(int)(yCenter - radius*Math.sin(5*2*Math.PI/6)));
// Draw the polygon
g.fillPolygon(polygon);
}
}
}
}
package chapter15;
import java.awt.Graphics;
import java.awt.*;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class DisplayImage extends JFrame{
public DisplayImage (){
add(new ImagePanel());
}
public static void main(String[] args) {
JFrame frame = new DisplayImage();
frame.setTitle("aaa");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setSize(250, 180);
frame.setVisible(true);
}
}
@SuppressWarnings("serial")
class ImagePanel extends JPanel{
private ImageIcon imageIcon = new ImageIcon("image/china.gif");
private Image image = imageIcon.getImage();
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if(image!=null)
g.drawImage(image, 0, 0, getWidth(), getHeight(), this);
}
}
package chapter15;
import java.awt.Graphics;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class exercise15_07 extends JFrame{
public exercise15_07 (){
setLayout(new GridLayout(3,3));
for(int i =1;i<=9;i++){
add(new jingGame((int)(0+Math.random()*3)));
}
}
public static void main(String[] args) {
exercise15_07 frame = new exercise15_07();
frame.setSize(300, 300);
frame.setTitle("Exercise15_15");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null); // Center the frame
frame.setVisible(true);
}
}
@SuppressWarnings("serial")
class jingGame extends JPanel{
private static final int line = 1;
private static final int ovel = 2;
private static final int nul = 0;
private int shape = 0;
public jingGame(){
}
public jingGame(int shape){
this.shape = shape;
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int width = getWidth();
int height = getHeight();
switch(shape)
{
case line:
g.drawLine(5, 5, width-10, height-10);
g.drawLine(5, height-10, width-10, 5);
break;
case nul:
g.drawLine(0, 0, 1, 1);
break;
case ovel:
g.drawArc(5, 5, width-10, height-10, 0, 360);
break;
default:
break;
}
}
}
package chapter15;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Polygon;
import javax.swing.JFrame;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class exercise15_08 extends JFrame{
public exercise15_08 (){
add(new eight());
}
public static void main(String[] args) {
exercise15_08 frame = new exercise15_08();
frame.setSize(300, 300);
frame.setTitle("Exercise15_8");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null); // Center the frame
frame.setVisible(true);
}
}
@SuppressWarnings("serial")
class eight extends JPanel{
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int radius = (int)(Math.min(getWidth(), getHeight())*0.4);
int x = getWidth()/2;
int y = getHeight()/2;
Polygon polygon = new Polygon();
polygon.addPoint(x+radius, y);
polygon.addPoint((int)(x+radius*Math.cos(2*Math.PI/8)), (int)(y-radius*Math.sin(2*Math.PI/8)));
polygon.addPoint((int)(x+radius*Math.cos(2*2*Math.PI/8)), (int)(y-radius*Math.sin(2*2*Math.PI/8)));
polygon.addPoint((int)(x+radius*Math.cos(3*2*Math.PI/8)), (int)(y-radius*Math.sin(2*3*Math.PI/8)));
polygon.addPoint((int)(x+radius*Math.cos(4*2*Math.PI/8)), (int)(y-radius*Math.sin(2*4*Math.PI/8)));
polygon.addPoint((int)(x+radius*Math.cos(5*2*Math.PI/8)), (int)(y-radius*Math.sin(2*5*Math.PI/8)));
polygon.addPoint((int)(x+radius*Math.cos(6*2*Math.PI/8)), (int)(y-radius*Math.sin(2*6*Math.PI/8)));
polygon.addPoint((int)(x+radius*Math.cos(7*2*Math.PI/8)), (int)(y-radius*Math.sin(2*7*Math.PI/8)));
g.setColor(Color.magenta);
g.drawPolygon(polygon);
}
}
package chapter15;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class exercise15_09 extends JFrame{
public exercise15_09(){
setLayout(new GridLayout(2,2));
for(int i=0;i<=3;i++){
add(new nine());
}
}
public static void main(String[] args) {
exercise15_09 frame = new exercise15_09();
frame.setSize(300, 300);
frame.setTitle("Exercise15_9");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null); // Center the frame
frame.setVisible(true);
}
}
@SuppressWarnings("serial")
class nine extends JPanel{
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int x = getWidth();
int y = getHeight();
// int radius = (int)(Math.min(getWidth(), getHeight())*0.4);
g.setColor(Color.pink);
g.drawOval(5, 5, x-10, y-10);
g.fillArc(10, 10, x-20, y-20, 0, 30);
g.fillArc(10, 10, x-20, y-20, 90, 30);
g.fillArc(10, 10, x-20, y-20, 180, 30);
g.fillArc(10, 10, x-20, y-20, 270, 30);
}
}
package chapter15;
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.Polygon;
import javax.swing.JFrame;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class exercise15_11 extends JFrame{
public exercise15_11 (){
setLayout(new BorderLayout());
add(new eleven(), BorderLayout.CENTER);
}
public static void main(String[] args) {
exercise15_11 frame = new exercise15_11();
frame.setSize(500, 500);
frame.setTitle("Exercise15_7");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null); // Center the frame
frame.setVisible(true);
}
}
@SuppressWarnings("serial")
class eleven extends JPanel{
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawLine(10, 200, 390, 200);
g.drawLine(200, 30, 200, 390);
// Draw arrows
g.drawLine(390, 200, 370, 190);
g.drawLine(390, 200, 370, 210);
g.drawLine(200, 30, 190, 50);
g.drawLine(200, 30, 210, 50);
g.drawString("X", 390, 170);
g.drawString("Y", 220, 40);
Polygon p = new Polygon();
double scaleFactor = 0.01;
for(int x=-100;x<=100;x++){
p.addPoint(x+200, 200-(int)(scaleFactor*x*x));
}
g.drawPolyline(p.xpoints,p.ypoints,p.npoints);
}
}
package chapter15;
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.Polygon;
import javax.swing.JFrame;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class exercise15_12 extends JFrame{
public exercise15_12 (){
setLayout(new BorderLayout());
add(new twelve(),BorderLayout.CENTER);
}
public static void main(String[] args) {
exercise15_12 frame = new exercise15_12();
frame.setSize(400, 300);
frame.setTitle("Exercise15_12");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null); // Center the frame
frame.setVisible(true);
}
}
@SuppressWarnings("serial")
class twelve extends JPanel{
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawLine(10, 100, 380, 100);
g.drawLine(200, 30, 200, 190);
// Draw arrows
g.drawLine(380, 100, 370, 90);
g.drawLine(380, 100, 370, 110);
g.drawLine(200, 30, 190, 40);
g.drawLine(200, 30, 210, 40);
// Draw x, y axises
g.drawString("X", 360, 80);
g.drawString("Y", 220, 40);
Polygon p = new Polygon();
for(int x=-100;x<=100;x++){
p.addPoint(x+200 ,100- (int)(50 * Math.sin((x / 100.0) * 2 * Math.PI)));
}
g.drawString("-2\u03c0", 95, 115);
g.drawString("2\u03c0", 305, 115);
g.drawString("0", 200, 115);
g.drawPolyline(p.xpoints, p.ypoints, p.npoints);
}
}
package chapter15_编程练习题;
import java.awt.*;
import javax.swing.*;
@SuppressWarnings("serial")
public class Exercise15_15 extends JFrame {
public static void main(String[] args) {
JFrame frame = new Exercise15_15();
frame.setSize(300, 300);
frame.setTitle("Exercise15_15");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null); // Center the frame
frame.setVisible(true);
}
public Exercise15_15() {
add(new PieChart1());
}
}
@SuppressWarnings("serial")
class PieChart1 extends JPanel {
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int w = getWidth();
int h = getHeight();
int xCenter = w / 2;
int yCenter = h / 2;
int radius = (int)(Math.min(w, h) * 0.8 / 2);
int x = xCenter - radius;
int y = yCenter - radius;
// Draw the arc for projects
g.setColor(Color.red);
g.fillArc(x, y, 2 * radius, 2 * radius, 0, (int)(20 * 360 / 100));
g.setColor(Color.black);
g.drawString("Projects -- 20%",
(int)(xCenter + radius*Math.cos(2 * Math.PI * 0.1)),
(int)(yCenter - radius*Math.sin(2 * Math.PI * 0.1)));
// Draw the arc for quizzes
g.setColor(Color.blue);
g.fillArc(x, y, 2 * radius, 2 * radius, (int)(20 * 360 / 100),
(int)(10 * 360 / 100));
g.setColor(Color.black);
g.drawString("Quizzes -- 10%",
(int)(xCenter + radius * Math.cos(2 * Math.PI * 0.25)),
(int)(yCenter - radius * Math.sin(2 * Math.PI * 0.25)));
// Draw the arc for midterm exams
g.setColor(Color.green);
g.fillArc(x, y, 2 * radius, 2 * radius, (int)(30*360/100),
(int)(30 * 360 / 100));
g.setColor(Color.black);
g.drawString("Midterms -- 30%",
(int)(xCenter + radius*Math.cos(2 * Math.PI * 0.45)) - 40,
(int)(yCenter - radius*Math.sin(2 * Math.PI * 0.45)));
// Draw the arc for the final exam
g.setColor(Color.white);
g.fillArc(x, y, 2 * radius, 2 * radius, (int)(60 * 360 / 100),
(int)(40 * 360 / 100));
g.setColor(Color.black);
g.drawString("Final -- 40%",
(int)(xCenter + radius*Math.cos(2 * Math.PI * 0.8)),
(int)(yCenter - radius*Math.sin(2 * Math.PI * 0.8)));
}
}
package chapter15_编程练习题;
import java.awt.*;
import javax.swing.*;
@SuppressWarnings("serial")
public class Exercise15_14 extends JFrame {
public Exercise15_14() {
BarChart1 chart1 = new BarChart1();
double[] data1 = {20, 10, 30, 40};
String[] dataName1 = {"Project -- 20%", "Quizzes -- 10%",
"Midtems -- 30%", "Final -- 40%"};
chart1.setData(dataName1, data1);//传进关键词
add(chart1);
}
public static void main(String[] args) {
Exercise15_14 frame = new Exercise15_14();
frame.setTitle("Exercise15_14");
frame.setSize(500, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null); // Center the frame
frame.setVisible(true);
}
}
@SuppressWarnings("serial")
class BarChart1 extends JPanel {
BorderLayout borderLayout1 = new BorderLayout();
Color[] colors = {Color.red, Color.yellow, Color.green, Color.blue,
Color.cyan, Color.magenta, Color.orange, Color.pink,
Color.darkGray};
String[] dataName;
double[] data;
public void paintComponent(Graphics g) {
super.paintComponent(g);
// Find the maximum value in the data
double max = data[0];
for (int i=1; i<data.length; i++)
max = Math.max(max, data[i]);
int barWidth = (int)((getWidth() - 10.0) / data.length - 10);//设置单个宽度
int maxBarHeight = getHeight() - 30;//设置表格中最大高度
g.drawLine(5, getHeight() - 10, getWidth() - 5, getHeight() - 10);//底线
int x = 15;
for (int i = 0; i < data.length; i++) {
g.setColor(colors[i % colors.length]);
int newHeight = (int)(maxBarHeight * data[i] / max);//更新每个列的高度
int y = getHeight() - 10 - newHeight;
g.fillRect(x, y, barWidth, newHeight);
g.setColor(Color.black);
g.drawString(dataName[i], x, y - 7);
x += barWidth + 10;//更新x轴坐标
}
}
public void setData(String[] dataName, double[] data) {//从类外传进值 使该类具有封装性 普遍性
this.dataName = dataName;
this.data = data;
}
}
package chapter15_编程练习题;
import java.awt.*;
import javax.swing.*;
@SuppressWarnings("serial")
public class Exercise15_20 extends JFrame {
public static void main(String[] args) {
// Obtain the total seconds since the midnight, Jan 1, 1970
long totalSeconds = System.currentTimeMillis() / 1000;
// Compute the current second in the minute in the hour
int currentSecond = (int)(totalSeconds % 60);
// Obtain the total minutes
long totalMinutes = totalSeconds / 60;
// Compute the current minute in the hour
int currentMinute = (int)(totalMinutes % 60);
// Obtain the total hours
long totalHours = totalMinutes / 60;
// Compute the current hour
int currentHour = (int)(totalHours % 24);
// Create a frame to hold the clock
Exercise15_20 frame = new Exercise15_20();
frame.setTitle("Exercise15_20");
DetailedClock clock = new DetailedClock(currentHour, currentMinute, currentSecond);
clock.setSecondHandVisible(false);
frame.add(clock);
MessagePanel messagePanel = new MessagePanel(
currentHour + ":" + currentMinute + ":" + currentSecond + " GMT");
messagePanel.setCentered(true);
messagePanel.setForeground(Color.blue);
frame.add(messagePanel, BorderLayout.SOUTH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 350);
frame.setLocationRelativeTo(null); // Center the frame
frame.setVisible(true);
}
}
@SuppressWarnings("serial")
class DetailedClock extends JPanel {
private int hour;
private int minute;
private int second;
protected int xCenter, yCenter;
protected int clockRadius;
public DetailedClock() {
}
// Construct a clock panel
public DetailedClock(int hour, int minute, int second) {
this.hour = hour;
this.minute = minute;
this.second = second;
}
// Draw the clock
protected void paintComponent(Graphics g) {
super.paintComponent(g);
// Initialize clock parameters
clockRadius =
(int)(Math.min(getSize().width, getSize().height) * 0.9 * 0.5);
xCenter = (getSize().width) / 2;
yCenter = (getSize().height) / 2;
// Draw circle
g.setColor(Color.black);
g.drawOval(xCenter - clockRadius, yCenter - clockRadius,
2 * clockRadius, 2 * clockRadius);
// Draw second hand
if (secondHandVisible) {
int sLength = (int)(clockRadius * 0.7);
int xSecond =
(int)(xCenter + sLength * Math.sin(second * (2 * Math.PI / 60)));
int ySecond =
(int)(yCenter - sLength * Math.cos(second * (2 * Math.PI / 60)));
g.setColor(Color.red);
g.drawLine(xCenter, yCenter, xSecond, ySecond);
}
// Draw minute hand
if (minuteHandVisible) {
int mLength = (int)(clockRadius * 0.6);
int xMinute =
(int)(xCenter + mLength * Math.sin(minute * (2 * Math.PI / 60)));
int yMinute =
(int)(yCenter - mLength * Math.cos(minute * (2 * Math.PI / 60)));
g.setColor(Color.blue);
g.drawLine(xCenter, yCenter, xMinute, yMinute);
}
// Draw hour hand
if (hourHandVisible) {
int hLength = (int)(clockRadius * 0.5);
int xHour = (int)(xCenter +
hLength *
Math.sin((hour + minute / 60.0) * (2 * Math.PI / 12)));
int yHour = (int)(yCenter -
hLength *
Math.cos((hour + minute / 60.0) * (2 * Math.PI / 12)));
g.setColor(Color.black);
g.drawLine(xCenter, yCenter, xHour, yHour);
}
// Display more details on the clock
for (int i = 0; i < 60; i++) {
double percent;
if (i % 5 == 0) {
percent = 0.9;
}
else {
percent = 0.95;
}
int xOuter = (int)(xCenter +
clockRadius * Math.sin(i * (2 * Math.PI / 60)));
int yOuter = (int)(yCenter -
clockRadius * Math.cos(i * (2 * Math.PI / 60)));
int xInner = (int)(xCenter +
percent * clockRadius * Math.sin(i * (2 * Math.PI / 60)));
int yInner = (int)(yCenter -
percent * clockRadius * Math.cos(i * (2 * Math.PI / 60)));
g.drawLine(xOuter, yOuter, xInner, yInner);
}
// Display hours on the clock
g.setColor(Color.black);
for (int i = 0; i < 12; i++) {
int x = (int)(xCenter +
0.8 * clockRadius * Math.sin(i * (2 * Math.PI / 12)));
int y = (int)(yCenter -
0.8 * clockRadius * Math.cos(i * (2 * Math.PI / 12)));
g.drawString("" + ((i == 0) ? 12 : i), x, y);
}
}
/** Return hour */
public int getHour() {
return hour;
}
/** Set a new hour */
public void setHour(int hour) {
this.hour = hour;
repaint();
}
/** Return minute */
public int getMinute() {
return minute;
}
/** Set a new minute */
public void setMinute(int minute) {
this.minute = minute;
repaint();
}
/** Return second */
public int getSecond() {
return second;
}
/** Set a new second */
public void setSecond(int second) {
this.second = second;
repaint();
}
private boolean hourHandVisible = true;
private boolean minuteHandVisible = true;
private boolean secondHandVisible = true;
public boolean isHourHandVisible() {
return hourHandVisible;
}
public boolean isMinuteHandVisible() {
return hourHandVisible;
}
public boolean isSecondHandVisible() {
return secondHandVisible;
}
public void setHourHandVisible(boolean hourHandVisible) {
this.hourHandVisible = hourHandVisible;
repaint();
}
public void setMinuteHandVisible(boolean minuteHandVisible) {
this.minuteHandVisible = minuteHandVisible;
repaint();
}
public void setSecondHandVisible(boolean secondHandVisible) {
this.secondHandVisible = secondHandVisible;
repaint();
}
}
@SuppressWarnings("serial")
class MessagePanel extends JPanel {
/** The message to be displayed */
private String message = "Welcome to Java";
/** The x coordinate where the message is displayed */
private int xCoordinate = 20;
/** The y coordinate where the message is displayed */
private int yCoordinate = 20;
/** Indicate whether the message is displayed in the center */
private boolean centered;
/** The interval for moving the message horizontally
* and vertically */
private int interval = 10;
/** Construct with default properties */
public MessagePanel() {
}
/** Construct a message panel with a specified message */
public MessagePanel(String message) {
this.message = message;
}
/** Return message */
public String getMessage() {
return message;
}
/** Set a new message */
public void setMessage(String message) {
this.message = message;
repaint();
}
/** Return xCoordinator */
public int getXCoordinate() {
return xCoordinate;
}
/** Set a new xCoordinator */
public void setXCoordinate(int x) {
this.xCoordinate = x;
repaint();
}
/** Return yCoordinator */
public int getYCoordinate() {
return yCoordinate;
}
/** Set a new yCoordinator */
public void setYCoordinate(int y) {
this.yCoordinate = y;
repaint();
}
/** Return centered */
public boolean isCentered() {
return centered;
}
/** Set a new centered */
public void setCentered(boolean centered) {
this.centered = centered;
repaint();
}
/** Return interval */
public int getInterval() {
return interval;
}
/** Set a new interval */
public void setInterval(int interval) {
this.interval = interval;
repaint();
}
/** Paint the message */
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (centered) {
// Get font metrics for the current font
FontMetrics fm = g.getFontMetrics();
// Find the center location to display
int stringWidth = fm.stringWidth(message);
int stringAscent = fm.getAscent();
// Get the position of the leftmost character in the baseline
xCoordinate = getWidth() / 2 - stringWidth / 2;
yCoordinate = getHeight() / 2 + stringAscent / 2;
}
g.drawString(message, xCoordinate, yCoordinate);
}
/** Move the message left */
public void moveLeft() {
xCoordinate -= interval;
repaint();
}
/** Move the message right */
public void moveRight() {
xCoordinate += interval;
repaint();
}
/** Move the message up */
public void moveUp() {
yCoordinate -= interval;
repaint();
}
/** Move the message down */
public void moveDown() {
yCoordinate += interval;
repaint();
}
/** Override get method for preferredSize */
public Dimension getPreferredSize() {
return new Dimension(200, 30);
}
}