⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 myhomework.java

📁 一个简单的画图的程序比较简单的gui程序!自己编写的!欢迎大家下载学习交流!
💻 JAVA
字号:
package mycode;

import java.awt.*;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.*;

public class Myhomework extends JFrame {
	private static final Graphics g = null;

	private CheckboxGroup cg1;

	int width1 = 200, height1 = 200;

	public Myhomework() {
		super("GUI 程序设计");

		Container container = getContentPane();// 利用getcontentpane() 向面板中添加组件
		// container.setBackground(Color.green);//设置背景颜色
		setLayout(new BorderLayout());// 布局方式

		this.setSize(500, 500);

		MyPanel panel = new MyPanel();// 新建一个panel

		container.add(panel, BorderLayout.CENTER);

		this.setVisible(true);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

	}

	private class MyPanel extends JPanel implements ComponentListener,
			ItemListener {

		private JLabel lable;// 设置标签,利用显示文字

		private ImageIcon image;

		private Checkbox cb1, cb2, cb3, cb4;// 设计副选筐实现

		private int n = 1;

		public MyPanel() {
			image = new ImageIcon("mycode/image.gif");
			lable = new JLabel("GUI程序设计", image, SwingConstants.LEFT);// 创建图片标签
			lable.setSize(1800, 1800);

			cg1 = new CheckboxGroup();
			cb1 = new Checkbox("半圆", cg1, true);
			cb2 = new Checkbox("正六变形", cg1, false);
			cb3 = new Checkbox("坐标函数", cg1, false);
			cb4 = new Checkbox("极坐标函数", cg1, false);// 创建选项框
			addComponentListener((ComponentListener) this);
			cb1.addItemListener(this);
			cb2.addItemListener(this);
			cb3.addItemListener(this);
			cb4.addItemListener(this);// 注册监听器

			add(cb1);
			add(cb2);
			add(cb3);
			add(cb4);
			add(lable);
			// 向panel中添加组件
		}

		// 绘制图象的函数
		public void paintComponent(Graphics g) {
			super.paintComponent(g);

			// System.out.println(n);
			switch (n) {
			case 1:
				g.setColor(Color.RED);
				Paint_by(g);
				break;
			case 2:
				g.setColor(Color.BLUE);
				Paint_6(g);
				break;
			case 3:
				g.setColor(Color.BLACK);
				paint_herat(g);
				break;
			case 4:
				g.setColor(Color.red);
				paint_luanxuan(g);
			default:
				break;
			}

		}

		// 画半圆
		public void Paint_by(Graphics g) {

			int x0, y0; // 原点坐标
			x0 = width1 / 2;
			y0 = height1 / 2;
			g.drawLine(x0, 0, x0, height1);
			g.drawLine(0, y0, width1, y0);
			int radius = 500;// 半径

			int arcAngle = 180;// 弧的角度
			// 画线
			g.drawLine(x0 - 250, y0 - 250, x0 + 250, y0 - 250);// 设置圆的位置根据坐标进行设置
			// 画弧
			g.drawArc(x0 - 250, y0 - 500, radius, radius, arcAngle, arcAngle);

		}

		// 画正六边形
		public void Paint_6(Graphics g) {
			int x0, y0; // 原点坐标
			x0 = width1 / 2;
			y0 = height1 / 2;
			g.drawLine(x0, 0, x0, height1);
			g.drawLine(0, y0, width1, y0);// 直角坐标系的设置
			int radius = 500;// 半径
			Polygon p = new Polygon();

			radius /= 2;

			for (int i = 0; i < 6; i++)
				// 画正6边形
				p.addPoint((int) (x0 + radius * Math.cos(i * 2 * Math.PI / 6)),
						(int) (y0 + radius * Math.sin(i * 2 * Math.PI / 6)));

			g.drawPolygon(p);
		}

		// 画心型线
		public void paint_herat(Graphics g) {

			int x0, y0; // 原点坐标
			x0 = width1 / 2;
			y0 = height1 / 2;
			g.drawLine(x0, 0, x0, height1);
			g.drawLine(0, y0, width1, y0);
			int i, j = 40, x, y;
			double pi = 3.14, angle, r;

			while (j < 200) {
				for (i = 0; i < 1023; i++) {
					angle = i * pi / 512;
					r = j * (1 - Math.cos(angle));
					x = (int) Math.round(r * Math.cos(angle) * 2);
					y = (int) Math.round(r * Math.sin(angle));
					g.fillOval(x0 + x, y0 + y, 1, 1);

				}
				j = j + 20;
			}
		}

		// 画螺旋线
		public void paint_luanxuan(Graphics g) {

			int x0, y0; // 原点坐标
			x0 = width1 / 2;
			y0 = height1 / 2;
			// g.drawLine(x0, 0, x0, height1);
			g.drawLine(0, y0, width1, y0);
			int i, j = 40, x, y;
			double pi = 3.14, angle, r;

			while (j < 200) {
				for (i = 0; i < 1000; i++) {
					angle = i * pi / 128;
					r = j * (1 - Math.cos(angle));
					x = (int) (r * (Math.cos(angle) + angle * Math.sin(angle)));
					y = (int) (r * (Math.sin(angle) - angle * Math.cos(angle)));
					g.fillOval(x0 + x, y0 + y, 1, 2);

				}
				j = j + 20;
			}
		}

		// 单选框的监听函数
		public void itemStateChanged(ItemEvent e) { // 选中单选按钮时

			if (e.getSource() == cb1) // 判断产生事件对象e的组件是谁
			{
				n = 1;
				// System.out.println("你按下的是cb1");
				repaint();
			}

			if (e.getSource() == cb2) // 判断单选按钮cb2的状态
			{
				n = 2;
				// System.out.println("你按下的是cb2");
				repaint();
			}
			if (e.getSource() == cb3) // 判断复选框组cg3选中的是谁
			{
				n = 3;
				// System.out.println("你按下的是cb3");
				repaint();
			}
			if (e.getSource() == cb4) {
				n = 4;
				repaint();
			}

		}

		public void componentResized(ComponentEvent e) // 改变窗口大小时
		{
			width1 = getWidth(); // 取得窗口的大小
			height1 = getHeight();
			repaint();
		}

		public void componentMoved(ComponentEvent arg0) {
			// TODO 自动生成方法存根

		}

		public void componentShown(ComponentEvent arg0) {
			// TODO 自动生成方法存根

		}

		public void componentHidden(ComponentEvent arg0) {
			// TODO 自动生成方法存根

		}

	}

	// 主函数
	public static void main(String[] args) {
		// TODO 自动生成方法存根
		Myhomework jj = new Myhomework();

	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -