《JAVA实验报告简单绘图程序》由会员分享,可在线阅读,更多相关《JAVA实验报告简单绘图程序(8页珍藏版)》请在人人文库网上搜索。
1、实验三绘制图形一、实验目的学会JButton的使用,能进行JButton的基本操作,能使用JButton构造 一个图形界面框架通过实验熟悉swing图形用户界面程序设计熟悉java中的事件处理模型,每个事件动作代表的意义进一步提高编程能力,学会独立 思考,学会上网查找资料决问题。二、实验内容设计一个程序,该程序能够绘制基本的图形(包括圆形、矩形、直线等); 要求有一个简单易操作的用户界面(包括菜单、按钮等组件)。编写的程序 要求能够实现保存和读取的功能。三、实验代码Mai n.javaimport ;import ;import ;importimportimportimport win pa。
2、i nt.Wi nToolBar;import winpain t.w in dowsPa int;public class Main /* param args*/public static void main( Stri ng args) / TODOAuto-ge nerated method stubJPa nel pa in tWin =new wi ndowsPa in t(画图);paintWin.setBounds(0, 30, 1000, 1000);paintWin. setLayout( n ull );paintWin.setVisible(true );JToolBa。
3、r tool =new Win ToolBar(wi ndowsPai nt)pai ntWin);tool.setBounds(0, 0, 1000, 30);tool.setVisible(true );JFrame frame = new JFrame();frame.setLayout( n ull );frame.setVisible( true );frame.setBounds(0, 0, 1000, 1000);frame.add(tool);frame.add(pai ntWin);test.javaimport base.GrapicsElem;import base.Re。
4、ct;public class testfractor /* param args* throws ClassNotFou ndExcepti on*/public static void main( Str in g args) throws ClassNotFou ndExcepti on / TODOAuto-ge nerated method stubGrapicsElem Ge = new Rect();Class demo1= null;Class demo2=null;Class demo3=null;try/一般尽量采用这种形式demo1=Class.forName(base.。
5、Rect);Class con= demo1.getSuperclass();demo2 = Class.forName(base.GrapicsElem);Class sun s = demo2.getl nterfaces();for (i nt i = 0; i public Lin e()track = new ArrayList();Overridepublic void draw(Graphics g) for ( int i = 0; i Lg = new ArrayList() ;GrapicsElem cur;JLabel label;public windowsPaint(。
6、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(圆。
7、形 )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(); g2d.setStroke(new BasicStroke(10.0f);for (int i = 0; i Lg.size(); i+) G。
8、rapicsElem 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) 。
9、/ 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 SaveTo。
10、File() 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); Override public void mousePressed(Mou。
11、seEvent 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.get。
12、Y(); 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 mous。
13、eExited(MouseEvent e) / TODO Auto-generated method stubWinToolBar.javapackage winpaint;public class WinToolBar extends JToolBar implements ActionListenerpublic static String Command = 矩形 ;JButton btn1,btn2,btn3;/windowsPaint winp;public WinToolBar(windowsPaint winP )btn1 = new JButton(矩形 );btn2 = ne。
14、w JButton(圆形 );btn3 = new JButton(线 );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 com = new JComboBox();com.addItem( 红色 );com.addItem( 蓝色 )。
15、;com.addItem( 黑色 );com.addItemListener(new ItemListener() Overridepublic void itemStateChanged(ItemEvent e) / TODO Auto-generated method stub);this.add(com);Overridepublic void actionPerformed(ActionEvent e) / TODO Auto-generated method stubJButton btn = (JButton) e.getSource();btn.setBackground(Color.green);Command = btn.getActionCommand();三、实验结果。