91av视频/亚洲h视频/操亚洲美女/外国一级黄色毛片 - 国产三级三级三级三级

  • 大小: 2KB
    文件類型: .zip
    金幣: 2
    下載: 1 次
    發(fā)布日期: 2021-05-13
  • 語言: Java
  • 標(biāo)簽: java??gui??swing??awt??Graphics2D??

資源簡介

java GUI awt 實(shí)現(xiàn)鼠標(biāo)繪制矩形,鼠標(biāo)拖動矩形,鼠標(biāo)改變矩形大小功能. 其它圖形的繪制方法參考: https://blog.csdn.net/xietansheng/article/details/55669157

資源截圖

代碼片段和文件信息

package?com.test;

import?java.awt.Cursor;
import?java.awt.Graphics;
import?java.awt.Graphics2D;
import?java.awt.Rectangle;
import?java.awt.event.MouseAdapter;
import?java.awt.event.MouseEvent;
import?java.awt.event.MouseMotionListener;
import?java.awt.geom.Point2D;
import?java.util.ArrayList;

import?javax.swing.JComponent;
import?javax.swing.Jframe;
import?javax.swing.JPanel;

public?class?MouseComponent?extends?JComponent?implements?MouseMotionListener?{
????private?Rectangle?rect;
????private?boolean?isTopLeft;
????private?boolean?isTop;
????private?boolean?isTopRight;
????private?boolean?isRight;
????private?boolean?isBottomRight;
????private?boolean?isBottom;
????private?boolean?isBottomLeft;
????private?boolean?isDrag;
????private?boolean?isLeft;
????//矩形最小寬和高為多少
????private?final?static?int?RESIZE_WIDTH?=?5;
????private?final?static?int?MIN_WIDTH?=?20;
????private?final?static?int?MIN_HEIGHT?=?20;
????
????
????private?static?final?int?SIDELENGTH?=?50;//單擊增加的矩形大小
????private?ArrayList?squares;//所有的矩形
????private?Rectangle?current;
????public?MouseComponent()?{
????????this.squares?=?new?ArrayList();
????????this.rect?=?null;
????????addMouseMotionListener(this);
????????this.addMouseListener(new?MouseHandle());//?監(jiān)聽鼠標(biāo)點(diǎn)擊事件
????????//this.addMouseMotionListener(new?MouseMotionHandler());//?監(jiān)聽鼠標(biāo)移動事件
????}

????//?如果鼠標(biāo)單擊屏幕的地方有矩形則不繪制矩形
????public?Rectangle?find(Point2D?p)?{
????????for?(Rectangle?r?:?squares)?{
????????????if?(r.contains(p))
????????????????return?r;
????????}
????????return?null;
????}

????//?繪制矩形
????public?void?paintComponent(Graphics?g)?{
????????Graphics2D?g2?=?(Graphics2D)?g;
????????for?(Rectangle?r?:?squares)?{
????????????g2.draw(r);
????????}
????}

????//?添加矩形到屏幕
????public?void?add(Point2D?p)?{
????????double?x?=?p.getX();
????????double?y?=?p.getY();
????????rect?=?new?Rectangle((int)?(x?-?SIDELENGTH?/?2)?(int)?(y?-?SIDELENGTH?/?2)?SIDELENGTH?SIDELENGTH);
????????squares.add(rect);
????????repaint();
????}

????//?刪除矩形
????public?void?remove(Rectangle?s)?{
????????if?(s?==?null)
????????????return;
????????if?(s?==?rect)
????????????rect?=?null;
????????squares.remove(s);
????????repaint();
????}

????private?class?MouseHandle?extends?MouseAdapter?{

????????@Override
????????public?void?mouseClicked(MouseEvent?e)?{
????????????rect?=?find(e.getPoint());
????????????//?當(dāng)前位置如果有矩形,且點(diǎn)擊大于2次,則刪除矩形
????????????if?(rect?!=?null?&&?e.getClickCount()?>=?2)
????????????????remove(rect);
????????}

????????@Override
????????public?void?mousePressed(MouseEvent?e)?{
????????????rect?=?find(e.getPoint());
????????????//?當(dāng)前位置沒有矩形則繪制
????????????if?(rect?==?null)
????????????????add(e.getPoint());
????????}

????}

????private?class?MouseMotionHandler?implements?MouseMotionListener?{

????????@Override
????????public?void?mouseDragged(MouseEvent?e)?{
????????????//?如果當(dāng)前位置有矩形,進(jìn)行拖拽。
????????????if?(rect

?屬性????????????大小?????日期????時(shí)間???名稱
-----------?---------??----------?-----??----
?????文件????????8452??2019-05-28?12:17??MouseComponent.java

評論

共有 條評論