欢迎来到三一办公! | 帮助中心 三一办公31ppt.com(应用文档模板下载平台)
三一办公
全部分类
  • 办公文档>
  • PPT模板>
  • 建筑/施工/环境>
  • 毕业设计>
  • 工程图纸>
  • 教育教学>
  • 素材源码>
  • 生活休闲>
  • 临时分类>
  • ImageVerifierCode 换一换
    首页 三一办公 > 资源分类 > DOC文档下载  

    JAVA实验报告简单绘图程序文件.doc

    • 资源ID:1123571       资源大小:128.50KB        全文页数:11页
    • 资源格式: DOC        下载积分:10金币
    快捷下载 游客一键下载
    会员登录下载
    三方登录下载: 微信开放平台登录 QQ登录  
    下载资源需要10金币
    邮箱/手机:
    温馨提示:
    用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)
    支付方式: 支付宝    微信支付   
    验证码:   换一换

    加入VIP免费专享
     
    账号:
    密码:
    验证码:   换一换
      忘记密码?
        
    友情提示
    2、PDF文件下载后,可能会被浏览器默认打开,此种情况可以点击浏览器菜单,保存网页到桌面,就可以正常下载了。
    3、本站不支持迅雷下载,请使用电脑自带的IE浏览器,或者360浏览器、谷歌浏览器下载即可。
    4、本站资源下载后的文档和图纸-无水印,预览文档经过压缩,下载后原文更清晰。
    5、试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。

    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

    注意事项

    本文(JAVA实验报告简单绘图程序文件.doc)为本站会员(李司机)主动上传,三一办公仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知三一办公(点击联系客服),我们立即给予删除!

    温馨提示:如果因为网速或其他原因下载失败请重新下载,重复下载不扣分。




    备案号:宁ICP备20000045号-2

    经营许可证:宁B2-20210002

    宁公网安备 64010402000987号

    三一办公
    收起
    展开