开始用的方法没有体现类的封装性 没有类的普遍性
package chapter15; import java.awt.*; import javax.swing.*; @SuppressWarnings("serial") public class exercise15_14 extends JFrame { public exercise15_14() { add(new barchart()); } 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 barchart extends JPanel{ public barchart() { } String[] str = {"Project -- 20%", "Quizzes -- 10%","Midtems -- 30%", "Final -- 40%"}; Color[] color = {Color.red,Color.yellow, Color.green, Color.blue}; int[] number = {20,10,30,40}; protected void paintComponent(Graphics g) { super.paintComponent(g); int max=0; for(int i=0;i<number.length;i++){ max = Math.max(max, number[i]); } g.drawLine(5, getHeight()-10, getWidth()-10, getHeight()-10); int x = 15; int y = getHeight()-10; int singleWidth = (int)((getWidth()-10-5-20)/4); int maxHeight = getHeight()-50; for(int i=0;i<4;i++){ g.setColor(color[i]); int newHeight = (int)(maxHeight *number[i]/max); g.fillRect(x, y-newHeight, singleWidth, newHeight); g.setColor(Color.black); g.drawString(str[i],x, y-newHeight-10); x+=singleWidth+5; } } }
对其进行封装改编 使该表格类(barChart)具备普遍性
体现其封装性
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; } }
时间: 2024-10-16 00:30:10