import javax.swing.event.MouseInputAdapter;
import java.awt.event.MouseEvent;
import java.awt.*;
import javax.swing.*;
import java.awt.geom.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.BoxLayout;
public class SelectionDemo {
JFrame frame = new JFrame("SelectionDemo");
JButton Get = new JButton("Get it");
JButton finish = new JButton("Ok");
JButton cancel = new JButton("Cancel");
boolean iscut = false;
Rectangle2D rectsave = new Rectangle2D.Double(0.0, 0.0, 0.0, 0.0);
Rectangle2D orgrect = new Rectangle2D.Double(0.0, 0.0, 0.0, 0.0);
private SelectEndingHandler handler;
static ImageIcon imgicon = null;
JLabel label;
JPanel bigpanel = new JPanel();
JPanel panel = new JPanel();
JPanel buttonpanel = new JPanel();
SelectionDemo() {
}
public void show(SelectEndingHandler handler, String starFile) {
this.handler = handler;
finish.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (iscut == true) {
frame.dispose();
} else {
JOptionPane.showMessageDialog(null, "u have not cut already");
}
}
});
cancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
saveAs sve = new saveAs("./images/selectsave.JPG", imgicon.getImage(), orgrect);
sve.save();
frame.dispose();
dispose();
}
});
Get.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if ((rectsave.getWidth() >= 150) && (rectsave.getHeight() >= 150)) {
saveAs sve = new saveAs("./images/selectsave.JPG", imgicon.getImage(), rectsave);
sve.save();
dispose();
iscut = true;
} else {
JOptionPane.showMessageDialog(null, "Size of the image is too small");
}
}
});
label = new JLabel("Drag within the image.");
imgicon = createImageIcon(starFile);
orgrect = new Rectangle2D.Double(0.0, 0.0, (double) imgicon.getIconWidth(), (double) imgicon.getIconHeight());
panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
SelectionArea area = new SelectionArea(imgicon, this);
panel.add(area);
label.setLabelFor(area);
panel.add(label);
buttonpanel.add(Get);
buttonpanel.add(finish);
buttonpanel.add(cancel);
area.setAlignmentX(Component.LEFT_ALIGNMENT);
label.setAlignmentX(Component.LEFT_ALIGNMENT);
bigpanel.add(panel);
bigpanel.add(buttonpanel);
frame.setLocation(80, 80);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setResizable(false);
frame.add(bigpanel);
//Display the window.
frame.pack();
frame.setVisible(true);
}
public void dispose() {
handler.selectEnded(this);
}
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("size=(" + rect.width + "X" + rect.height + ")");
}
protected static ImageIcon createImageIcon(String path) {
if (path != null) {
return new ImageIcon(path);
} else {
System.err.println("Couldn't find file: " + path);
return null;
}
}
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);
}
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);
rectsave = 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);
}
}
}
}
分享到:
相关推荐
java notejava notejava notejava notejava notejava notejava note
华为Note10手机原厂维修图纸 原理图 电路图 元件图 手机故障维修图 华为Note10 位置图 位号图 元件图.exe 华为Note10 原理图 电路图.exe 华为Note10 注释图 故障标注 主板元器件位置图.exe 华为Note10 原厂图 维修...
NoteExpress参考文献管理软件简要教程 一.什么是NoteExpress 参考文献管理软件 二.NoteExpress软件安装 三.建立自己第一个NoteExpress 数据库 四.以参考文献为中心的个人知识管理 五. 研究时心得的记录---笔记...
黑群无更新NoteStation后显示一直加载,通过安装会旧版本可解决。 最近套件中心提示NoteStation要升级(版本 2.6.0-1407)升级完成后打开NoteStation一直显示 “正在加载” ,不过数据是都在的。 二、解决办法 1、...
红米Note4驱动是小米官方提供的红米note4手机usb的驱动程序,很多用户的手机初次连接到电脑的时候总是会提供未知设备接入,其实这就是驱动没安装的缘故,这里给大家提供的这款驱动可以解决手机连电脑的时候出现的...
红米note移动版手机USB驱动程序是一款可以有效解决红米note移动版手机无法正常连接电脑的驱动工具,本站提供了红米note移动版手机USB驱动下载地址,有需要的朋友们可以前来下载使用。 本驱动是小米最新产品红米note...
解锁后刷入 红米Note7|lineage18.1| 安卓11.0|顶级流畅|唯美清新|全新手势|强力推荐
红米note10pro谷歌相机
魅蓝note6解BL卡刷补丁 解锁bl专用文件 第三方twrp下刷入的
红米note11t pro面具一键root资源 持续更新 1---手机必须解锁bl后才可以使用此资源 2---资源内一键刷写root脚本。方便操作。你只需要进入fast模式链接电脑运行即可 3---资源带操作步骤说明。 4----资源在网盘。下载...
cloudnote云笔记基础资料含sql。资料来源于网络,站位
NoteExpress破解版 v2.2.0.674,科研文献管理利器。
MIDI音阶对应Note 速查表
红米note 4g电路原理图。可用于手机维修,可放大 无水印。
设备名称 红米Note11T Pro ROM版本 V13.1.22.8.22.DEV MIUI版本 13 安卓版本 12.0 版本类型 开发版MIUI 刷包类型 卡刷包
NoteExpress 参考文献
noteexpress是一款功能强大的文献管理软件,由国内软件公司开发推出,不过是收费的,比较贵,使用期为30天,该文档给出了处理过期问题的方法,可以参考。
红米note4强刷专用DA 如果不使用该DA文件,线刷红米note4时会出现cust.img报错或者其他问题 ,使用da文件,可以使用flash_tool线刷工具直接对红米note4进行刷机,无需解锁bootloader,可用于设备救砖,稳定版刷开发...
红米note9 4G通病,无缘无故出现9008模式的,不开机,一关机就不能开机,充电无反应。 重做cpu-字库-电源 无效-返修,关机后又不开机的。 不是电源虚焊,不是cpu和字库虚焊,另有它病,最终秒杀方案,完美解决无返修...