JAVA实验报告简单绘图程序文件.doc
实验三 绘制图形一、实验目的 学会utton的使用,能进展utton的根本操作,能使用utton构造一个图形界面框架通过实验熟悉swing图形用户界面程序设计熟悉java中的事件处理模型,每个事件动作代表的意义进一步提高编程能力,学会独立思考,学会上网查找资料决问题。二、实验容 设计一个程序,该程序能够绘制根本的图形包括圆形、矩形、直线等;要求有一个简单易操作的用户界面包括菜单、按钮等组件。编写的程序要求能够实现保存和读取的功能。三、实验代码Main.javaimportjava.awt.BorderLayout;importjava.awt.event.ActionListener;importjavax.swing.utton;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.JToolBar;import winpaint.WinToolBar;importwinpaint.windowsPaint;publicclass Main /* * param args */publicstaticvoid main(String args) / TODO Auto-generated method stubJPanel paintWin = newwindowsPaint("画图");paintWin.setBounds(0, 30, 1000, 1000);paintWin.setLayout(null);paintWin.setVisible(true);JToolBar tool = new WinToolBar(windowsPaint)paintWin);tool.setBounds(0, 0, 1000, 30);tool.setVisible(true);JFrame frame = new JFrame();frame.setLayout(null);frame.setVisible(true);frame.setBounds(0, 0, 1000, 1000);frame.add(tool);frame.add(paintWin);test.javaimport java.awt.GradientPaint;import java.lang.reflect.Constructor;import base.GrapicsElem;import base.Rect;public class testfractor /* * param args * throws ClassNotFoundException */public static void main(String args) throws ClassNotFoundException / TODO Auto-generated method stubGrapicsElem Ge = new Rect();System.out.println(Ge.getClass().getName(); Class<?> demo1=null; Class<?> demo2=null; Class<?> demo3=null; try /一般尽量采用这种形式 demo1=Class.forName("base.Rect"); Class<?> con= demo1.getSuperclass(); System.out.println(con.getName(); demo2 = Class.forName("base.GrapicsElem"); Class<?> suns = demo2.getInterfaces(); for (int i = 0; i < suns.length; i+) System.out.println(sunsi.getName(); System.out.println(con.length); / GrapicsElem gg = (GrapicsElem)con0.newInstance(); System.out.println(con0); System.out.println(con1); System.out.println(con2); catch(Exception e) e.printStackTrace(); demo2=new Rect().getClass(); demo3=Rect.class; System.out.println("类名称 "+demo1.getName(); System.out.println("类名称 "+demo2.getName(); System.out.println("类名称 "+demo3.getName();GrapicsElem.javapackage base;import java.awt.Color;import java.awt.Graphics;import java.awt.Point;publicabstractclass GrapicsElem Point start;Point end;private Color color;public Color getColor() returncolor;publicvoid setColor(Color color) this.color = color;publicvoid setStart(Point start) this.start = start;publicvoid setEnd(Point end) this.end = end;publicabstractvoid doMousePress(Point p);publicabstractvoid doMouseDrag(Point p);publicabstractvoid doMouseRelease(Point p);publicabstractvoid draw(Graphics g ) ;Line.javapackage base;import java.awt.Graphics;import java.awt.Point;import java.util.ArrayList;publicclass Line extends GrapicsElem ArrayList<Point> track;public Line()track = new ArrayList<Point>();Overridepublicvoid draw(Graphics g) for (int i = 0; i < track.size()-1; i+) Point cur = track.get(i);Point next = track.get(i+1);g.drawLine(cur.x, cur.y, next.x, next.y);privatevoid AddPoint(Point p)track.add(p);Overridepublicvoid doMousePress(Point p) / TODO Auto-generated method stubAddPoint(p);Overridepublicvoid doMouseDrag(Point p) / TODO Auto-generated method stubAddPoint(p);Overridepublicvoid doMouseRelease(Point p) / TODO Auto-generated method stubAddPoint(p);Rect.javapackage base;import java.awt.Graphics;import java.awt.Point;publicclass Rect extends GrapicsElem intx,y,width,height;Overridepublicvoid draw(Graphics g) / TODO Auto-generated method stubg.drawRect(getX(), getY(), getWidth(), getHeight();Overridepublicvoid doMousePress(Point p) super.setStart(p);super.setEnd(p);Overridepublicvoid doMouseDrag(Point p) super.setEnd(p);Overridepublicvoid doMouseRelease(Point p) super.setEnd(p);publicint getX() return Math.min(start.x, end.x);publicint getY() return Math.min(start.y, end.y);publicint getWidth() return Math.abs(start.x-end.x);publicint getHeight() return Math.abs(start.y-end.y);Circle.javapackage base;import java.awt.Graphics;publicclass Circle extends Rect Overridepublicvoid draw(Graphics g) / TODO Auto-generated method stubg.drawOval(getX(), getY(), getWidth(), getHeight();windowsPaint.javapackage winpaint;import java.awt.BasicStroke;import java.awt.Button;import java.awt.Color;import java.awt.Font;import java.awt.Graphics;import java.awt.Graphics2D;import java.awt.Image;import java.awt.Point;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import java.awt.event.MouseMotionListener;import java.io.File;import java.io.IOException;import java.nio.Buffer;import java.util.ArrayList;import java.util.BitSet;import java.util.List;import java.awt.geom.*;import java.awt.image.BufferedImage;import javax.imageio.ImageIO;import javax.swing.ImageIcon;import javax.swing.utton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.JToolBar;import base.Circle;import base.GrapicsElem;import base.Line;import base.Rect;import java.awt.BorderLayout;public class windowsPaint extends JPanelimplements MouseListener, MouseMotionListenerString command = "矩形"List<GrapicsElem> Lg = new ArrayList() ;GrapicsElem cur;JLabel label;public windowsPaint(String title)init();private void init() addMouseListener(this);addMouseMotionListener(this);/* * 可以使用反射去掉判断语句、 * Class<?> g = Class.forName("base.Rect"); * g.newInstance(); * return */private GrapicsElem newInstance()command = WinToolBar.Command;if(command.equals("矩形")return new Rect();else if(command.equals("圆形")return new Circle();else if(command.equals("线")return new Line();return null;Overridepublic void paint(Graphics g) / TODO Auto-generated method stub super.paint(g); Graphics2D g2d = (Graphics2D) g; /g2d.setColor(cur.getColor(); /System.out.println(cur.getColor(); g2d.setStroke(new BasicStroke(10.0f); for (int i = 0; i < Lg.size(); i+) GrapicsElem ge = Lg.get(i); ge.draw(g);/Override/public void repaint() / TODO Auto-generated method stub/ TODO Auto-generated method stub/super.repaint();/ Thread thread = new Thread(new Runnable() /Override/public void run() / TODO Auto-generated method stub/try /SaveToFile();/ catch (IOException e) / TODO Auto-generated catch block/e.printStackTrace();/);/thread.start();/ImageIcon bg = new ImageIcon("tmp.jpg");/if(bg!=null)/if(label!=null)/this.remove(label);/ label = new JLabel(bg); / label.setBounds(0,0,bg.getIconWidth(),bg.getIconHeight();/ this.add(label);/ super.repaint();/ /private void SaveToFile() throws IOException / TODO Auto-generated method stubBufferedImage bimg = new BufferedImage(this.getWidth(), this.getHeight(), BufferedImage.TYPE_3BYTE_BGR);Graphics2D g2d = bimg.createGraphics();this.paint(g2d);ImageIO.write(bimg, "jpg", new File("tmp.jpg");Overridepublic void mousePressed(MouseEvent e) Point p = new Point(e.getX(),e.getY();cur = newInstance();cur.doMousePress(p);Lg.add(cur);Overridepublic void mouseDragged(MouseEvent e) Point p = new Point(e.getX(),e.getY();cur.doMouseDrag(p);repaint();Overridepublic void mouseReleased(MouseEvent e) Point p = new Point(e.getX(),e.getY();cur.doMouseRelease(p);repaint();Overridepublic void mouseMoved(MouseEvent e) / TODO Auto-generated method stubOverridepublic void mouseClicked(MouseEvent e) / TODO Auto-generated method stubOverridepublic void mouseEntered(MouseEvent e) / TODO Auto-generated method stubOverridepublic void mouseExited(MouseEvent e) / TODO Auto-generated method stubWinToolBar.javapackage winpaint;import java.awt.Color;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.ItemEvent;import java.awt.event.ItemListener;import javax.swing.utton;import javax.swing.JComboBox;import javax.swing.JSeparator;import javax.swing.JToolBar;public class WinToolBar extends JToolBar implements ActionListenerpublic static String Command = "矩形"utton btn1,btn2,btn3;/windowsPaint winp;public WinToolBar(windowsPaint winP )btn1 = new utton("矩形");btn2 = new utton("圆形");btn3 = new utton("线");this.add(btn1);this.add(btn2);this.add(btn3);btn1.addActionListener(ActionListener)this);btn2.addActionListener(ActionListener)this);btn3.addActionListener(ActionListener)this);/this.winp =winP;JComboBox = new JComboBox();.addItem("红色");.addItem("蓝色");.addItem("黑色");.addItemListener(new ItemListener() Overridepublic void itemStateChanged(ItemEvent e) / TODO Auto-generated method stubSystem.out.println(e.getItem();System.out.println(e.getID();/winp.cur.setColor(Color.blue););this.add();Overridepublic void actionPerformed(ActionEvent e) / TODO Auto-generated method stubthis.btn1.setBackground(Color.getColor("Control");this.btn2.setBackground(Color.getColor("Control");this.btn3.setBackground(Color.getColor("Control");utton btn = (utton) e.getSource();btn.setBackground(Color.green);Command = btn.getActionCommand();System.out.println(Command);3、 实验结果11 / 11