📄 drc.java
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Drc extends JPanel
{
JButton b = new JButton("y");
JTextField t = new JTextField(5);
JPanel p1 = new JPanel();
int h1 = 15;
int[] numbers = {19, 13, 21, 12, 17, 8};
protected void paintComponent(Graphics g) {
super.paintComponent(g);
setLayout(new FlowLayout());
g.drawString("考试成绩统计图",170,50);
int width = getWidth();
int height = getHeight();
int x1 = (width - 75) / 12;
int x2 = (width - 75) / 10;
int w = width - 75;
int h = height - 70;
g.setColor(Color.white);
g.fillRect(0, 0, width, height);
//画X轴
g.setColor(Color.black);
g.drawLine(25,height - 20,width - 50,height - 20);
//画Y轴
g.setColor(Color.black);
g.drawLine(25,50,25,height - 20);
//定义直方图的高度
int h2=12,h3=23,h4=13,h5=16;
g.setColor(Color.black);
g.drawString("人数",8,40);
//定义Y轴上的人数标度
g.setColor(Color.black);
g.drawString("0",17, height - 20);
//定义Y轴上的人数标度
g.setColor(Color.black);
g.drawString("5",17, height -(height - 70) /5 - 20);
//定义Y轴上的人数标度
g.setColor(Color.black);
g.drawString("10",10,height -(height - 70) /5 * 2 - 20);
//定义Y轴上的人数标度
g.setColor(Color.black);
g.drawString("15",10, height -(height - 70) /5 * 3 - 20);
//定义Y轴上的人数标度
g.setColor(Color.black);
g.drawString("20",10, height -(height - 70) /5 * 4 - 20);
//定义Y轴上的人数标度
g.setColor(Color.black);
g.drawString("25",10, 55);
//显示<60的直方图
;
g.setColor(Color.yellow);
g.fillRect(25 + x1,50 + (int)(h * (25 -numbers[1]) / 25),x2,h * numbers[1] / 25);
g.setColor(Color.BLACK);
g.drawString("<60分",25 + x1, height - 10);
//显示60-69的直方图
g.setColor(Color.green);
g.fillRect(24 + x1 * 2 + x2,50 + (int)(h * (25 -numbers[2]) / 25),x2 +1 ,h * numbers[2] / 25);
g.setColor(Color.BLACK);
g.drawString("60-69分",25 + x1 * 2+ x2, height - 10);
//显示70-79的直方图
g.setColor(Color.blue);
g.fillRect(24 + x1 * 3 + x2 * 2,50 + (int)(h * (25 - numbers[3]) / 25),x2 +1 ,h * numbers[3] / 25);
g.setColor(Color.BLACK);
g.drawString("70-79分",25 + x1 * 3 + x2 * 2, height - 10);
//显示80-89的直方图
g.setColor(Color.cyan);
g.fillRect(24 + x1 * 4 + x2 * 3,50 + (int)(h * (25 - numbers[4]) / 25),x2 +1 ,h * numbers[4] / 25);
g.setColor(Color.BLACK);
g.drawString("80-89分",25 + x1 * 4 + x2 * 3, height - 10);
//显示>=90的直方图
g.setColor(Color.pink);
g.fillRect(24 + x1 * 5 + x2 * 4,50 + (int)(h * (25 - numbers[5]) / 25),x2 +1 ,h * numbers[5] / 25);
g.setColor(Color.BLACK);
g.drawString(">=90分",25 + x1 * 5 + x2 * 4, height - 10);
}
public void setNumbers(int[] numbers) {
this.numbers = numbers;
repaint();
}
/* public static void main(String args[])
{
JFrame frame=new JFrame();
//frame.setLayeredPane(new BorderLayouy());
//frame.getContentPane().add(p1, BorderLayout.NORTH);
frame.getContentPane().add(new Drawrec());
frame.setTitle("成绩考试统计图");
frame.setSize(400,270);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}**/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -