【JAVA语言程序设计基础篇】--图形-- 绘制封装表格类的思考


开始用的方法没有体现类的封装性 没有类的普遍性


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;
  }
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值