Java实验八(下):图形绘制——小型画板

实验目的

熟悉基本的图形绘制操作

实验内容

实现一个小型的图形绘制画板,包括直线、矩形、椭圆、多边形等基本图形

实验要求

1、实现一个小型画板
2、有菜单和工具栏选项,在菜单或工具栏子项中选择对应的图形,则在主窗体画板上自动绘制对应的图形
3、在工具栏和菜单中,还可以对画笔的颜色进行设置
4、在绘制图形时,可以实现为图形设置默认的位置坐标参数,也可通过填写对应的坐标值,然后再进行绘制
5、进阶:图形的旋转、缩放等(选做)

实验代码

package experiment_eight;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Line2D;
import java.awt.geom.*;

import javax.swing.*;

class WinDow extends JFrame implements ActionListener{
	JMenuBar bar;
	JMenu menu1;
	JMenu menu2;
	JMenuItem item1;
	JMenuItem item2;
	JMenuItem item3;
	JMenuItem item4;
	JMenuItem item5;
	JMenuItem item6;
	public WinDow() {
		bar = new JMenuBar();
		this.setJMenuBar(bar);
		//this.setLayout(new FlowLayout());
		this.setVisible(true);
		this.setBounds(500,200,400,400);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		menu1 = new JMenu("菜单");
		menu2 = new JMenu("工具栏");
		bar.add(menu1);
		bar.add(menu2);
		item1 = new JMenuItem("直线");
		item2 = new JMenuItem("矩形");
		item3 = new JMenuItem("椭圆");
		item4 = new JMenuItem("多边形");
		
		item5 = new JMenuItem("画笔颜色");
		menu1.add(item1);
		menu1.add(item2);
		menu1.add(item3);
		menu1.add(item4);
		menu2.add(item5);
		item1.addActionListener(this);
		item2.addActionListener(this);
		item3.addActionListener(this);
		item4.addActionListener(this);
		item5.addActionListener(this);
		
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if(e.getSource()==item1) {
			System.out.println("直线");
			add(new MyCanvas(1));
			setVisible(true);
			
		}
			
		if(e.getSource()==item2) {
			System.out.println("矩形");
			add(new MyCanvas(2));
			this.setVisible(true);
		}
			
		if(e.getSource()==item3) {
			System.out.println("椭圆");
			add(new MyCanvas(3));
			this.setVisible(true);
			
		}
			
		if(e.getSource()==item4) {
			System.out.println("多边形");
			add(new MyCanvas(4));
			setVisible(true);
		}
		if(e.getSource()==item5) {
			System.out.println("画笔颜色");
			ColorSet test = new ColorSet();
		}
	}
	
}
class ColorSet extends JFrame{
	JButton button[] = new JButton[6];
	String[] color = new String[]{"红色","绿色","黑色","蓝色","灰色","黄色"};
	public ColorSet() {
		this.setSize(200,200);
		this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		this.setVisible(true);
		this.setLayout(new FlowLayout());
		for(int i = 0;i<color.length;i++) {
			button[i] = new JButton(color[i]);
			add(button[i]);
		}
		for(int i = 0;i<button.length;i++) {
			int j = i;
			button[i].addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					// TODO Auto-generated method stub
					MyCanvas.color = button[j].getText();
					System.out.println(button[j].getText());
					JOptionPane.showMessageDialog(null, "切换"+button[j].getText()+"成功");
					setVisible(false);
				}
			});
		}
		
	}
}
class MyCanvas extends JPanel{
	int type;
	static String color = "black";
	public MyCanvas(int type) {
		this.type = type;
	}
	public void paint(Graphics g) {
		
		Graphics2D g_2d = (Graphics2D)g;
		if(color.equals("红色")) 
			g_2d.setColor(Color.red);
		if(color.equals("绿色")) 
			g_2d.setColor(Color.green);
		if(color.equals("黑色"))
			g_2d.setColor(Color.black);
		if(color.equals("蓝色"))
			g_2d.setColor(Color.blue);
		if(color.equals("灰色"))
			g_2d.setColor(Color.gray);
		if(color.equals("黄色"))
			g_2d.setColor(Color.yellow);
		
		if(type==1) {
			g_2d.draw(new Line2D.Double(30, 30, 80, 100));
		}
		if(type==2) {
			g_2d.draw(new Rectangle2D.Double(50, 50, 150, 100));
		}
		if(type==3) {
			g_2d.draw(new Ellipse2D.Double(60, 60, 150, 120));
		}
		if(type==4) {
			Polygon polygon = new Polygon();
			polygon.addPoint(150, 10);
			polygon.addPoint(100, 90);
			polygon.addPoint(210, 90);
			polygon.addPoint(260, 10);
			g_2d.draw(polygon);
		}
		
		
	}
	
}
public class project8 {
	public static void main(String[] args) {
		WinDow win = new WinDow();
		
	}
}
1.腾龙梦屋文章内容无特殊注明皆为源儿原创,转载请注明来源,谢谢!
2.若有相关文章侵犯您的权益,请联系源儿删除,谢谢!
3.相关软件、资料仅供学习参考使用,在24h内务必删除!
腾龙梦屋 » Java实验八(下):图形绘制——小型画板
加速支持