收藏 分销(赏)

2023年JAVA实验报告简单绘图程序.doc

上传人:快乐****生活 文档编号:3181018 上传时间:2024-06-24 格式:DOC 页数:22 大小:72.04KB
下载 相关 举报
2023年JAVA实验报告简单绘图程序.doc_第1页
第1页 / 共22页
2023年JAVA实验报告简单绘图程序.doc_第2页
第2页 / 共22页
2023年JAVA实验报告简单绘图程序.doc_第3页
第3页 / 共22页
2023年JAVA实验报告简单绘图程序.doc_第4页
第4页 / 共22页
2023年JAVA实验报告简单绘图程序.doc_第5页
第5页 / 共22页
点击查看更多>>
资源描述

1、试验三 绘制图形一、试验目旳 学会JButton旳使用,能进行JButton旳基本操作,能使用JButton构造一种图形界面框架通过试验熟悉swing图形顾客界面程序设计熟悉java中旳事件处理模型,每个事件动作代表旳意义深入提高编程能力,学会独立思索,学会上网查找资料决问题。二、试验内容 设计一种程序,该程序可以绘制基本旳图形(包括圆形、矩形、直线等);规定有一种简朴易操作旳顾客界面(包括菜单、按钮等组件)。编写旳程序规定可以实现保留和读取旳功能。三、试验代码Main.javaimport java.awt.BorderLayout;import java.awt.event.ActionL

2、istener;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.JToolBar;import winpaint.WinToolBar;import winpaint.windowsPaint;public class Main /* * param args */public static void main(String args) / TODO Auto-generated method stubJPanel paintWin = new w

3、indowsPaint(画图);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

4、(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 ClassNotF

5、oundException / 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 = Cla

6、ss.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); c

7、atch(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.

8、Point;public abstract class GrapicsElem Point start;Point end;private Color color;public Color getColor() return color;public void setColor(Color color) this.color = color;public void setStart(Point start) this.start = start;public void setEnd(Point end) this.end = end;public abstract void doMousePr

9、ess(Point p);public abstract void doMouseDrag(Point p);public abstract void doMouseRelease(Point p);public abstract void draw(Graphics g ) ;Line.javapackage base;import java.awt.Graphics;import java.awt.Point;import java.util.ArrayList;public class Line extends GrapicsElem ArrayList track;public Lin

10、e()track = new ArrayList();Overridepublic void 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);private void AddPoint(Point p)track.add(p);Overridepublic void doMousePress(Point p) / TODO Auto-generat

11、ed method stubAddPoint(p);Overridepublic void doMouseDrag(Point p) / TODO Auto-generated method stubAddPoint(p);Overridepublic void doMouseRelease(Point p) / TODO Auto-generated method stubAddPoint(p);Rect.javapackage base;import java.awt.Graphics;import java.awt.Point;public class Rect extends Grap

12、icsElem int x,y,width,height;Overridepublic void draw(Graphics g) / TODO Auto-generated method stubg.drawRect(getX(), getY(), getWidth(), getHeight();Overridepublic void doMousePress(Point p) super.setStart(p);super.setEnd(p);Overridepublic void doMouseDrag(Point p) super.setEnd(p);Overridepublic vo

13、id doMouseRelease(Point p) super.setEnd(p);public int getX() return Math.min(start.x, end.x);public int getY() return Math.min(start.y, end.y);public int getWidth() return Math.abs(start.x-end.x);public int getHeight() return Math.abs(start.y-end.y);Circle.javapackage base;import java.awt.Graphics;p

14、ublic class Circle extends Rect Overridepublic void 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.Gra

15、phics;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.IOExce

16、ption;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.JButton;import javax.swing.JFrame;import javax.swing.JLabel;im

17、port 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 Lg = new ArrayList() ;GrapicsEle

18、m 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(矩形

19、)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.getCo

20、lor(); 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 ru

21、n() / 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.getIconW

22、idth(),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

23、);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();Over

24、ridepublic 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 mouseEnt

25、ered(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.

26、awt.event.ItemListener;import javax.swing.JButton;import javax.swing.JComboBox;import javax.swing.JSeparator;import javax.swing.JToolBar;public class WinToolBar extends JToolBar implements ActionListenerpublic static String Command = 矩形;JButton btn1,btn2,btn3;/windowsPaint winp;public WinToolBar(win

27、dowsPaint winP )btn1 = new JButton(矩形);btn2 = new 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 JC

28、omboBox();com.addItem(红色);com.addItem(蓝色);com.addItem(黑色);com.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(com);Overridepubli

29、c 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);JButton btn = (JButton) e.getSource();btn.setBackground(Color.green);Command = btn.getActionCommand();System.out.println(Command);三、 试验成果

展开阅读全文
部分上传会员的收益排行 01、路***(¥15400+),02、曲****(¥15300+),
03、wei****016(¥13200+),04、大***流(¥12600+),
05、Fis****915(¥4200+),06、h****i(¥4100+),
07、Q**(¥3400+),08、自******点(¥2400+),
09、h*****x(¥1400+),10、c****e(¥1100+),
11、be*****ha(¥800+),12、13********8(¥800+)。
相似文档                                   自信AI助手自信AI助手
搜索标签

当前位置:首页 > 教育专区 > 实验设计

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        获赠5币

©2010-2025 宁波自信网络信息技术有限公司  版权所有

客服电话:4008-655-100  投诉/维权电话:4009-655-100

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :gzh.png    weibo.png    LOFTER.png 

客服