Java swing繪製柱狀圖和餅圖

**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);
		}
	}
}
發佈了56 篇原創文章 · 獲贊 4 · 訪問量 13萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章