一段在jcomponent上绘制矩形框的代码,很不错的代码。

对图像画包围框进行标注 代码已经封装成DLL,你只需要打开项目,将图像路径修改成你的即可。 (我用的VS为2013)。生成的txt内容为:图像名 标签 x1 y1 x2 y2。(包围框坐标) 立即下载

/* From http://java.sun.com/docs/books/tutorial/index.html */
/*
 * SelectionDemo.java is a 1.4 application that requires one other file:
 * images/starfield.gif
 */

import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.event.MouseInputAdapter;
import java.awt.*;
import java.awt.event.MouseEvent;

/*
 * This displays an image. When the user drags within the image, this program
 * displays a rectangle and a string indicating the bounds of the rectangle.
 */
public class SelectionDemo {
  JLabel label;

  static String starFile = "starfield.gif";

  private void buildUI(Container container, ImageIcon image) {
    container.setLayout(new BoxLayout(container, BoxLayout.PAGE_AXIS));

    SelectionArea area = new SelectionArea(image, this);
    container.add(area);

    label = new JLabel("Drag within the image.");
    label.setLabelFor(area);
    container.add(label);

    //Align the left edges of the components.
    area.setAlignmentX(Component.LEFT_ALIGNMENT);
    label.setAlignmentX(Component.LEFT_ALIGNMENT); //redundant
  }

  public void updateLabel(Rectangle rect) {
    int width = rect.width;
    int height = rect.height;

    //Make the coordinates look OK if a dimension is 0.
    if (width == 0) {
      width = 1;
    }
    if (height == 0) {
      height = 1;
    }

    label.setText("Rectangle goes from (" + rect.x + ", " + rect.y
        + ") to (" + (rect.x + width - 1) + ", "
        + (rect.y + height - 1) + ").");
  }

  /** Returns an ImageIcon, or null if the path was invalid. */
  protected static ImageIcon createImageIcon(String path) {
    java.net.URL imgURL = SelectionDemo.class.getResource(path);
    if (imgURL != null) {
      return new ImageIcon(imgURL);
    } else {
      System.err.println("Couldn't find file: " + path);
      return null;
    }
  }

  /**
   * Create the GUI and show it. For thread safety, this method should be
   * invoked from the event-dispatching thread.
   */
  private static void createAndShowGUI() {
    //Make sure we have nice window decorations.
    JFrame.setDefaultLookAndFeelDecorated(true);

    //Create and set up the window.
    JFrame frame = new JFrame("SelectionDemo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //Set up the content pane.
    SelectionDemo controller = new SelectionDemo();
    controller.buildUI(frame.getContentPane(), createImageIcon(starFile));

    //Display the window.
    frame.pack();
    frame.setVisible(true);
  }

  public static void main(String[] args) {
    //Schedule a job for the event-dispatching thread:
    //creating and showing this application's GUI.
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
      public void run() {
        createAndShowGUI();
      }
    });
  }

  private class SelectionArea extends JLabel {
    Rectangle currentRect = null;

    Rectangle rectToDraw = null;

    Rectangle previousRectDrawn = new Rectangle();

    SelectionDemo controller;

    public SelectionArea(ImageIcon image, SelectionDemo controller) {
      super(image); //This component displays an image.
      this.controller = controller;
      setOpaque(true);
      setMinimumSize(new Dimension(10, 10)); //don't hog space

      MyListener myListener = new MyListener();
      addMouseListener(myListener);
      addMouseMotionListener(myListener);
    }

    private class MyListener extends MouseInputAdapter {
      public void mousePressed(MouseEvent e) {
        int x = e.getX();
        int y = e.getY();
        currentRect = new Rectangle(x, y, 0, 0);
        updateDrawableRect(getWidth(), getHeight());
        repaint();
      }

      public void mouseDragged(MouseEvent e) {
        updateSize(e);
      }

      public void mouseReleased(MouseEvent e) {
        updateSize(e);

 //在这里矩形框绘制完成后可以做很多应用
      }

      /*
       * Update the size of the current rectangle and call repaint.
       * Because currentRect always has the same origin, translate it if
       * the width or height is negative.
       *
       * For efficiency (though that isn't an issue for this program),
       * specify the painting region using arguments to the repaint()
       * call.
       * 
       */
      void updateSize(MouseEvent e) {
        int x = e.getX();
        int y = e.getY();
        currentRect.setSize(x - currentRect.x, y - currentRect.y);
        updateDrawableRect(getWidth(), getHeight());
        Rectangle totalRepaint = rectToDraw.union(previousRectDrawn);
        repaint(totalRepaint.x, totalRepaint.y, totalRepaint.width,
            totalRepaint.height);
      }
    }

    protected void paintComponent(Graphics g) {
      super.paintComponent(g); //paints the background and image

      //If currentRect exists, paint a box on top.
      if (currentRect != null) {
        //Draw a rectangle on top of the image.
        g.setXORMode(Color.white); //Color of line varies
        //depending on image colors
        g.drawRect(rectToDraw.x, rectToDraw.y, rectToDraw.width - 1,
            rectToDraw.height - 1);

        controller.updateLabel(rectToDraw);
      }
    }

    private void updateDrawableRect(int compWidth, int compHeight) {
      int x = currentRect.x;
      int y = currentRect.y;
      int width = currentRect.width;
      int height = currentRect.height;

      //Make the width and height positive, if necessary.
      if (width < 0) {
        width = 0 - width;
        x = x - width + 1;
        if (x < 0) {
          width += x;
          x = 0;
        }
      }
      if (height < 0) {
        height = 0 - height;
        y = y - height + 1;
        if (y < 0) {
          height += y;
          y = 0;
        }
      }

      //The rectangle shouldn't extend past the drawing area.
      if ((x + width) > compWidth) {
        width = compWidth - x;
      }
      if ((y + height) > compHeight) {
        height = compHeight - y;
      }

      //Update rectToDraw after saving old value.
      if (rectToDraw != null) {
        previousRectDrawn.setBounds(rectToDraw.x, rectToDraw.y,
            rectToDraw.width, rectToDraw.height);
        rectToDraw.setBounds(x, y, width, height);
      } else {
        rectToDraw = new Rectangle(x, y, width, height);
      }
    }
  }
}

 

jComponent:一个简单的jQuery组件库 jComponent库 下载:每个人均可免费使用230多个jComponents 当前版本: v18 >= jQuery +1.7 >= IE9 与 , 或 与 , , , 等一起工作 您可以通过jComponent包装数千个jQuery插件 最好与一起使用 您需要查看: 主页: : 下载现有组件 作者+联系人 彼得·希尔卡(PeterŠirka)-https 立即下载

相关推荐

RAGFlow源码解析-2、从源码构架ragflow v0.25.0镜像

本文详细记录了从零开始构建自定义RAGFlow镜像lt/ragflow:0.25.0的全过程。首先介绍了环境准备,包括检查Node.js、Docker版本和项目结构。然后详细说明了构建步骤:编译前端、创建自定义Dockerfile、构建镜像和验证。接着讲解了docker-compose配置修改要点,包括网络定义、MySQL依赖处理等。最后总结了部署过程中遇到的五个关键问题及解决方案,包括networks未定义、MySQL服务依赖、tiktoken文件缺失、数据库配置错误和端口映射问题。通过完整的操作流程和问

liutao841204的专栏 364

【Flutter】SelectionArea之实现文本全选复制功能

SelectionArea实现一个可复用的文本的全选、复制功能的widget组件

brave的博客 3078

Win10局域网内文件共享+远程桌面控制保姆级教程(含常见问题解决)

本文提供Win10局域网内文件共享和远程桌面控制的详细教程,包括环境检查、共享文件夹设置、远程桌面连接步骤及常见问题解决方案。通过内置功能实现高效办公协作,提升多设备协同工作效率,特别适合需要频繁传输文件或远程控制场景的专业人士。

rose2的博客 777

Java鼠标事件绘制简单图形-直线、矩形、椭圆

 博主是一枚正在学习Java语言的小白,这篇博客也是博主的第一篇博客,想在这里分享给大家。这个绘图程序的关键是怎样使用Java的鼠标事件处理,我做的这个程序就是利用鼠标按下“public void mousePressed(MouseEvent e)”事件和鼠标释放“public void mouseReleased(MouseEvent e)”事件来完成绘制简单图形的。 除了绘制图案以外,我还...

quanbisen的博客 1万+

海康工业相机使用教程

海康工业相机连接电脑教程

qq_43251445的博客 2万+

BEV感知技术全景解析:从纯视觉到多传感器融合的实战指南

本文全面解析了自动驾驶中的BEV感知技术,从纯视觉到多传感器融合的实战应用。文章深入探讨了BEV如何提供“上帝视角”,对比了IPM、Lift-Splat-Shoot及Transformer等核心实现范式,并详细阐述了多传感器融合(尤其是中融合)的工程实践与优势,为开发者提供了从理论到原型搭建的完整指南。

win55的博客 1139

JFrame, JPanel, JComponent

在进行GUI编写时,经常会遇到这几个类。它们之间的关系是怎么样的呢?这需要从包含层次(containment hierarchy)说起。以下内容参考java tutorials。 Swing库提供了3个顶层容器类(Top-Level container class): JFrame, JDialog, JApplet。在创建GUI时应该遵循以下原则: 要显示在屏幕上,每个GUI部件必须在一个树状的

简单快乐 3292

ArcGIS JSAPI 高级教程 - 添加多个 box 立方体 - 通过顶点、索引、UV 自定义每个 box、每个面不同颜色

ArcGIS JSAPI 高级教程 - 添加多个 box 立方体 - 通过顶点、索引、UV 自定义每个 box、每个面不同颜色。

非科班Java出身GISer的博客 400

JComponent - 组件显示消息

/** * 关于getPreferredSize * 对于组件的首选尺寸,最大尺寸和最小尺寸,并不决定组件的实际尺寸。 * 只是尺寸请求,在确定组件尺寸时,该请求可能被考虑也可能不考虑。 * getXxxSize返回尺寸的过程: * 1.如果显示地使用setXxxSize设置了组件的尺寸,则该尺寸值从getXxxSize返回 * 2.如果没有,不过组件有一个UI代表,则调用这个UI代表

rexih的专栏 795

SAP Parallel Accounting(平行分类账业务)配置及操作手册(超详细的说明和测试)

非常详细的介绍了SAP 的平行分类账配置以及测试效果哦

nopicture的博客 4341

基于Python的上市公司年报PDF文本抓取与财务风险分析系统

随着大数据时代的到来,金融数据分析已成为投资决策和风险管理的重要工具。上市公司年报包含了丰富的财务信息和经营数据,通过对这些数据的提取和分析,可以评估公司的财务状况和潜在风险。本文将详细介绍如何使用Python构建一个完整的上市公司年报文本抓取与财务风险分析系统,涵盖PDF文本提取、数据清洗、财务指标计算和风险分析等全流程。

2201_76125261的博客 311

Swing JComponent类

+java.lang.Object   +java.awt.Component     +java.awt.Container       +javax.swing.JComponent API中文解释:   该类是除顶层容器外所有 Swing 组件的基类。要使用继承自 JComponent 的组件,必须将该组件置于一个根为顶层 Swing 容器的包含层次结构(contain

TuxedoLinux的博客 3563

推理引擎终极对决:TensorRT vs ONNX Runtime,YOLOv8速度差30%的核心原因与实操指南

若你使用NVIDIA GPU,且追求极致速度和低延迟,TensorRT是唯一选择,尤其是边缘设备和实时场景;若你需要跨硬件部署、快速验证原型,或预算有限(无需NVIDIA GPU),ONNX Runtime是性价比之王;对于YOLOv8这类检测模型,TensorRT的FP16/INT8量化是“免费的速度提升”,建议优先采用。最后提醒:推理引擎的优化是“细节决定成败”——同样的引擎,不同的预处理方式、显存配置、批大小,都会导致10%-20%的性能差异。

专注于Python爬虫开发,分享爬虫技巧、项目实战与反爬经验,使用Scrapy、BeautifulSoup等工具,解决数据抓取难题。 1026

Java图形化:JComponent组件

JComponent是一个和JPanel很相似的组件的容器,但又有区别。 JPanel不透明,所以在需要透明等应用场景的条件比较麻烦,使用JComponent比较方便。 package swing; import javax.swing.*; import java.awt.*; /** * @author: 我的袜子都是洞 * @description: * @path: tourJa...

weixin_33835103的博客 1060

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

JAVA使用字符画出矩形方法_java绘制矩形

为什么我画不出矩形?代码哪里有问题?求大神指点importjava.awt.*;importjava.awt.event.*;importjava.awt.geom.Rectangle2D;importjavax.swing.*;publicclassGameTest{privateFrame...为什么我画不出矩形?代码哪里有问题?求大神指点import java.awt.*;import ja...

weixin_29082143的博客 917

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

用java在JPanel中建一个表格_如何使用Java在JPanel中显示JTable?

导入和表模型作为练习留给此代码的用户.此外,为简单起见,任意选择面板布局.public class JTableDisplay {public JTableDisplay() {JFrame frame = new JFrame("JTable Test Display");JPanel panel = new JPanel();panel.setLayout(new BorderLayout()...

weixin_36117191的博客 1050

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

一个Java画图板程序的设计

本文讲述一个画图板应用程序的设计,屏幕抓图如下。这篇文章带有三个附件,其中两个jar文件都是j2sdk1.4.2_08编译打包,包含源代码,可执行,如下表: 附件名称及链接 详情 jDraw_basic.jar 本文是基

6567

Java中jcomponent意思,Java图形化:JComponent组件

JComponent是一个和JPanel很相似的组件的容器,但又有区别。 JPanel不透明,所以在需要透明等应用场景的条件比较麻烦,使用JComponent比较方便。package swing;import javax.swing.*;import java.awt.*;/*** @author: 我的袜子都是洞* @description:* @path: tourJava-swing-Not...

weixin_34662372的博客 1578

Swing之画图形_矩形、椭圆、三角形实例

1 package com.szsm.swing.framepanel; import java.awt.Graphics; import javax.swing.JFrame; import javax.swing.JPanel; import com.szsm.swing.parent.FrameSet; public class CavasFrame extends Frame

有 prepare , No out 6522
上一篇: [转]利用Java事件处理机制实现录制、回放功能
下一篇: [转]Swing技巧总结
飞羽飞之猪
博客等级 码龄21年 127粉丝 85原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值