收藏 分销(赏)

简化画板资料.doc

上传人:仙人****88 文档编号:9280741 上传时间:2025-03-19 格式:DOC 页数:3 大小:18.50KB 下载积分:10 金币
下载 相关 举报
简化画板资料.doc_第1页
第1页 / 共3页
简化画板资料.doc_第2页
第2页 / 共3页


点击查看更多>>
资源描述
import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.io.*; public class huahua extends JFrame { DrawPanel drawingArea; int index = 0; drewings[] itemList = new drewings[5000]; Container c = getContentPane(); public huahua() { super("简单画板"); drawingArea = new DrawPanel(); c.add(drawingArea, BorderLayout.CENTER); itemList[index] = new Penci(); setSize(500 , 500) ; setVisible(true); show(); } public static void main(String[] Strgs) { new huahua(); } class mousea extends MouseAdapter { public void mousePressed(MouseEvent e) { itemList[index].x1 = itemList[index].x2 = e.getX(); itemList[index].y1 = itemList[index].y2 = e.getY(); index++; itemList[index] = new Penci(); } public void mouseReleased(MouseEvent e) { itemList[index].x1 = e.getX(); itemList[index].y1 = e.getY(); //重新获取x1y1的值 虽然移动的时候x1y1在变但那时移动监听的不是释放监听的 //如果不重新获取坐标将变成默认的0,0 itemList[index].x2 = e.getX(); itemList[index].y2 = e.getY(); repaint(); index++; itemList[index] = new Penci(); } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } } class mouseb extends MouseMotionAdapter { public void mouseDragged(MouseEvent e) { itemList[index - 1].x1 = itemList[index].x2 = itemList[index].x1 = e.getX(); itemList[index - 1].y1 = itemList[index].y2 = itemList[index].y1 = e.getY(); //按住移动是获取了x1y1的坐标但这坐标不会赋值给释放 动作不同不会相互赋值 //index++; itemList[index] = new Penci(); repaint(); } public void mouseMoved(MouseEvent e) { } } class DrawPanel extends JPanel { public DrawPanel() { setBackground(Color.white); addMouseListener(new mousea()); addMouseMotionListener(new mouseb()); } @Override public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D) g; //定义画笔 int j = 0; while (j <= index) { drew(g2d, itemList[j]); j++; } } void drew(Graphics2D g2d, drewings i) { i.drew(g2d);//将画笔传入到各个子类中,用来完成各自的绘图 } } } class drewings implements Serializable { int x1 , y1 , x2, y2; void drew(Graphics2D g2d) { } } class Penci extends drewings{ void drew(Graphics2D g2d) { g2d.drawLine(x1, y1, x2, y2); //System.out.println(x1); } }
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 教育专区 > 小学其他

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

关于我们      便捷服务       自信AI       AI导航        抽奖活动

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

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

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

关注我们 :微信公众号    抖音    微博    LOFTER 

客服