Java アプリケーションで geom 領域を使用しています。このアプリケーションでは、JFrame で領域をペイントし、JFrame で動的な四角形を描画して geom 領域を減算していますが、正常に動作しますが、マウスを離すと消えたり、領域にツールチップ テキストが表示されると邪魔になったりすることがあります。
これが私のコードです。
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g.create();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setStroke(dashed);
//g.setColor(Color.red);
if(x1 ==0 && y1 == 0){
g2.dispose();
}
else{
g2.setColor(Color.BLACK);
g2.draw(shape);
g2.setColor(Color.black);
Area area = new Area();
area.add(new Area(new Rectangle2D.Float(0, 0, getWidth(), getHeight())));
g2.setColor(Color.BLACK.brighter());
int width = getWidth() - 1;
int height = getHeight() - 1;
int openWidth = 200;
int openHeight = 200;
int x = (width - openWidth) / 2;
int y = (height - openHeight) / 2;
area.subtract(new Area(shape));
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f));
g2.fill(area);
g2.setColor(Color.WHITE);
g2.setStroke(new BasicStroke(1.5f));
g2.fillRect(x3-8, y3-8, 8, 8);
g2.fillRect(x3-8, (y3+height1/2)-8, 8, 8);
g2.fillRect(x3-8, y3+height1, 8, 8);
g2.fillRect((x3+width1/2)-8, y3-8, 8, 8);
g2.fillRect(x3+width1, y3-8, 8, 8);
g2.fillRect((x3+width1/2)-8, y3+height1, 8, 8);
g2.fillRect(x3+width1, y3+height1, 8, 8);
g2.fillRect(x3+width1, (y3+height1/2)-8, 8, 8);
g2.dispose();
}
}
ここで形状は Rectangle であり、mouseReleased 関数が呼び出されると、領域がしばらく消えると、mouseListener を介して動的にサイズ変更されます。また、ツールチップ テキスト付きのボタンをいくつか使用したので、ツールチップ テキストが表示されると、その領域が邪魔になり、ぎこちなく見えます。
CropScreen.java
package Main;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Area;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.stream.ImageOutputStream;
import javax.swing.*;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;
public class CropScreen extends JComponent
implements MouseListener, MouseMotionListener {
static Point startPoint = new Point(), endPoint = new Point();
private File movieFolder = null;
ActionListener a1,a2,a3,a4;
final JPanel panel =new JPanel();
JPanel np =null,bp,im=null;
String cl;
Rectangle shape = null;
JScrollPane sp=null;
private static final long serialVersionUID = 1L;
private JLabel mousePosition;
int x, y,c=0,m=0;
int x1, x2, y1, y2,x4,x5,y4,y5;
int w, h,width,height,width2,height2;
private JLabel recStart;
private JLabel recStop;
private JLabel cords;
private int mousePressedX, mousePressedY;
private int width1;
private int height1;
int x3, y3;
JFrame frame = new JFrame("Edit Images");
final static float dash1[] = { 2.0f };
final static BasicStroke dashed = new BasicStroke(1.0f,
BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 10.0f, dash1, 0.0f);
JButton blur,pencil,pencil1,clear,b,txt,saveedit,Pencil;
Boolean etb = true;
ImageIcon save = new ImageIcon(ClassLoader.getSystemResource("images/save.png"));
ImageIcon close = new ImageIcon(ClassLoader.getSystemResource("images/close.png"));
ImageIcon tool = new ImageIcon(ClassLoader.getSystemResource("images/tool.png"));
ImageIcon browse = new ImageIcon(ClassLoader.getSystemResource("images/browse.png"));
JButton jp,brows;
Robot robot;
JFrame window = null;
JButton jp1;
JButton but = new JButton();
JPanel p = new JPanel();
JPanel sz = new JPanel();
JLabel size = new JLabel();
MainIconPage d =null;
CropScreen component;
//
boolean dragging = false;
final int PROX_DIST = 3;
public CropScreen()
{
component = this;
if(window == null)
window = new JFrame("Edit Pane");
if(d == null)
d = new MainIconPage();
pencil = new JButton(tool);
pencil.setToolTipText("Edit Selected Area !");
but.setEnabled(false);
sz.add(but);
sz.setVisible(true);
d.main.add(but);
p.setBackground(new Color(50,50,50,1));
d.main.add(p);
//super( "Rectangle Drawer" );
jp =new JButton(save);
jp.setToolTipText("Save Selected Area !");
jp1 = new JButton(close);
jp1.setToolTipText("Close");
brows = new JButton(browse);
brows.setToolTipText("Browse Directory !");
brows.setPreferredSize(new Dimension(40,40));
jp1.setPreferredSize(new Dimension(40,40));
jp.setPreferredSize(new Dimension(40,40));
pencil.setPreferredSize(new Dimension(40,40));
p.add(jp);
p.add(jp1);
p.add(pencil);
p.add(brows);
p.setVisible(false);
mousePosition = new JLabel();
mousePosition.setHorizontalAlignment(SwingConstants.CENTER);
panel.add( mousePosition, BorderLayout.CENTER );
JLabel text1 = new JLabel();
text1.setText( "At the center the mouse pointer's coordinates will be displayed." );
panel.add( text1, BorderLayout.SOUTH );
recStart = new JLabel();
panel.add(recStart, BorderLayout.WEST);
recStop = new JLabel();
panel.add(recStop, BorderLayout.EAST);
cords = new JLabel();
panel.add(cords, BorderLayout.NORTH);
addMouseListener( this );
addMouseMotionListener( this );
}
public void mouseClicked( MouseEvent event )
{
x1 = 0;
y1=0;
w = 0;
h=0;
x3 = 0;
y3 = 0;
width1=0;
height1 = 0;
x4 = 0;
y4 = 0;
x5 = 0;
y5 = 0;
width2=0;
height2 = 0;
window.getContentPane().removeAll();
d.r = true;
d.jfc = null;
jp.removeActionListener(a1);
jp1.removeActionListener(a2);
brows.removeActionListener(a3);
pencil.removeActionListener(a4);
}
public void mousePressed( MouseEvent event )
{
if(d.r==false){if(this.getCursor() != Cursor.getDefaultCursor()) {
dragging = true;
}}else{
if(shape == null)
shape = new Rectangle();
if(!but.isShowing()){
but.setVisible(true);
}
mousePressedX = event.getX();
mousePressedY = event.getY();
c =0;
but.setText("0x0");
size.setText("0x0");
startPoint = endPoint = event.getPoint();
if(m==1){
sz.setVisible(false);
}else{sz.setVisible(true);}
but.setBounds(x1, y1-30, 100, 30);
p.setVisible(false);
window.getContentPane().removeAll();
window.setExtendedState(JFrame.MAXIMIZED_BOTH);
d.et.displayImage = null;
d.et.OSC = null;
d.et.currentColor = Color.BLACK;
d.et.currentTool = EditTool.Tool.CURVE;
}}
public void mouseReleased( MouseEvent event )
{
if(d.r == false){
dragging = false;
endPoint = event.getPoint();
if(x1<x2 &&y1<y2 ){
p.setBounds(x3+width1,y3+height1-200,50,200);}
p.setVisible(true);
}
else{
d.r =false;
endPoint = event.getPoint();
c=1;
if(x1<x2 &&y1<y2 ){
p.setBounds(x2+10,y2-200,50,200);}
else if(x1 == x2 && y1<y2){p.setBounds(x2-60,y2-200,50,200);}
else if(x1 == x2 && y1==y2){p.setBounds(x2-60,y2,50,200);}
else if(x1 < x2 && y1==y2){p.setBounds(x2+10,y2,50,200);}
if(m==1){
p.setVisible(false);
}else{p.setVisible(true);}
m=1;
x1 = 0;
y1=0;
w = 0;
h=0;
shape.reshape(x3, y3, width1, height1);
a1 = new ActionListener(){
public void actionPerformed(ActionEvent e){
try { x1 = (startPoint.x < endPoint.x) ? startPoint.x : endPoint.x;
y1 = (startPoint.y < endPoint.y) ? startPoint.y : endPoint.y;
x2 = (startPoint.x > endPoint.x) ? startPoint.x : endPoint.x;
y2 = (startPoint.y > endPoint.y) ? startPoint.y : endPoint.y;
width = (x2-2 - x1) + 1;
height = (y2-2 - y1) + 1;
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy_MM_dd_h_mm_ss_a");
String formattedDate = sdf.format(date);
robot = new Robot();
BufferedImage screenShot = robot.createScreenCapture(new Rectangle(x3, y3, width1,height1));
if( movieFolder == null){
movieFolder = null;
if(d.jfc == null)
d.jfc = new JFileChooser();
d.jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int r = d.jfc.showSaveDialog(d.jfc);
if (r == JFileChooser.APPROVE_OPTION) {
movieFolder = d.jfc.getSelectedFile();
}}
ImageIO.write(screenShot, "JPG", new File(movieFolder,"screenShot"+formattedDate+".png"));
} catch (IOException ex) {
Logger.getLogger(CropScreen.class.getName()).log(Level.SEVERE, null, ex);
} catch (AWTException ex) {
Logger.getLogger(CropScreen.class.getName()).log(Level.SEVERE, null, ex);
}
}
};
jp.addActionListener(a1);
a2 = new ActionListener(){
public void actionPerformed(ActionEvent ae){
p.setVisible(false);
but.setVisible(false);
d.main.dispose();
x1 = 0;
y1=0;
w = 0;
h=0;
x3 = 0;
y3 = 0;
width1=0;
height1 = 0;
jp.removeActionListener(a1);
jp1.removeActionListener(a2);
brows.removeActionListener(a3);
pencil.removeActionListener(a4);
movieFolder = null;
}
};
jp1.addActionListener(a2);
//-----------------------------------------------------------------------
a3 = new ActionListener(){
public void actionPerformed(ActionEvent ae){
try { x1 = (startPoint.x < endPoint.x) ? startPoint.x : endPoint.x;
y1 = (startPoint.y < endPoint.y) ? startPoint.y : endPoint.y;
x2 = (startPoint.x > endPoint.x) ? startPoint.x : endPoint.x;
y2 = (startPoint.y > endPoint.y) ? startPoint.y : endPoint.y;
width = (x2-2 - x1) + 1;
height = (y2-2 - y1) + 1;
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy_MM_dd_h_mm_ss_a");
String formattedDate = sdf.format(date);
robot = new Robot();
BufferedImage screenShot = robot.createScreenCapture(new Rectangle(x3, y3, width1,height1));
movieFolder = null;
if(d.jfc == null)
d.jfc = new JFileChooser();
d.jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int r = d.jfc.showSaveDialog(d.jfc);
if (r == JFileChooser.APPROVE_OPTION) {
movieFolder = d.jfc.getSelectedFile();
}
ImageIO.write(screenShot, "JPG", new File(movieFolder,"screenShot"+formattedDate+".png"));
} catch (IOException ex) {
Logger.getLogger(CropScreen.class.getName()).log(Level.SEVERE, null, ex);
} catch (AWTException ex) {
Logger.getLogger(CropScreen.class.getName()).log(Level.SEVERE, null, ex);
}
}
};
brows.addActionListener(a3);
//-----------------------------------------------------------------------
a4 = new ActionListener(){
public void actionPerformed(ActionEvent ae){
Date date1 = new Date();
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy_MM_dd_h_mm_ss_a");
String formattedDate1 = sdf1.format(date1);
try { x4 = (startPoint.x < endPoint.x) ? startPoint.x : endPoint.x;
y4 = (startPoint.y < endPoint.y) ? startPoint.y : endPoint.y;
x5 = (startPoint.x > endPoint.x) ? startPoint.x : endPoint.x;
y5 = (startPoint.y > endPoint.y) ? startPoint.y : endPoint.y;
width2 = (x5-2 - x4) + 1;
height2 = (y5-2 - y4) + 1;
robot = new Robot();
BufferedImage screenShot = robot.createScreenCapture(new Rectangle(x3+1, y3+1, width1,height1));
if( movieFolder == null){
movieFolder = null;
if(d.jfc == null)
d.jfc = new JFileChooser();
d.jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int r = d.jfc.showSaveDialog(d.jfc);
if (r == JFileChooser.APPROVE_OPTION) {
movieFolder = d.jfc.getSelectedFile();
}}
ImageIO.write(screenShot, "JPG", new File(movieFolder,"screenShot"+formattedDate1+".png"));
} catch (IOException ex) {
Logger.getLogger(CropScreen.class.getName()).log(Level.SEVERE, null, ex);
} catch (AWTException ex) {
Logger.getLogger(CropScreen.class.getName()).log(Level.SEVERE, null, ex);
}
x1 = 0;
y1=0;
w = 0;
h=0;
x3 = 0;
y3 = 0;
width1=0;
height1 = 0;
x4 = 0;
y4 = 0;
x5 = 0;
y5 = 0;
width2=0;
height2 = 0;
MainIconPage d1 = new MainIconPage();
p.setVisible(false);
but.setVisible(false);
d1.main.dispose();
try {
cl = movieFolder.toURL().toString().substring(6)+"/screenShot"+formattedDate1+".png";
} catch (MalformedURLException ex) {
Logger.getLogger(CropScreen.class.getName()).log(Level.SEVERE, null, ex);
}
//System.out.println(d.bs);
if(d.et == null){
d.et=new EditTool(cl);
d.et.s1 = "";}
else{d.et.s1 = "";
d.et.repaint();
d.et.cl(cl);
}
jp.removeActionListener(a1);
jp1.removeActionListener(a2);
brows.removeActionListener(a3);
pencil.removeActionListener(a4);
}
};
pencil.addActionListener(a4);
}}
public void mouseEntered( MouseEvent event )
{
this.setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
}
public void mouseExited( MouseEvent event )
{
mousePosition.setText( "Mouse outside window" );
}
public void mouseDragged( MouseEvent event )
{if(d.r ==false){
if(dragging) {
endPoint = event.getPoint();
Point p1 = event.getPoint();
Rectangle r = component.shape;
int type = component.getCursor().getType();
int dx = p1.x - r.x;
int dy = p1.y - r.y;
switch(type) {
case Cursor.N_RESIZE_CURSOR:
int height=height1 = Math.abs(r.height - dy);
x3=r.x;
y3=r.y+dy;
shape.reshape(r.x, r.y+dy, r.width, height);
break;
case Cursor.NW_RESIZE_CURSOR:
int width =width1 = Math.abs(r.width - dx);
height=height1 = Math.abs(r.height - dy);
x3=r.x+dx;
y3=r.y+dy;
shape.reshape(r.x+dx, r.y+dy, width, height);
break;
case Cursor.W_RESIZE_CURSOR:
width =width1 = Math.abs(r.width - dx);
x3=r.x+dx;
y3=r.y;
shape.reshape(r.x+dx, r.y, width, r.height);
break;
case Cursor.SW_RESIZE_CURSOR:
width =width1 = Math.abs(r.width - dx);
height=height1 = Math.abs(dy);
x3=r.x+dx;
y3=r.y;
shape.reshape(r.x+dx, r.y, width, height);
break;
case Cursor.S_RESIZE_CURSOR:
height=height1 = Math.abs(dy);
x3=r.x;
y3=r.y;
shape.reshape(r.x, r.y, r.width, height);
break;
case Cursor.SE_RESIZE_CURSOR:
width =width1 = Math.abs(dx);
height=height1 = Math.abs(dy);
x3=r.x;
y3=r.y;
shape.reshape(r.x, r.y, width, height);
break;
case Cursor.E_RESIZE_CURSOR:
width =width1 = Math.abs(dx);
x3=r.x;
y3=r.y;
shape.reshape(r.x, r.y, width, r.height);
break;
case Cursor.NE_RESIZE_CURSOR:
width =width1 = Math.abs(dx);
height=height1 = Math.abs(r.height - dy);
x3=r.x;
y3=r.y+dy;
shape.reshape(r.x, r.y+dy, width, height);
break;
default:
System.out.println("unexpected type: " + type);
}
if(c == 0){endPoint = event.getPoint();}
x1 = (startPoint.x < endPoint.x) ? startPoint.x : endPoint.x;
y1 = (startPoint.y < endPoint.y) ? startPoint.y : endPoint.y;
x2 = (startPoint.x > endPoint.x) ? startPoint.x : endPoint.x;
y2 = (startPoint.y > endPoint.y) ? startPoint.y : endPoint.y;
width = (x2-2 - x1) + 1;
height = (y2-2 - y1) + 1;
p.setVisible(true);
if(x1<x2 &&y1<y2 ){
but.setBounds(x3, y3-30, 100, 30);
p.setBounds(x3+width1,y3+height1-200,50,200);}
shape.reshape(x3, y3, width1, height1);
but.setText(width1+"x"+height1);
repaint();
}
}
else{
int mouseDraggedX = event.getX();
int mouseDraggedY = event.getY();
width1 = Math.abs(mouseDraggedX - mousePressedX);
height1 = Math.abs(mouseDraggedY - mousePressedY);
x3 = Math.min(mouseDraggedX, mousePressedX);
y3 = Math.min(mouseDraggedY, mousePressedY);
m=0;
if(c == 0){endPoint = event.getPoint();}
x1 = (startPoint.x < endPoint.x) ? startPoint.x : endPoint.x;
y1 = (startPoint.y < endPoint.y) ? startPoint.y : endPoint.y;
x2 = (startPoint.x > endPoint.x) ? startPoint.x : endPoint.x;
y2 = (startPoint.y > endPoint.y) ? startPoint.y : endPoint.y;
width = (x2-2 - x1) + 1;
height = (y2-2 - y1) + 1;
if(x1<x2 &&y1<y2 ){
but.setBounds(x3, y3-30, 100, 30);}
shape.reshape(x3, y3, width1, height1);
but.setText(width+"x"+height);
repaint();
}}
public void mouseMoved( MouseEvent event )
{
if(d.r == false){
Point p = event.getPoint();
if(!isOverRect(p)) {
if(component.getCursor() != Cursor.getDefaultCursor()) {
component.setCursor(Cursor.getDefaultCursor());
}
return;
}
int outcode = getOutcode(p);
Rectangle r = component.shape;
switch(outcode) {
case Rectangle.OUT_TOP:
if(Math.abs(p.y - r.y) < PROX_DIST) {
component.setCursor(Cursor.getPredefinedCursor(
Cursor.N_RESIZE_CURSOR));
}
break;
case Rectangle.OUT_TOP + Rectangle.OUT_LEFT:
if(Math.abs(p.y - r.y) < PROX_DIST &&
Math.abs(p.x - r.x) < PROX_DIST) {
component.setCursor(Cursor.getPredefinedCursor(
Cursor.NW_RESIZE_CURSOR));
}
break;
case Rectangle.OUT_LEFT:
if(Math.abs(p.x - r.x) < PROX_DIST) {
component.setCursor(Cursor.getPredefinedCursor(
Cursor.W_RESIZE_CURSOR));
}
break;
case Rectangle.OUT_LEFT + Rectangle.OUT_BOTTOM:
if(Math.abs(p.x - r.x) < PROX_DIST &&
Math.abs(p.y - (r.y+r.height)) < PROX_DIST) {
component.setCursor(Cursor.getPredefinedCursor(
Cursor.SW_RESIZE_CURSOR));
}
break;
case Rectangle.OUT_BOTTOM:
if(Math.abs(p.y - (r.y+r.height)) < PROX_DIST) {
component.setCursor(Cursor.getPredefinedCursor(
Cursor.S_RESIZE_CURSOR));
}
break;
case Rectangle.OUT_BOTTOM + Rectangle.OUT_RIGHT:
if(Math.abs(p.x - (r.x+r.width)) < PROX_DIST &&
Math.abs(p.y - (r.y+r.height)) < PROX_DIST) {
component.setCursor(Cursor.getPredefinedCursor(
Cursor.SE_RESIZE_CURSOR));
}
break;
case Rectangle.OUT_RIGHT:
if(Math.abs(p.x - (r.x+r.width)) < PROX_DIST) {
component.setCursor(Cursor.getPredefinedCursor(
Cursor.E_RESIZE_CURSOR));
}
break;
case Rectangle.OUT_RIGHT + Rectangle.OUT_TOP:
if(Math.abs(p.x - (r.x+r.width)) < PROX_DIST &&
Math.abs(p.y - r.y) < PROX_DIST) {
component.setCursor(Cursor.getPredefinedCursor(
Cursor.NE_RESIZE_CURSOR));
}
break;
default:
component.setCursor(Cursor.getDefaultCursor());
}
}
}
private int getOutcode(Point p) {
Rectangle r = (Rectangle)component.shape.clone();
r.grow(-PROX_DIST, -PROX_DIST);
return r.outcode(p.x, p.y);
}
private boolean isOverRect(Point p) {
Rectangle r = (Rectangle)component.shape.clone();
r.grow(PROX_DIST, PROX_DIST);
return r.contains(p);
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g.create();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setStroke(dashed);
if(x1 ==0 && y1 == 0){
g2.dispose();
}
else{
g2.setColor(Color.BLACK);
g2.draw(shape);
g2.setColor(Color.black);
Area area = new Area();
area.add(new Area(new Rectangle2D.Float(0, 0, getWidth(), getHeight())));
g2.setColor(Color.BLACK.brighter());
int width = getWidth() - 1;
int height = getHeight() - 1;
int openWidth = 200;
int openHeight = 200;
int x = (width - openWidth) / 2;
int y = (height - openHeight) / 2;
area.subtract(new Area(shape));
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f));
g2.fill(area);
g2.setColor(Color.WHITE);
g2.setStroke(new BasicStroke(1.5f));
g2.fillRect(x3-8, y3-8, 8, 8);
g2.fillRect(x3-8, (y3+height1/2)-8, 8, 8);
g2.fillRect(x3-8, y3+height1, 8, 8);
g2.fillRect((x3+width1/2)-8, y3-8, 8, 8);
g2.fillRect(x3+width1, y3-8, 8, 8);
g2.fillRect((x3+width1/2)-8, y3+height1, 8, 8);
g2.fillRect(x3+width1, y3+height1, 8, 8);
g2.fillRect(x3+width1, (y3+height1/2)-8, 8, 8);
g2.dispose();
}
}
boolean selectedExistingRectangle(int x, int y) {
return shape != null && shape.contains(x, y);
}
void setRectangle(int x, int y) {
//shape = new Rectangle(x-(size/2), y-(size/2), 0, 0);
shape.reshape(x, y, width, height);
repaint();
}
public static String removeExtension(String s) {
String separator = System.getProperty("file.separator");
String filename;
int lastSeparatorIndex = s.lastIndexOf(separator);
if (lastSeparatorIndex == -1) {
filename = s;
} else {
filename = s.substring(lastSeparatorIndex + 1);
}
int extensionIndex = filename.lastIndexOf(".");
if (extensionIndex == -1)
return filename;
return filename.substring(0, extensionIndex);
}
}
前もって感謝します。