18 个解决方案
#1
小例子,可以参考下:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class RectLabel extends JLabel {
private int x1 = 0;
private int y1 = 0;
private int x2 = 0;
private int y2 = 0;
private boolean current = true;
public RectLabel() {
super();
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent event) {
Point p = event.getPoint();
x1 = (int) p.getX();
y1 = (int) p.getY();
}
public void mouseReleased(MouseEvent event) {
Point p = event.getPoint();
x2 = (int) p.getX();
y2 = (int) p.getY();
current = true;
repaint();
}
});
addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent event) {
Point p = event.getPoint();
x2 = (int) p.getX();
y2 = (int) p.getY();
current = false;
repaint();
}
});
}
// 画圆的背景和标签
protected void paintComponent(Graphics g) {
if (!current)
return;
g.setColor(Color.red);
g.fillRect(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1 - x2),
Math.abs(y1 - y2));
super.paintComponent(g);
}
// 用简单的弧画按钮的边界。
protected void paintBorder(Graphics g) {
if (current)
return;
g.setColor(getForeground());
g.drawRect(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1 - x2),
Math.abs(y1 - y2));
}
public static void main(String[] args) {
JLabel test = new RectLabel();
JFrame frame = new JFrame();
frame.getContentPane().add(test);
frame.setSize(300, 300);
frame.setVisible(true);
}
}
#2
这是我之前用过的画圆形按钮的程序,改了改。
注释是以前的,可以忽略。
注释是以前的,可以忽略。
#3
你这样是之前画的矩形每次都清掉了,可不可以让之前的不要消失?
#4
我的目的是要做个类似于橡皮擦的功能,在Lable上放一张图片,然后再图片上拖动鼠标就画矩形填充背景色,例如这样:
#5
我也没有好办法。
想到一个:把所有画过的区域都存起来,每次重画都把它们都画一遍。
想到一个:把所有画过的区域都存起来,每次重画都把它们都画一遍。
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Shape;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class RectLabel extends JLabel {
private int x1 = 0;
private int y1 = 0;
private int x2 = 0;
private int y2 = 0;
private boolean current = true;
private List<Shape> list = new ArrayList<Shape>();
public RectLabel() {
super();
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent event) {
Point p = event.getPoint();
x1 = (int) p.getX();
y1 = (int) p.getY();
}
public void mouseReleased(MouseEvent event) {
Point p = event.getPoint();
x2 = (int) p.getX();
y2 = (int) p.getY();
list.add(new Rectangle2D.Double(Math.min(x1, x2), Math.min(y1,
y2), Math.abs(x1 - x2), Math.abs(y1 - y2)));
current = true;
repaint();
}
});
addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent event) {
Point p = event.getPoint();
x2 = (int) p.getX();
y2 = (int) p.getY();
current = false;
repaint();
}
});
}
protected void paintComponent(Graphics g) {
if (!current)
return;
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.red);
for (Shape s : list) {
g2.fill(s);
}
}
protected void paintBorder(Graphics g) {
if (current)
return;
Graphics2D g2 = (Graphics2D) g;
g.setColor(getForeground());
for (Shape s : list) {
g2.fill(s);
}
g.drawRect(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1 - x2),
Math.abs(y1 - y2));
}
public static void main(String[] args) {
JLabel test = new RectLabel();
JFrame frame = new JFrame();
frame.getContentPane().add(test);
frame.setSize(300, 300);
frame.setVisible(true);
}
}
#6
主面板,可以直接调用
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Stroke;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.JPanel;
public class DragDao extends JPanel implements MouseMotionListener,
MouseListener {
/** 矩形的起点 左上角* */
private Point rectStart = null;
/** 矩形的终点 右下角* */
private Point rectStop = null;
/** 是否绘制虚线矩形* */
private boolean dottedTag = true;
public DragDao() {
this.setBackground(Color.WHITE);
this.setSize(1000, 1000);
rectStart = new Point(0, 0);
rectStop = new Point(0, 0);
this.addMouseListener(this);
this.addMouseMotionListener(this);
}
@Override
protected void paintComponent(Graphics g) {
// TODO Auto-generated method stub
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
/** 矩形的宽度* */
int w = Math.abs(rectStop.x - rectStart.x);
/** 矩形的高度* */
int h = Math.abs(rectStop.y - rectStart.y);
/** 起点终点的最小值作为起点* */
Point min = new Point(0, 0);
min.x = Math.min(rectStop.x, rectStart.x);
min.y = Math.min(rectStop.y, rectStart.y);
/** 起点终点的最小值作为终点* */
Point max = new Point(0, 0);
max.x = Math.max(rectStop.x, rectStart.x);
max.y = Math.max(rectStop.y, rectStart.y);
/** 如果是绘制虚线矩形* */
if (dottedTag) {
/** new float[] { 5, 5, } 数组可以改变虚线的密度* */
Stroke dash = new BasicStroke(0.5f, BasicStroke.CAP_BUTT,
BasicStroke.JOIN_ROUND, 0.5f, new float[] { 5, 5, }, 0f);
g2.setStroke(dash);
g2.setColor(Color.BLUE);
g2.drawRect(min.x, min.y, w, h);
} else {
g2.setStroke(new BasicStroke());
g2.setColor(new Color(198, 214, 239));
g.fillRect(min.x, min.y, w, h);
}
}
/** *鼠标拖动 */
public void mouseDragged(MouseEvent arg0) {
// TODO Auto-generated method stub
/** 随着鼠标的拖动 改变终点* */
rectStop = arg0.getPoint();
this.repaint();
}
public void mouseMoved(MouseEvent arg0) {
// TODO Auto-generated method stub
}
/** 鼠标按键在组件上单击(按下并释放)时调用* */
public void mouseClicked(MouseEvent arg0) {
// TODO Auto-generated method stub
}
/** 鼠标按键在组件上按下时调用 */
public void mousePressed(MouseEvent arg0) {
/** 设置可以进行绘制* */
dottedTag = true;
/** 记录起始点* */
rectStart = arg0.getPoint();
/** 记录起终点* */
rectStop = rectStart;
}
/** 鼠标按钮在组件上释放时调用* */
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
dottedTag = false;
this.repaint();
}
/** 鼠标进入到组件上时调用* */
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
/** 鼠标离开组件时调用* */
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
}
测试类
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JFrame;
public class DragDemo extends JFrame{
public DragDao drag = new DragDao();
public DragDemo() {
super();
drag = new DragDao();
this.add(drag,BorderLayout.CENTER);
Initialization("XuXian JuXing");
}
private void Initialization(String title) {
Dimension screen = getToolkit().getScreenSize();
this.setSize(screen.width*4/5,screen.height*4/5);
this.setTitle(title);
this.setLocationRelativeTo(null);
this.setResizable(false);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
public static void main(String[] args) {
new DragDemo();
}
}
#7
效果图
#8
#9
#10
另外,是先通过菜单栏打开一张图片,然后再对图片编辑,先谢谢了。
#11
你的程序已经可以了,不需要整合我那个了。
就是你有一个地方有问题:
另外,
1.orgx, orgy - 60, endx - orgx, endy - orgy + 60
这个-60 +60是什么意思?不明白。我去掉之后效果是对的
2.如果鼠标从右上往左下画,你的程序会出问题,应该判断初始坐标和当前坐标哪个小
3.如果拖拽很大再拖回去,draw的线框不能消失。这个我还没想到好办法。如果可以的话,建议在图片上面放一个透明的Label,然后功能都实现在这个Label上。
就是你有一个地方有问题:
listener mouseListener = new listener(this);
this.label.addMouseListener(mouseListener);
// this.label.addMouseMotionListener(new motionListener(this));
this.label.addMouseMotionListener(new motionListener(mouseListener));//按照你的代码,这里的mouseListener应该和上面那个一样,不能new一个新的
另外,
1.orgx, orgy - 60, endx - orgx, endy - orgy + 60
这个-60 +60是什么意思?不明白。我去掉之后效果是对的
2.如果鼠标从右上往左下画,你的程序会出问题,应该判断初始坐标和当前坐标哪个小
3.如果拖拽很大再拖回去,draw的线框不能消失。这个我还没想到好办法。如果可以的话,建议在图片上面放一个透明的Label,然后功能都实现在这个Label上。
#12
我这个实现还是有问题的,如果在鼠标拖拽过程中不在一条直线上就会出现多个矩形,然后就会擦不掉,说白了就是没有清除掉拖动的痕迹。这个要怎么弄呢?有没好办法?
#13
这个有那么点效果,不过我想要能保留每次话的图,能否请大神在我发的代码基础上帮我改改呢?
#14
你的程序已经可以了,不需要整合我那个了。
就是你有一个地方有问题:
Java code
?
1234
listener mouseListener = new listener(this); this.label.addMouseListener(mouseListener); // this.label.addMouseMotionListener(new mo……
我这个实现还是有问题的,如果在鼠标拖拽过程中不在一条直线上就会出现多个矩形,然后就会擦不掉,说白了就是没有清除掉拖动的痕迹。这个要怎么弄呢?有没好办法?
这个就是我说的第3点。暂时没有想到好办法。
#15
引用 12 楼 zaihushiqu 的回复:
Quote: 引用 11 楼 abc41106 的回复:
你的程序已经可以了,不需要整合我那个了。
就是你有一个地方有问题:
Java code
?
1234
listener mouseListener = new listener(this); this.label.addMouseListener(……
我整合了下你的例子到我的类中,但是我还是保存不了前一次的修改,图片打开的时候好像也要多点一下界面里面,求助,代码贴出来你看看;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FileDialog;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.Stroke;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.DirectColorModel;
import java.awt.image.FilteredImageSource;
import java.awt.image.ImageProducer;
import java.awt.image.RGBImageFilter;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.filechooser.FileFilter;
class ImageTransferFrame2 extends JFrame implements ActionListener {
public static BufferedImage bimg = null;
private static Object source = null;
public static Image theImage;
private JLabel label;
private JMenuItem openItem;
private JScrollPane pane;
private JMenuItem exitItem;
private JButton zoomOut;
private JButton zoomIn;
private JButton rotateLeft;
private JButton rotateRight;
private JButton save;
private JPanel editPane;
public static List<Shape> list = new ArrayList<Shape>();
private JSlider sliderBrightness = null;
public ImageTransferFrame2() {
setSize(800, 500);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
Container contentPane = getContentPane();
this.label = new RectLabel(new ImageIcon());
this.pane = new JScrollPane(this.label);
contentPane.add(this.pane, "Center");
contentPane.add(getEditorPanel(), "North");
JMenu fileMenu = new JMenu("File");
this.openItem = new JMenuItem("Open");
this.openItem.addActionListener(this);
fileMenu.add(this.openItem);
this.exitItem = new JMenuItem("Exit");
this.exitItem.addActionListener(this);
fileMenu.add(this.exitItem);
JMenuBar menuBar = new JMenuBar();
menuBar.add(fileMenu);
setJMenuBar(menuBar);
}
public JPanel getEditorPanel() {
this.editPane = new JPanel();
this.zoomOut = new JButton("放大");
zoomOut.addActionListener(this);
zoomOut.setName("放大");
this.zoomIn = new JButton("缩小");
zoomIn.addActionListener(this);
zoomIn.setName("缩小");
this.rotateLeft = new JButton("右旋转90度");
rotateLeft.addActionListener(this);
rotateLeft.setName("右旋转90度");
this.rotateRight = new JButton("左旋转90度");
rotateRight.addActionListener(this);
rotateRight.setName("左旋转90度");
JLabel lBrightness = new JLabel("亮度调节");
this.sliderBrightness = new JSlider(0);
this.sliderBrightness.setValue(50);
this.sliderBrightness.setPaintLabels(true);
this.sliderBrightness.addChangeListener(new slider_changeListener());
this.save = new JButton("保存");
save.setName("保存");
save.addActionListener(this);
editPane.setLayout(new FlowLayout(0));
editPane.add(save);
editPane.add(zoomOut);
editPane.add(zoomIn);
editPane.add(rotateLeft);
editPane.add(rotateRight);
editPane.add(lBrightness);
editPane.add(this.sliderBrightness);
return editPane;
}
public void actionPerformed(ActionEvent evt) {
this.source = evt.getSource();
if (source == this.openItem) {
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new File("."));
chooser.setFileFilter(new FileFilter() {
public boolean accept(File f) {
String name = f.getName().toLowerCase();
return ((name.endsWith(".gif")) || (name.endsWith(".jpg"))
|| (name.endsWith(".jpeg")) || (f.isDirectory()));
}
public String getDescription() {
return "Image files";
}
});
int r = chooser.showOpenDialog(this);
if (r == 0) {
String name = chooser.getSelectedFile().getAbsolutePath();
File file = new File(name);
try {
BufferedImage im = ImageIO.read(file);
setImage(im);
} catch (IOException e) {
e.printStackTrace();
}
}
} else if (source == this.exitItem) {
System.exit(0);
}
else if(source==this.save)
{
save();
}
else if (source == this.zoomOut)
{// 放大
this.theImage=this.theImage.getScaledInstance((int) (this.bimg.getWidth(this) * 1.2),(int) (this.bimg.getHeight(this) * 1.2), Image.SCALE_SMOOTH);
this.bimg = new BufferedImage(this.theImage.getWidth(this),this.theImage.getHeight(this), 1);
this.bimg.getGraphics().drawImage(this.theImage, 0, 0, this);
setImage(this.bimg);
}
else if (source == this.zoomIn)
{// 缩小
this.theImage=this.theImage.getScaledInstance((int) (this.bimg.getWidth(this) * 0.8),
(int) (this.bimg.getHeight(this) *0.8), Image.SCALE_SMOOTH);
this.bimg = new BufferedImage(this.theImage.getWidth(this),this.theImage.getHeight(this), 1);
this.bimg.getGraphics().drawImage(this.theImage, 0, 0, this);
setImage(this.bimg);
}
else if (source == this.rotateLeft)
{// 左旋转90度
bimg = rotateImg(bimg,90,Color.WHITE);
setImage(this.bimg);
}
else if (source == this.rotateRight)
{// 右旋转90度
bimg = rotateImg(bimg,-90,Color.WHITE);
setImage(this.bimg);
}
}
private void save() {
FileDialog fileDialog = new FileDialog( new Frame() , "请指定一个文件名", FileDialog.SAVE );
fileDialog.show();
if(fileDialog.getFile()==null)
{
return;
}
else{
// 目标文件
try {
BufferedImage inputbig = new BufferedImage(256, 256, BufferedImage.TYPE_INT_BGR);
Graphics2D g = (Graphics2D) inputbig.getGraphics();
g.drawImage(this.bimg, 0, 0,256,256,null); //画图
g.dispose();
inputbig.flush();
ImageIO.write(inputbig, "jpg", new File(fileDialog.getDirectory() + fileDialog.getFile() + ".jpg")); //将其保存在C:/imageSort/targetPIC/下
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
public void resize(){//改變大小
bimg = new BufferedImage(this.bimg.getWidth(this), this.bimg.getHeight(this),BufferedImage.TYPE_3BYTE_BGR);
JLabel jlbImg = new JLabel(new ImageIcon(bimg));//在JLabel上放置bufImg,用來繪圖
this.removeAll();
this.add(jlbImg);
jlbImg.setBounds(new Rectangle(0, 0, this.bimg.getWidth(this), this.bimg.getHeight(this)));
//畫出原本圖形//
Graphics2D g2d_bufImg = (Graphics2D) bimg.getGraphics();
g2d_bufImg.setPaint(Color.white);
g2d_bufImg.fill(new Rectangle2D.Double(0,0,this.bimg.getWidth(this), this.bimg.getHeight(this)));
g2d_bufImg.drawImage(bimg,0,0,this);
}
public static BufferedImage rotateImg( BufferedImage image, int degree, Color bgcolor )
{
int iw = image.getWidth();//原始图象的宽度
int ih = image.getHeight();//原始图象的高度
int w=0;
int h=0;
int x=0;
int y=0;
degree=degree%360;
if(degree<0)
degree=360+degree;//将角度转换到0-360度之间
double ang=degree* 0.0174532925;//将角度转为弧度
if(degree == 180|| degree == 0 || degree == 360)
{
w = iw;
h = ih;
}
else if(degree == 90|| degree == 270)
{
w = ih;
h = iw;
}
else
{
int d=iw+ih;
w=(int)(d*Math.abs(Math.cos(ang)));
h=(int)(d*Math.abs(Math.sin(ang)));
}
x = (w/2)-(iw/2);//确定原点坐标
y = (h/2)-(ih/2);
BufferedImage rotatedImage=new BufferedImage(w,h,image.getType());
Graphics gs=rotatedImage.getGraphics();
gs.setColor(bgcolor);
gs.fillRect(0,0,w,h);//以给定颜色绘制旋转后图片的背景
AffineTransform at=new AffineTransform();//二维变换类
at.rotate(ang,w/2,h/2);//旋转图象
at.translate(x,y); //连接此变换与平移变换。
/*
此类使用仿射转换来执行从源图像或 Raster 中 2D 坐标到目标图像或 Raster 中
2D 坐标的线性映射。所使用的插值类型由构造方法通过一个 RenderingHints 对象或通过此类中定义的整数插值类型之一来指定。
*/
AffineTransformOp op=new AffineTransformOp(at,AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
//转换源 BufferedImage 并将结果存储在目标 BufferedImage 中。
op.filter(image, rotatedImage);
image=rotatedImage;
return image;
}
public void setImage(BufferedImage image) {
this.theImage = image;
ImageProducer picSource = image.getSource();
MyImage bwFilter = new MyImage();
this.theImage = createImage(new FilteredImageSource(picSource, bwFilter));
this.bimg = image;
this.theImage.flush();
//this.label.setIcon(new ImageIcon(this.theImage));
this.label = new RectLabel(new ImageIcon(this.theImage));
}
public static void main(String[] args)
{
JFrame frame1 = new ImageTransferFrame2();
frame1.setLocationRelativeTo(null);//
frame1.setTitle("Frame 1");
frame1.show();
}
}
#16
还有些类:
class slider_changeListener implements ChangeListener {
public void stateChanged(ChangeEvent arg0) {
JSlider slider = (JSlider) arg0.getSource();
}
}
class MyImage extends RGBImageFilter
{
int width = 0;
int height = 0;
int alpha = 0;
int start = 0;
int end = 0;
public MyImage() {
this.canFilterIndexColorModel = true;
}
public int filterRGB(int x, int y, int rgb)
{
DirectColorModel dcm = (DirectColorModel)ColorModel.getRGBdefault();
int red = dcm.getRed(rgb);
int green = dcm.getGreen(rgb);
int blue = dcm.getBlue(rgb);
Color newColor = new Color(red, green, blue);
return newColor.getRGB();
}
}
class PointsSet
{
private ArrayList points;
public PointsSet()
{
this.points = new ArrayList();
}
public PointsSet(int initCap) {
this.points = new ArrayList(initCap);
}
public void addPoint(int x, int y) {
int size = this.points.size();
if (size > 0) {
Point point = (Point)this.points.get(size - 1);
if ((point.x == x) && (point.y == y))
return;
}
Point p = new Point();
p.x = x;
p.y = y;
this.points.add(p);
}
public int[][] getPoints() {
int size = this.points.size();
if (size == 0)
return null;
int[][] result = new int[2][size];
for (int i = 0; i < size; ++i) {
Point p = (Point)this.points.get(i);
result[0][i] = p.x;
result[1][i] = p.y;
}
return result;
}
public int[][] getPoints(int x, int y) {
int size = this.points.size();
if (size == 0)
return null;
int[][] result = new int[2][size + 1];
int i=0;
for (; i < size; ++i) {
Point p = (Point)this.points.get(i);
result[0][i] = p.x;
result[1][i] = p.y;
}
result[0][i] = x;
result[1][i] = y;
return result;
}
}
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Shape;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import java.util.List;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
public class RectLabel extends JLabel {
private int x1 = 0;
private int y1 = 0;
private int x2 = 0;
private int y2 = 0;
private boolean current = true;
private static List<Shape> shapelist = new ArrayList<Shape>();
public static ImageIcon icon = null;
public RectLabel() {
}
public RectLabel(ImageIcon image) {
this.icon = image;
this.setIcon(icon);
repaint();
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent event) {
Point p = event.getPoint();
x1 = (int) p.getX();
y1 = (int) p.getY();
}
public void mouseReleased(MouseEvent event) {
Point p = event.getPoint();
x2 = (int) p.getX();
y2 = (int) p.getY();
shapelist.add(new Rectangle2D.Double(Math.min(x1, x2), Math.min(y1,
y2), Math.abs(x1 - x2), Math.abs(y1 - y2)));
current = true;
repaint();
}
});
addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent event) {
Point p = event.getPoint();
x2 = (int) p.getX();
y2 = (int) p.getY();
current = false;
repaint();
}
});
}
protected void paintComponent(Graphics g) {
if (!current)
return;
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.WHITE);
for (Shape s : shapelist) {
if(this.icon!=null)
{
g2.drawImage(this.icon.getImage(), 0, 0, 800, 560, this);
}
g2.fill(s);
repaint();
}
}
protected void paintBorder(Graphics g) {
if (current)
return;
Graphics2D g2 = (Graphics2D) g;
g.setColor(getForeground());
if(this.icon!=null)
{
g2.drawImage(this.icon.getImage(), 0, 0, 800, 560, this);
}
g.drawRect(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1 - x2), Math
.abs(y1 - y2));
repaint();
}
}
#17
参照上面保存每次都记录的思想,做一点小改动就可以了,在鼠标释放之后保存记录
代码如下
代码如下
package xuxian;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Shape;
import java.awt.Stroke;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JPanel;
public class DragDao extends JPanel implements MouseMotionListener,
MouseListener {
/** 矩形的起点 左上角* */
private static Point rectStart = null;
/** 矩形的终点 右下角* */
private static Point rectStop = null;
/** 是否绘制虚线矩形* */
private boolean dottedTag = true;
/*************这里更改了****************/
private List<Shape> list = new ArrayList<Shape>();
public DragDao() {
this.setBackground(Color.WHITE);
this.setSize(1000, 1000);
rectStart = new Point(0, 0);
rectStop = new Point(0, 0);
this.addMouseListener(this);
this.addMouseMotionListener(this);
// this.isClear = isClear;
}
@Override
protected void paintComponent(Graphics g) {
// TODO Auto-generated method stub
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
/** 矩形的宽度* */
int w = Math.abs(rectStop.x - rectStart.x);
/** 矩形的高度* */
int h = Math.abs(rectStop.y - rectStart.y);
/** 起点终点的最小值作为起点* */
Point min = new Point(0, 0);
min.x = Math.min(rectStop.x, rectStart.x);
min.y = Math.min(rectStop.y, rectStart.y);
/** 起点终点的最小值作为终点* */
Point max = new Point(0, 0);
max.x = Math.max(rectStop.x, rectStart.x);
max.y = Math.max(rectStop.y, rectStart.y);
/** 如果是绘制虚线矩形* */
if ( dottedTag) {
/** new float[] { 5, 5, } 数组可以改变虚线的密度* */
Stroke dash = new BasicStroke(0.5f, BasicStroke.CAP_BUTT,
BasicStroke.JOIN_ROUND, 0.5f, new float[] { 5, 5, }, 0f);
g2.setStroke(dash);
g2.setColor(Color.BLUE);
g2.drawRect(min.x, min.y, w, h);
} else {
list.add(new Rectangle2D.Double(min.x, min.y, w, h));
}
/*************这里更改了****************/
g2.setStroke(new BasicStroke());
g2.setColor(new Color(198, 214, 239));
for(Shape s:list){
g2.fill(s);
}
}
/** *鼠标拖动 */
public void mouseDragged(MouseEvent arg0) {
// TODO Auto-generated method stub
/** 随着鼠标的拖动 改变终点* */
rectStop = arg0.getPoint();
this.repaint();
}
public void mouseMoved(MouseEvent arg0) {
// TODO Auto-generated method stub
}
/** 鼠标按键在组件上单击(按下并释放)时调用* */
public void mouseClicked(MouseEvent arg0) {
// TODO Auto-generated method stub
}
/** 鼠标按键在组件上按下时调用 */
public void mousePressed(MouseEvent arg0) {
/** 设置可以进行绘制* */
dottedTag = true;
/** 记录起始点* */
rectStart = arg0.getPoint();
/** 记录起终点* */
rectStop = rectStart;
}
/** 鼠标按钮在组件上释放时调用* */
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
dottedTag = false;
this.repaint();
}
/** 鼠标进入到组件上时调用* */
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
/** 鼠标离开组件时调用* */
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
}
#18
引用 14 楼 abc41106 的回复:引用 12 楼 zaihushiqu 的回复:
Quote: 引用 11 楼 abc41106 的回复:
你的程序已经可以了,不需要整合我那个了。
就是你有一个地方有问题:
Java code
?
1234
listener mouseListener = new listener(t……
你要是整合我那个代码的话,那你的代码改动可就大了。。放大、缩小、保存,都要改。还不如重写一个了。。。
我只能把我的这部分给你改好。剩下的,只能你自己改了。。。
class RectLabel extends JLabel {
private int x1 = 0;
private int y1 = 0;
private int x2 = 0;
private int y2 = 0;
private boolean current = true;
private static List<Shape> shapelist = new ArrayList<Shape>();
public static ImageIcon icon = null;
public RectLabel() {
}
public RectLabel(ImageIcon image) {
this.icon = image;
this.setIcon(icon);
//repaint();
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent event) {
Point p = event.getPoint();
x1 = (int) p.getX();
y1 = (int) p.getY();
}
public void mouseReleased(MouseEvent event) {
Point p = event.getPoint();
x2 = (int) p.getX();
y2 = (int) p.getY();
shapelist.add(new Rectangle2D.Double(Math.min(x1, x2), Math
.min(y1, y2), Math.abs(x1 - x2), Math.abs(y1 - y2)));
current = true;
repaint();
}
});
addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent event) {
Point p = event.getPoint();
x2 = (int) p.getX();
y2 = (int) p.getY();
current = false;
repaint();
}
});
}
protected void paintComponent(Graphics g) {
if (!current)
return;
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.WHITE);
if (this.icon != null) {//这个要放到循环外面
g2.drawImage(this.icon.getImage(), 0, 0, 800, 560, this);
}
for (Shape s : shapelist) {
g2.fill(s);
// repaint();
}
}
protected void paintBorder(Graphics g) {
if (current)
return;
Graphics2D g2 = (Graphics2D) g;
g.setColor(getForeground());
if (this.icon != null) {
g2.drawImage(this.icon.getImage(), 0, 0, 800, 560, this);
}
for (Shape s : shapelist) {//这个循环按你的需求,想加就加,不加就去掉。
g2.fill(s);
}
g.drawRect(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1 - x2),
Math.abs(y1 - y2));
// repaint();
}
}
#1
小例子,可以参考下:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class RectLabel extends JLabel {
private int x1 = 0;
private int y1 = 0;
private int x2 = 0;
private int y2 = 0;
private boolean current = true;
public RectLabel() {
super();
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent event) {
Point p = event.getPoint();
x1 = (int) p.getX();
y1 = (int) p.getY();
}
public void mouseReleased(MouseEvent event) {
Point p = event.getPoint();
x2 = (int) p.getX();
y2 = (int) p.getY();
current = true;
repaint();
}
});
addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent event) {
Point p = event.getPoint();
x2 = (int) p.getX();
y2 = (int) p.getY();
current = false;
repaint();
}
});
}
// 画圆的背景和标签
protected void paintComponent(Graphics g) {
if (!current)
return;
g.setColor(Color.red);
g.fillRect(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1 - x2),
Math.abs(y1 - y2));
super.paintComponent(g);
}
// 用简单的弧画按钮的边界。
protected void paintBorder(Graphics g) {
if (current)
return;
g.setColor(getForeground());
g.drawRect(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1 - x2),
Math.abs(y1 - y2));
}
public static void main(String[] args) {
JLabel test = new RectLabel();
JFrame frame = new JFrame();
frame.getContentPane().add(test);
frame.setSize(300, 300);
frame.setVisible(true);
}
}
#2
这是我之前用过的画圆形按钮的程序,改了改。
注释是以前的,可以忽略。
注释是以前的,可以忽略。
#3
这是我之前用过的画圆形按钮的程序,改了改。
注释是以前的,可以忽略。
你这样是之前画的矩形每次都清掉了,可不可以让之前的不要消失?
#4
我的目的是要做个类似于橡皮擦的功能,在Lable上放一张图片,然后再图片上拖动鼠标就画矩形填充背景色,例如这样:
#5
我也没有好办法。
想到一个:把所有画过的区域都存起来,每次重画都把它们都画一遍。
想到一个:把所有画过的区域都存起来,每次重画都把它们都画一遍。
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Shape;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class RectLabel extends JLabel {
private int x1 = 0;
private int y1 = 0;
private int x2 = 0;
private int y2 = 0;
private boolean current = true;
private List<Shape> list = new ArrayList<Shape>();
public RectLabel() {
super();
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent event) {
Point p = event.getPoint();
x1 = (int) p.getX();
y1 = (int) p.getY();
}
public void mouseReleased(MouseEvent event) {
Point p = event.getPoint();
x2 = (int) p.getX();
y2 = (int) p.getY();
list.add(new Rectangle2D.Double(Math.min(x1, x2), Math.min(y1,
y2), Math.abs(x1 - x2), Math.abs(y1 - y2)));
current = true;
repaint();
}
});
addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent event) {
Point p = event.getPoint();
x2 = (int) p.getX();
y2 = (int) p.getY();
current = false;
repaint();
}
});
}
protected void paintComponent(Graphics g) {
if (!current)
return;
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.red);
for (Shape s : list) {
g2.fill(s);
}
}
protected void paintBorder(Graphics g) {
if (current)
return;
Graphics2D g2 = (Graphics2D) g;
g.setColor(getForeground());
for (Shape s : list) {
g2.fill(s);
}
g.drawRect(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1 - x2),
Math.abs(y1 - y2));
}
public static void main(String[] args) {
JLabel test = new RectLabel();
JFrame frame = new JFrame();
frame.getContentPane().add(test);
frame.setSize(300, 300);
frame.setVisible(true);
}
}
#6
主面板,可以直接调用
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Stroke;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.JPanel;
public class DragDao extends JPanel implements MouseMotionListener,
MouseListener {
/** 矩形的起点 左上角* */
private Point rectStart = null;
/** 矩形的终点 右下角* */
private Point rectStop = null;
/** 是否绘制虚线矩形* */
private boolean dottedTag = true;
public DragDao() {
this.setBackground(Color.WHITE);
this.setSize(1000, 1000);
rectStart = new Point(0, 0);
rectStop = new Point(0, 0);
this.addMouseListener(this);
this.addMouseMotionListener(this);
}
@Override
protected void paintComponent(Graphics g) {
// TODO Auto-generated method stub
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
/** 矩形的宽度* */
int w = Math.abs(rectStop.x - rectStart.x);
/** 矩形的高度* */
int h = Math.abs(rectStop.y - rectStart.y);
/** 起点终点的最小值作为起点* */
Point min = new Point(0, 0);
min.x = Math.min(rectStop.x, rectStart.x);
min.y = Math.min(rectStop.y, rectStart.y);
/** 起点终点的最小值作为终点* */
Point max = new Point(0, 0);
max.x = Math.max(rectStop.x, rectStart.x);
max.y = Math.max(rectStop.y, rectStart.y);
/** 如果是绘制虚线矩形* */
if (dottedTag) {
/** new float[] { 5, 5, } 数组可以改变虚线的密度* */
Stroke dash = new BasicStroke(0.5f, BasicStroke.CAP_BUTT,
BasicStroke.JOIN_ROUND, 0.5f, new float[] { 5, 5, }, 0f);
g2.setStroke(dash);
g2.setColor(Color.BLUE);
g2.drawRect(min.x, min.y, w, h);
} else {
g2.setStroke(new BasicStroke());
g2.setColor(new Color(198, 214, 239));
g.fillRect(min.x, min.y, w, h);
}
}
/** *鼠标拖动 */
public void mouseDragged(MouseEvent arg0) {
// TODO Auto-generated method stub
/** 随着鼠标的拖动 改变终点* */
rectStop = arg0.getPoint();
this.repaint();
}
public void mouseMoved(MouseEvent arg0) {
// TODO Auto-generated method stub
}
/** 鼠标按键在组件上单击(按下并释放)时调用* */
public void mouseClicked(MouseEvent arg0) {
// TODO Auto-generated method stub
}
/** 鼠标按键在组件上按下时调用 */
public void mousePressed(MouseEvent arg0) {
/** 设置可以进行绘制* */
dottedTag = true;
/** 记录起始点* */
rectStart = arg0.getPoint();
/** 记录起终点* */
rectStop = rectStart;
}
/** 鼠标按钮在组件上释放时调用* */
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
dottedTag = false;
this.repaint();
}
/** 鼠标进入到组件上时调用* */
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
/** 鼠标离开组件时调用* */
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
}
测试类
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JFrame;
public class DragDemo extends JFrame{
public DragDao drag = new DragDao();
public DragDemo() {
super();
drag = new DragDao();
this.add(drag,BorderLayout.CENTER);
Initialization("XuXian JuXing");
}
private void Initialization(String title) {
Dimension screen = getToolkit().getScreenSize();
this.setSize(screen.width*4/5,screen.height*4/5);
this.setTitle(title);
this.setLocationRelativeTo(null);
this.setResizable(false);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
public static void main(String[] args) {
new DragDemo();
}
}
#7
主面板,可以直接调用
Java code
?
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868……
效果图
#8
我也没有好办法。
想到一个:把所有画过的区域都存起来,每次重画都把它们都画一遍
有那么点我想要的效果了,我还不知道怎么整合到我的里面呢,求指教
下面是我写的:
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FileDialog;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.DirectColorModel;
import java.awt.image.FilteredImageSource;
import java.awt.image.ImageProducer;
import java.awt.image.RGBImageFilter;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.filechooser.FileFilter;
class ImageTransferFrame extends JFrame implements ActionListener {
public BufferedImage bimg = null;
private static Object source = null;
private static Clipboard localClipboard = new Clipboard("local");
private Image theImage;
private JLabel label;
private JMenuItem openItem;
private JScrollPane pane;
private JMenuItem exitItem;
private JMenuItem copyItem;
private JMenuItem pasteItem;
private JButton save;
private JButton zoomOut;
private JButton zoomIn;
private JButton rotateLeft;
private JButton rotateRight;
private JPanel editPane;
public ImageTransferFrame() {
setSize(800, 500);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
Container contentPane = getContentPane();
this.label = new JLabel();
this.label.addMouseListener(new listener(this));
//this.label.addMouseMotionListener(new motionListener(this));
this.label.addMouseMotionListener(new motionListener(new listener(this)));
this.pane = new JScrollPane(this.label);
contentPane.add(this.pane, "Center");
contentPane.add(getEditorPanel(), "North");
JMenu fileMenu = new JMenu("文件");
this.openItem = new JMenuItem("打开图片");
this.openItem.addActionListener(this);
fileMenu.add(this.openItem);
this.exitItem = new JMenuItem("退出");
this.exitItem.addActionListener(this);
fileMenu.add(this.exitItem);
JMenuBar menuBar = new JMenuBar();
menuBar.add(fileMenu);
setJMenuBar(menuBar);
}
public JPanel getEditorPanel() {
this.editPane = new JPanel();
this.zoomOut = new JButton("放大");
zoomOut.addActionListener(this);
zoomOut.setName("放大");
this.zoomIn = new JButton("缩小");
zoomIn.addActionListener(this);
zoomIn.setName("缩小");
this.rotateLeft = new JButton("右旋转90度");
rotateLeft.addActionListener(this);
rotateLeft.setName("右旋转90度");
this.rotateRight = new JButton("左旋转90度");
rotateRight.addActionListener(this);
rotateRight.setName("左旋转90度");
this.save = new JButton("保存");
save.addActionListener(this);
editPane.setLayout(new FlowLayout(0));
editPane.add(save);
editPane.add(zoomOut);
editPane.add(zoomIn);
editPane.add(rotateLeft);
editPane.add(rotateRight);
return editPane;
}
public void actionPerformed(ActionEvent evt) {
this.source = evt.getSource();
if (source == this.openItem) {
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new File("."));
chooser.setFileFilter(new FileFilter() {
public boolean accept(File f) {
String name = f.getName().toLowerCase();
return ((name.endsWith(".gif")) || (name.endsWith(".jpg"))
|| (name.endsWith(".jpeg")) || (f.isDirectory()));
}
public String getDescription() {
return "Image files";
}
});
int r = chooser.showOpenDialog(this);
if (r == 0) {
String name = chooser.getSelectedFile().getAbsolutePath();
File file = new File(name);
try {
BufferedImage im = ImageIO.read(file);
setImage(im);
} catch (IOException e) {
e.printStackTrace();
}
}
}
else if(source==this.save)
{
save();
}
else if (source == this.zoomOut)
{// 放大
this.theImage=this.theImage.getScaledInstance((int) (this.bimg.getWidth(this) * 1.2),(int) (this.bimg.getHeight(this) * 1.2), Image.SCALE_SMOOTH);
this.bimg = new BufferedImage(this.theImage.getWidth(this),this.theImage.getHeight(this), 1);
this.bimg.getGraphics().drawImage(this.theImage, 0, 0, this);
setImage(this.bimg);
}
else if (source == this.zoomIn)
{// 缩小
this.theImage=this.theImage.getScaledInstance((int) (this.bimg.getWidth(this) * 0.8),
(int) (this.bimg.getHeight(this) *0.8), Image.SCALE_SMOOTH);
this.bimg = new BufferedImage(this.theImage.getWidth(this),this.theImage.getHeight(this), 1);
this.bimg.getGraphics().drawImage(this.theImage, 0, 0, this);
setImage(this.bimg);
}
else if (source == this.rotateLeft)
{// 左旋转90度
bimg = rotateImg(bimg,90,Color.WHITE);
setImage(this.bimg);
}
else if (source == this.rotateRight)
{// 右旋转90度
bimg = rotateImg(bimg,-90,Color.WHITE);
setImage(this.bimg);
}
}
private void save() {
FileDialog fileDialog = new FileDialog( new Frame() , "请指定一个文件名", FileDialog.SAVE );
fileDialog.show();
if(fileDialog.getFile()==null)
{
return;
}
else{
// 目标文件
try {
BufferedImage inputbig = new BufferedImage(256, 256, BufferedImage.TYPE_INT_BGR);
Graphics2D g = (Graphics2D) inputbig.getGraphics();
g.drawImage(this.bimg, 0, 0,256,256,null); //画图
g.dispose();
inputbig.flush();
ImageIO.write(inputbig, "jpg", new File(fileDialog.getDirectory() + fileDialog.getFile() + ".jpg")); //将其保存在C:/imageSort/targetPIC/下
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
public void render()
{
setImage(this.bimg);
}
public void resize(){//改變大小
bimg = new BufferedImage(this.bimg.getWidth(this), this.bimg.getHeight(this),BufferedImage.TYPE_3BYTE_BGR);
JLabel jlbImg = new JLabel(new ImageIcon(bimg));//在JLabel上放置bufImg,用來繪圖
this.removeAll();
this.add(jlbImg);
jlbImg.setBounds(new Rectangle(0, 0, this.bimg.getWidth(this), this.bimg.getHeight(this)));
//畫出原本圖形//
Graphics2D g2d_bufImg = (Graphics2D) bimg.getGraphics();
g2d_bufImg.setPaint(Color.white);
g2d_bufImg.fill(new Rectangle2D.Double(0,0,this.bimg.getWidth(this), this.bimg.getHeight(this)));
g2d_bufImg.drawImage(bimg,0,0,this);
}
public static BufferedImage rotateImg( BufferedImage image, int degree, Color bgcolor )
{
int iw = image.getWidth();//原始图象的宽度
int ih = image.getHeight();//原始图象的高度
int w=0;
int h=0;
int x=0;
int y=0;
degree=degree%360;
if(degree<0)
degree=360+degree;//将角度转换到0-360度之间
double ang=degree* 0.0174532925;//将角度转为弧度
if(degree == 180|| degree == 0 || degree == 360)
{
w = iw;
h = ih;
}
else if(degree == 90|| degree == 270)
{
w = ih;
h = iw;
}
else
{
int d=iw+ih;
w=(int)(d*Math.abs(Math.cos(ang)));
h=(int)(d*Math.abs(Math.sin(ang)));
}
x = (w/2)-(iw/2);//确定原点坐标
y = (h/2)-(ih/2);
BufferedImage rotatedImage=new BufferedImage(w,h,image.getType());
Graphics gs=rotatedImage.getGraphics();
gs.setColor(bgcolor);
gs.fillRect(0,0,w,h);//以给定颜色绘制旋转后图片的背景
AffineTransform at=new AffineTransform();//二维变换类
at.rotate(ang,w/2,h/2);//旋转图象
at.translate(x,y); //连接此变换与平移变换。
/*
此类使用仿射转换来执行从源图像或 Raster 中 2D 坐标到目标图像或 Raster 中
2D 坐标的线性映射。所使用的插值类型由构造方法通过一个 RenderingHints 对象或通过此类中定义的整数插值类型之一来指定。
*/
AffineTransformOp op=new AffineTransformOp(at,AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
//转换源 BufferedImage 并将结果存储在目标 BufferedImage 中。
op.filter(image, rotatedImage);
image=rotatedImage;
return image;
}
public void setImage(BufferedImage image) {
this.theImage = image;
ImageProducer picSource = image.getSource();
MyImage bwFilter = new MyImage();
this.theImage = createImage(new FilteredImageSource(picSource, bwFilter));
this.bimg = image;
this.label.setIcon(new ImageIcon(this.theImage));
}
public static void main(String[] args)
{
JFrame frame1 = new ImageTransferFrame();
frame1.setTitle("Frame 1");
frame1.show();
}
}
#9
我也没有好办法。
想到一个:把所有画过的区域都存起来,每次重画都把它们都画一遍。
还有些类:
class listener extends MouseAdapter {
ImageTransferFrame app = null;
int orgx, orgy, endx, endy;
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
public listener(ImageTransferFrame a) {
this.app = a;
}
public void mousePressed(MouseEvent e) {
orgx = e.getX();
orgy = e.getY();
}
public void mouseReleased(MouseEvent e)
{
endx = e.getX();
endy = e.getY();
Graphics2D g = (Graphics2D) this.app.bimg.getGraphics();
g.setColor(Color.WHITE);
g.fillRect(orgx, orgy-60, endx - orgx, endy - orgy+60);
this.app.render();
}
}
class motionListener implements MouseMotionListener {
listener lis = null;
int orgx, orgy, endx, endy;
public motionListener(listener l) {
this.lis = l;
}
public void mouseDragged(MouseEvent e) {
orgx = lis.orgx;
orgy = lis.orgy;
endx=e.getX();
endy=e.getY();
Graphics2D g = (Graphics2D) lis.app.bimg.getGraphics();
g.setColor(Color.WHITE);
g.clearRect(orgx, orgy-60, endx - orgx, endy - orgy+60);
//g.drawRect(orgx, orgy-60, endx - orgx, endy - orgy+60);
g.fillRect(orgx, orgy-60, endx - orgx, endy - orgy+60);
lis.app.render();
lis.app.render();
}
public void mouseMoved(MouseEvent arg0) {
}
}
class MyImage extends RGBImageFilter
{
int width = 0;
int height = 0;
int alpha = 0;
int start = 0;
int end = 0;
public MyImage() {
this.canFilterIndexColorModel = true;
}
public int filterRGB(int x, int y, int rgb)
{
DirectColorModel dcm = (DirectColorModel)ColorModel.getRGBdefault();
int red = dcm.getRed(rgb);
int green = dcm.getGreen(rgb);
int blue = dcm.getBlue(rgb);
Color newColor = new Color(red, green, blue);
return newColor.getRGB();
}
}
class PointsSet
{
private ArrayList points;
public PointsSet()
{
this.points = new ArrayList();
}
public PointsSet(int initCap) {
this.points = new ArrayList(initCap);
}
public void addPoint(int x, int y) {
int size = this.points.size();
if (size > 0) {
Point point = (Point)this.points.get(size - 1);
if ((point.x == x) && (point.y == y))
return;
}
Point p = new Point();
p.x = x;
p.y = y;
this.points.add(p);
}
public int[][] getPoints() {
int size = this.points.size();
if (size == 0)
return null;
int[][] result = new int[2][size];
for (int i = 0; i < size; ++i) {
Point p = (Point)this.points.get(i);
result[0][i] = p.x;
result[1][i] = p.y;
}
return result;
}
public int[][] getPoints(int x, int y) {
int size = this.points.size();
if (size == 0)
return null;
int[][] result = new int[2][size + 1];
int i=0;
for (; i < size; ++i) {
Point p = (Point)this.points.get(i);
result[0][i] = p.x;
result[1][i] = p.y;
}
result[0][i] = x;
result[1][i] = y;
return result;
}
}
#10
我也没有好办法。
想到一个:把所有画过的区域都存起来,每次重画都把它们都画一遍。
Java code
?
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071……
另外,是先通过菜单栏打开一张图片,然后再对图片编辑,先谢谢了。
#11
你的程序已经可以了,不需要整合我那个了。
就是你有一个地方有问题:
另外,
1.orgx, orgy - 60, endx - orgx, endy - orgy + 60
这个-60 +60是什么意思?不明白。我去掉之后效果是对的
2.如果鼠标从右上往左下画,你的程序会出问题,应该判断初始坐标和当前坐标哪个小
3.如果拖拽很大再拖回去,draw的线框不能消失。这个我还没想到好办法。如果可以的话,建议在图片上面放一个透明的Label,然后功能都实现在这个Label上。
就是你有一个地方有问题:
listener mouseListener = new listener(this);
this.label.addMouseListener(mouseListener);
// this.label.addMouseMotionListener(new motionListener(this));
this.label.addMouseMotionListener(new motionListener(mouseListener));//按照你的代码,这里的mouseListener应该和上面那个一样,不能new一个新的
另外,
1.orgx, orgy - 60, endx - orgx, endy - orgy + 60
这个-60 +60是什么意思?不明白。我去掉之后效果是对的
2.如果鼠标从右上往左下画,你的程序会出问题,应该判断初始坐标和当前坐标哪个小
3.如果拖拽很大再拖回去,draw的线框不能消失。这个我还没想到好办法。如果可以的话,建议在图片上面放一个透明的Label,然后功能都实现在这个Label上。
#12
你的程序已经可以了,不需要整合我那个了。
就是你有一个地方有问题:
Java code
?
1234
listener mouseListener = new listener(this); this.label.addMouseListener(mouseListener); // this.label.addMouseMotionListener(new mo……
我这个实现还是有问题的,如果在鼠标拖拽过程中不在一条直线上就会出现多个矩形,然后就会擦不掉,说白了就是没有清除掉拖动的痕迹。这个要怎么弄呢?有没好办法?
#13
主面板,可以直接调用
Java code
?
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868……
效果图
这个有那么点效果,不过我想要能保留每次话的图,能否请大神在我发的代码基础上帮我改改呢?
#14
你的程序已经可以了,不需要整合我那个了。
就是你有一个地方有问题:
Java code
?
1234
listener mouseListener = new listener(this); this.label.addMouseListener(mouseListener); // this.label.addMouseMotionListener(new mo……
我这个实现还是有问题的,如果在鼠标拖拽过程中不在一条直线上就会出现多个矩形,然后就会擦不掉,说白了就是没有清除掉拖动的痕迹。这个要怎么弄呢?有没好办法?
这个就是我说的第3点。暂时没有想到好办法。
#15
引用 12 楼 zaihushiqu 的回复:
Quote: 引用 11 楼 abc41106 的回复:
你的程序已经可以了,不需要整合我那个了。
就是你有一个地方有问题:
Java code
?
1234
listener mouseListener = new listener(this); this.label.addMouseListener(……
我整合了下你的例子到我的类中,但是我还是保存不了前一次的修改,图片打开的时候好像也要多点一下界面里面,求助,代码贴出来你看看;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FileDialog;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.Stroke;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.DirectColorModel;
import java.awt.image.FilteredImageSource;
import java.awt.image.ImageProducer;
import java.awt.image.RGBImageFilter;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.filechooser.FileFilter;
class ImageTransferFrame2 extends JFrame implements ActionListener {
public static BufferedImage bimg = null;
private static Object source = null;
public static Image theImage;
private JLabel label;
private JMenuItem openItem;
private JScrollPane pane;
private JMenuItem exitItem;
private JButton zoomOut;
private JButton zoomIn;
private JButton rotateLeft;
private JButton rotateRight;
private JButton save;
private JPanel editPane;
public static List<Shape> list = new ArrayList<Shape>();
private JSlider sliderBrightness = null;
public ImageTransferFrame2() {
setSize(800, 500);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
Container contentPane = getContentPane();
this.label = new RectLabel(new ImageIcon());
this.pane = new JScrollPane(this.label);
contentPane.add(this.pane, "Center");
contentPane.add(getEditorPanel(), "North");
JMenu fileMenu = new JMenu("File");
this.openItem = new JMenuItem("Open");
this.openItem.addActionListener(this);
fileMenu.add(this.openItem);
this.exitItem = new JMenuItem("Exit");
this.exitItem.addActionListener(this);
fileMenu.add(this.exitItem);
JMenuBar menuBar = new JMenuBar();
menuBar.add(fileMenu);
setJMenuBar(menuBar);
}
public JPanel getEditorPanel() {
this.editPane = new JPanel();
this.zoomOut = new JButton("放大");
zoomOut.addActionListener(this);
zoomOut.setName("放大");
this.zoomIn = new JButton("缩小");
zoomIn.addActionListener(this);
zoomIn.setName("缩小");
this.rotateLeft = new JButton("右旋转90度");
rotateLeft.addActionListener(this);
rotateLeft.setName("右旋转90度");
this.rotateRight = new JButton("左旋转90度");
rotateRight.addActionListener(this);
rotateRight.setName("左旋转90度");
JLabel lBrightness = new JLabel("亮度调节");
this.sliderBrightness = new JSlider(0);
this.sliderBrightness.setValue(50);
this.sliderBrightness.setPaintLabels(true);
this.sliderBrightness.addChangeListener(new slider_changeListener());
this.save = new JButton("保存");
save.setName("保存");
save.addActionListener(this);
editPane.setLayout(new FlowLayout(0));
editPane.add(save);
editPane.add(zoomOut);
editPane.add(zoomIn);
editPane.add(rotateLeft);
editPane.add(rotateRight);
editPane.add(lBrightness);
editPane.add(this.sliderBrightness);
return editPane;
}
public void actionPerformed(ActionEvent evt) {
this.source = evt.getSource();
if (source == this.openItem) {
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new File("."));
chooser.setFileFilter(new FileFilter() {
public boolean accept(File f) {
String name = f.getName().toLowerCase();
return ((name.endsWith(".gif")) || (name.endsWith(".jpg"))
|| (name.endsWith(".jpeg")) || (f.isDirectory()));
}
public String getDescription() {
return "Image files";
}
});
int r = chooser.showOpenDialog(this);
if (r == 0) {
String name = chooser.getSelectedFile().getAbsolutePath();
File file = new File(name);
try {
BufferedImage im = ImageIO.read(file);
setImage(im);
} catch (IOException e) {
e.printStackTrace();
}
}
} else if (source == this.exitItem) {
System.exit(0);
}
else if(source==this.save)
{
save();
}
else if (source == this.zoomOut)
{// 放大
this.theImage=this.theImage.getScaledInstance((int) (this.bimg.getWidth(this) * 1.2),(int) (this.bimg.getHeight(this) * 1.2), Image.SCALE_SMOOTH);
this.bimg = new BufferedImage(this.theImage.getWidth(this),this.theImage.getHeight(this), 1);
this.bimg.getGraphics().drawImage(this.theImage, 0, 0, this);
setImage(this.bimg);
}
else if (source == this.zoomIn)
{// 缩小
this.theImage=this.theImage.getScaledInstance((int) (this.bimg.getWidth(this) * 0.8),
(int) (this.bimg.getHeight(this) *0.8), Image.SCALE_SMOOTH);
this.bimg = new BufferedImage(this.theImage.getWidth(this),this.theImage.getHeight(this), 1);
this.bimg.getGraphics().drawImage(this.theImage, 0, 0, this);
setImage(this.bimg);
}
else if (source == this.rotateLeft)
{// 左旋转90度
bimg = rotateImg(bimg,90,Color.WHITE);
setImage(this.bimg);
}
else if (source == this.rotateRight)
{// 右旋转90度
bimg = rotateImg(bimg,-90,Color.WHITE);
setImage(this.bimg);
}
}
private void save() {
FileDialog fileDialog = new FileDialog( new Frame() , "请指定一个文件名", FileDialog.SAVE );
fileDialog.show();
if(fileDialog.getFile()==null)
{
return;
}
else{
// 目标文件
try {
BufferedImage inputbig = new BufferedImage(256, 256, BufferedImage.TYPE_INT_BGR);
Graphics2D g = (Graphics2D) inputbig.getGraphics();
g.drawImage(this.bimg, 0, 0,256,256,null); //画图
g.dispose();
inputbig.flush();
ImageIO.write(inputbig, "jpg", new File(fileDialog.getDirectory() + fileDialog.getFile() + ".jpg")); //将其保存在C:/imageSort/targetPIC/下
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
public void resize(){//改變大小
bimg = new BufferedImage(this.bimg.getWidth(this), this.bimg.getHeight(this),BufferedImage.TYPE_3BYTE_BGR);
JLabel jlbImg = new JLabel(new ImageIcon(bimg));//在JLabel上放置bufImg,用來繪圖
this.removeAll();
this.add(jlbImg);
jlbImg.setBounds(new Rectangle(0, 0, this.bimg.getWidth(this), this.bimg.getHeight(this)));
//畫出原本圖形//
Graphics2D g2d_bufImg = (Graphics2D) bimg.getGraphics();
g2d_bufImg.setPaint(Color.white);
g2d_bufImg.fill(new Rectangle2D.Double(0,0,this.bimg.getWidth(this), this.bimg.getHeight(this)));
g2d_bufImg.drawImage(bimg,0,0,this);
}
public static BufferedImage rotateImg( BufferedImage image, int degree, Color bgcolor )
{
int iw = image.getWidth();//原始图象的宽度
int ih = image.getHeight();//原始图象的高度
int w=0;
int h=0;
int x=0;
int y=0;
degree=degree%360;
if(degree<0)
degree=360+degree;//将角度转换到0-360度之间
double ang=degree* 0.0174532925;//将角度转为弧度
if(degree == 180|| degree == 0 || degree == 360)
{
w = iw;
h = ih;
}
else if(degree == 90|| degree == 270)
{
w = ih;
h = iw;
}
else
{
int d=iw+ih;
w=(int)(d*Math.abs(Math.cos(ang)));
h=(int)(d*Math.abs(Math.sin(ang)));
}
x = (w/2)-(iw/2);//确定原点坐标
y = (h/2)-(ih/2);
BufferedImage rotatedImage=new BufferedImage(w,h,image.getType());
Graphics gs=rotatedImage.getGraphics();
gs.setColor(bgcolor);
gs.fillRect(0,0,w,h);//以给定颜色绘制旋转后图片的背景
AffineTransform at=new AffineTransform();//二维变换类
at.rotate(ang,w/2,h/2);//旋转图象
at.translate(x,y); //连接此变换与平移变换。
/*
此类使用仿射转换来执行从源图像或 Raster 中 2D 坐标到目标图像或 Raster 中
2D 坐标的线性映射。所使用的插值类型由构造方法通过一个 RenderingHints 对象或通过此类中定义的整数插值类型之一来指定。
*/
AffineTransformOp op=new AffineTransformOp(at,AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
//转换源 BufferedImage 并将结果存储在目标 BufferedImage 中。
op.filter(image, rotatedImage);
image=rotatedImage;
return image;
}
public void setImage(BufferedImage image) {
this.theImage = image;
ImageProducer picSource = image.getSource();
MyImage bwFilter = new MyImage();
this.theImage = createImage(new FilteredImageSource(picSource, bwFilter));
this.bimg = image;
this.theImage.flush();
//this.label.setIcon(new ImageIcon(this.theImage));
this.label = new RectLabel(new ImageIcon(this.theImage));
}
public static void main(String[] args)
{
JFrame frame1 = new ImageTransferFrame2();
frame1.setLocationRelativeTo(null);//
frame1.setTitle("Frame 1");
frame1.show();
}
}
#16
还有些类:
class slider_changeListener implements ChangeListener {
public void stateChanged(ChangeEvent arg0) {
JSlider slider = (JSlider) arg0.getSource();
}
}
class MyImage extends RGBImageFilter
{
int width = 0;
int height = 0;
int alpha = 0;
int start = 0;
int end = 0;
public MyImage() {
this.canFilterIndexColorModel = true;
}
public int filterRGB(int x, int y, int rgb)
{
DirectColorModel dcm = (DirectColorModel)ColorModel.getRGBdefault();
int red = dcm.getRed(rgb);
int green = dcm.getGreen(rgb);
int blue = dcm.getBlue(rgb);
Color newColor = new Color(red, green, blue);
return newColor.getRGB();
}
}
class PointsSet
{
private ArrayList points;
public PointsSet()
{
this.points = new ArrayList();
}
public PointsSet(int initCap) {
this.points = new ArrayList(initCap);
}
public void addPoint(int x, int y) {
int size = this.points.size();
if (size > 0) {
Point point = (Point)this.points.get(size - 1);
if ((point.x == x) && (point.y == y))
return;
}
Point p = new Point();
p.x = x;
p.y = y;
this.points.add(p);
}
public int[][] getPoints() {
int size = this.points.size();
if (size == 0)
return null;
int[][] result = new int[2][size];
for (int i = 0; i < size; ++i) {
Point p = (Point)this.points.get(i);
result[0][i] = p.x;
result[1][i] = p.y;
}
return result;
}
public int[][] getPoints(int x, int y) {
int size = this.points.size();
if (size == 0)
return null;
int[][] result = new int[2][size + 1];
int i=0;
for (; i < size; ++i) {
Point p = (Point)this.points.get(i);
result[0][i] = p.x;
result[1][i] = p.y;
}
result[0][i] = x;
result[1][i] = y;
return result;
}
}
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Shape;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import java.util.List;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
public class RectLabel extends JLabel {
private int x1 = 0;
private int y1 = 0;
private int x2 = 0;
private int y2 = 0;
private boolean current = true;
private static List<Shape> shapelist = new ArrayList<Shape>();
public static ImageIcon icon = null;
public RectLabel() {
}
public RectLabel(ImageIcon image) {
this.icon = image;
this.setIcon(icon);
repaint();
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent event) {
Point p = event.getPoint();
x1 = (int) p.getX();
y1 = (int) p.getY();
}
public void mouseReleased(MouseEvent event) {
Point p = event.getPoint();
x2 = (int) p.getX();
y2 = (int) p.getY();
shapelist.add(new Rectangle2D.Double(Math.min(x1, x2), Math.min(y1,
y2), Math.abs(x1 - x2), Math.abs(y1 - y2)));
current = true;
repaint();
}
});
addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent event) {
Point p = event.getPoint();
x2 = (int) p.getX();
y2 = (int) p.getY();
current = false;
repaint();
}
});
}
protected void paintComponent(Graphics g) {
if (!current)
return;
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.WHITE);
for (Shape s : shapelist) {
if(this.icon!=null)
{
g2.drawImage(this.icon.getImage(), 0, 0, 800, 560, this);
}
g2.fill(s);
repaint();
}
}
protected void paintBorder(Graphics g) {
if (current)
return;
Graphics2D g2 = (Graphics2D) g;
g.setColor(getForeground());
if(this.icon!=null)
{
g2.drawImage(this.icon.getImage(), 0, 0, 800, 560, this);
}
g.drawRect(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1 - x2), Math
.abs(y1 - y2));
repaint();
}
}
#17
参照上面保存每次都记录的思想,做一点小改动就可以了,在鼠标释放之后保存记录
代码如下
代码如下
package xuxian;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Shape;
import java.awt.Stroke;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JPanel;
public class DragDao extends JPanel implements MouseMotionListener,
MouseListener {
/** 矩形的起点 左上角* */
private static Point rectStart = null;
/** 矩形的终点 右下角* */
private static Point rectStop = null;
/** 是否绘制虚线矩形* */
private boolean dottedTag = true;
/*************这里更改了****************/
private List<Shape> list = new ArrayList<Shape>();
public DragDao() {
this.setBackground(Color.WHITE);
this.setSize(1000, 1000);
rectStart = new Point(0, 0);
rectStop = new Point(0, 0);
this.addMouseListener(this);
this.addMouseMotionListener(this);
// this.isClear = isClear;
}
@Override
protected void paintComponent(Graphics g) {
// TODO Auto-generated method stub
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
/** 矩形的宽度* */
int w = Math.abs(rectStop.x - rectStart.x);
/** 矩形的高度* */
int h = Math.abs(rectStop.y - rectStart.y);
/** 起点终点的最小值作为起点* */
Point min = new Point(0, 0);
min.x = Math.min(rectStop.x, rectStart.x);
min.y = Math.min(rectStop.y, rectStart.y);
/** 起点终点的最小值作为终点* */
Point max = new Point(0, 0);
max.x = Math.max(rectStop.x, rectStart.x);
max.y = Math.max(rectStop.y, rectStart.y);
/** 如果是绘制虚线矩形* */
if ( dottedTag) {
/** new float[] { 5, 5, } 数组可以改变虚线的密度* */
Stroke dash = new BasicStroke(0.5f, BasicStroke.CAP_BUTT,
BasicStroke.JOIN_ROUND, 0.5f, new float[] { 5, 5, }, 0f);
g2.setStroke(dash);
g2.setColor(Color.BLUE);
g2.drawRect(min.x, min.y, w, h);
} else {
list.add(new Rectangle2D.Double(min.x, min.y, w, h));
}
/*************这里更改了****************/
g2.setStroke(new BasicStroke());
g2.setColor(new Color(198, 214, 239));
for(Shape s:list){
g2.fill(s);
}
}
/** *鼠标拖动 */
public void mouseDragged(MouseEvent arg0) {
// TODO Auto-generated method stub
/** 随着鼠标的拖动 改变终点* */
rectStop = arg0.getPoint();
this.repaint();
}
public void mouseMoved(MouseEvent arg0) {
// TODO Auto-generated method stub
}
/** 鼠标按键在组件上单击(按下并释放)时调用* */
public void mouseClicked(MouseEvent arg0) {
// TODO Auto-generated method stub
}
/** 鼠标按键在组件上按下时调用 */
public void mousePressed(MouseEvent arg0) {
/** 设置可以进行绘制* */
dottedTag = true;
/** 记录起始点* */
rectStart = arg0.getPoint();
/** 记录起终点* */
rectStop = rectStart;
}
/** 鼠标按钮在组件上释放时调用* */
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
dottedTag = false;
this.repaint();
}
/** 鼠标进入到组件上时调用* */
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
/** 鼠标离开组件时调用* */
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
}
#18
引用 14 楼 abc41106 的回复:引用 12 楼 zaihushiqu 的回复:
Quote: 引用 11 楼 abc41106 的回复:
你的程序已经可以了,不需要整合我那个了。
就是你有一个地方有问题:
Java code
?
1234
listener mouseListener = new listener(t……
你要是整合我那个代码的话,那你的代码改动可就大了。。放大、缩小、保存,都要改。还不如重写一个了。。。
我只能把我的这部分给你改好。剩下的,只能你自己改了。。。
class RectLabel extends JLabel {
private int x1 = 0;
private int y1 = 0;
private int x2 = 0;
private int y2 = 0;
private boolean current = true;
private static List<Shape> shapelist = new ArrayList<Shape>();
public static ImageIcon icon = null;
public RectLabel() {
}
public RectLabel(ImageIcon image) {
this.icon = image;
this.setIcon(icon);
//repaint();
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent event) {
Point p = event.getPoint();
x1 = (int) p.getX();
y1 = (int) p.getY();
}
public void mouseReleased(MouseEvent event) {
Point p = event.getPoint();
x2 = (int) p.getX();
y2 = (int) p.getY();
shapelist.add(new Rectangle2D.Double(Math.min(x1, x2), Math
.min(y1, y2), Math.abs(x1 - x2), Math.abs(y1 - y2)));
current = true;
repaint();
}
});
addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent event) {
Point p = event.getPoint();
x2 = (int) p.getX();
y2 = (int) p.getY();
current = false;
repaint();
}
});
}
protected void paintComponent(Graphics g) {
if (!current)
return;
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.WHITE);
if (this.icon != null) {//这个要放到循环外面
g2.drawImage(this.icon.getImage(), 0, 0, 800, 560, this);
}
for (Shape s : shapelist) {
g2.fill(s);
// repaint();
}
}
protected void paintBorder(Graphics g) {
if (current)
return;
Graphics2D g2 = (Graphics2D) g;
g.setColor(getForeground());
if (this.icon != null) {
g2.drawImage(this.icon.getImage(), 0, 0, 800, 560, this);
}
for (Shape s : shapelist) {//这个循环按你的需求,想加就加,不加就去掉。
g2.fill(s);
}
g.drawRect(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1 - x2),
Math.abs(y1 - y2));
// repaint();
}
}