**15.14编写程序,使用条形图显示作业、平时测验、其中考试和期末考试占总成绩的百分比。假设作业占20%用红色显示,平时测验占10%用蓝色显示,其中考试占30%用绿色显示,期末考试占40%用橙色显示。
public class Job15_14 extends JFrame {
public Job15_14() throws HeadlessException {
Job1514 job1514 = new Job1514();
job1514.setBackground(Color.WHITE);
add(job1514);
}
public static void main(String[] args) {
Job15_14 frame = new Job15_14();
frame.setTitle("课后作业15.14");
frame.setSize(500, 200);// job15
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
class Job1514 extends JPanel {
int HEIGHT = 250;
int offsetY = 14;
Font font = new Font("Times", Font.PLAIN, 14);
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
HEIGHT = (int) (getHeight());
int width = getWidth();
int w80 = (int) (width * 0.8);
int w20 = (int) (width * 0.2);
int uw = w80 / 4;
int gap = w20 / 5;
int bottom = getHeight() - 20;
int rectH = (int) (HEIGHT * 0.2);
String str;
g.setFont(font);
g.setColor(Color.RED);
g.fillRect(gap, bottom - rectH, uw, rectH);
g.setColor(Color.BLACK);
str = "Project -- 20%";
g.drawString(str, gap, bottom - rectH - offsetY);
rectH = (int) (HEIGHT * 0.1);
g.setColor(Color.BLUE);
g.fillRect(gap * 2 + uw, bottom - rectH, uw, rectH);
g.setColor(Color.BLACK);
str = "Quizzes -- 10%";
g.drawString(str, gap * 2 + uw, bottom - rectH - offsetY);
rectH = (int) (HEIGHT * 0.3);
g.setColor(Color.GREEN);
g.fillRect(gap * 3 + uw * 2, bottom - rectH, uw, rectH);
g.setColor(Color.BLACK);
str = "Midtems -- 30%";
g.drawString(str, gap * 3 + uw * 2, bottom - rectH - offsetY);
rectH = (int) (HEIGHT * 0.4);
g.setColor(Color.ORANGE);
g.fillRect(gap * 4 + uw * 3, bottom - rectH, uw, rectH);
g.setColor(Color.BLACK);
str = "Final -- 40%";
g.drawString(str, gap * 4 + uw * 3, bottom - rectH - offsetY);
int x1, x2, y1, y2;
x1 = gap / 2;
y1 = bottom;
x2 = width - gap / 2;
y2 = y1;
g.setColor(Color.BLACK);
g.drawLine(x1, y1, x2, y2);
}
}
}
public class Job15_15 extends JFrame {
public Job15_15() throws HeadlessException {
Job1515 job1515 = new Job1515();
job1515.setBackground(Color.WHITE);
add(job1515);
}
public static void main(String[] args) {
Job15_15 frame = new Job15_15();
frame.setTitle("课后作业15.15");
frame.setSize(400, 250);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
class Job1515 extends JPanel {
Font font = new Font("Times", Font.PLAIN, 16);
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int radius = (int) (Math.min(getWidth(), getHeight() * 0.4));
String str;
g.setColor(Color.RED);
g.fillArc(getWidth() / 2 - radius, getHeight() / 2 - radius, radius * 2, radius * 2, 0, (int) (360 * 0.2));
g.setColor(Color.BLUE);
g.fillArc(getWidth() / 2 - radius, getHeight() / 2 - radius, radius * 2, radius * 2, (int) (360 * 0.2),
(int) (360 * 0.1));
g.setColor(Color.GREEN);
g.fillArc(getWidth() / 2 - radius, getHeight() / 2 - radius, radius * 2, radius * 2, (int) (360 * 0.3),
(int) (360 * 0.3));
g.setColor(Color.ORANGE);
g.fillArc(getWidth() / 2 - radius, getHeight() / 2 - radius, radius * 2, radius * 2, (int) (360 * 0.6),
(int) (360 * 0.4));
g.setFont(font);
g.setColor(Color.BLACK);
str = "Project -- 20%";
g.drawString(str, getWidth() / 2 + radius, getHeight() / 2 - 20);
g.setColor(Color.BLACK);
str = "Quizzes -- 10%";
g.drawString(str, getWidth() / 2, getHeight() / 2 - radius);
g.setColor(Color.BLACK);
str = "Midtems -- 30%";
g.drawString(str, getWidth() / 2 - radius - 20 * 2, getHeight() / 2);
g.setColor(Color.BLACK);
str = "Final -- 40%";
g.drawString(str, getWidth() / 2 + 20, getHeight() / 2 + radius);
}
}
}