怎么才可以选中JTextPane中插入的ImageIcon,可以拉伸改变其大小?

Chuyun130 2009-09-25 11:05:15
如题
...全文
172 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
SWAINY 2010-11-20
  • 打赏
  • 举报
回复
学习ing
stanley3319 2009-10-24
  • 打赏
  • 举报
回复
高招ㄚ 收下了~~THX
amdgaming 2009-09-25
  • 打赏
  • 举报
回复
学习 ing
windforcecn 2009-09-25
  • 打赏
  • 举报
回复

package testGUI;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JFrame;

public class TestImage extends JFrame {

private static final long serialVersionUID = 1L;

private static final String filename = "C:/1.jpg";

private volatile BufferedImage image = null;

public TestImage() {
init();
}

private void init() {
this.getContentPane().setLayout(new BorderLayout());
this.setSize(new Dimension(800, 600));
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.addComponentListener(new MyAdapter(this));
this.setVisible(true);
}

public BufferedImage getImage(boolean reload) {
if (image == null || reload) {
try {
image = ImageIO.read(new File(filename));
} catch (IOException e) {
e.printStackTrace();
}
}
return image;
}

public void resize(int height, int width, boolean bb) {
try {
double ratio = 0.0;
File f = new File(filename);
BufferedImage bi = getImage(false);
Image itemp = getImage(false).getScaledInstance(width, height,
BufferedImage.SCALE_SMOOTH);
if ((bi.getHeight() > height) || (bi.getWidth() > width)) {
if (bi.getHeight() > bi.getWidth()) {
ratio = (new Integer(height)).doubleValue()
/ bi.getHeight();
} else {
ratio = (new Integer(width)).doubleValue() / bi.getWidth();
}
AffineTransformOp op = new AffineTransformOp(AffineTransform
.getScaleInstance(ratio, ratio), null);
itemp = op.filter(bi, null);
}
if (bb) {
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics2D g = image.createGraphics();
g.setColor(Color.white);
g.fillRect(0, 0, width, height);
if (width == itemp.getWidth(null))
g.drawImage(itemp, 0, (height - itemp.getHeight(null)) / 2,
itemp.getWidth(null), itemp.getHeight(null),
Color.white, null);
else
g.drawImage(itemp, (width - itemp.getWidth(null)) / 2, 0,
itemp.getWidth(null), itemp.getHeight(null),
Color.white, null);
g.dispose();
itemp = image;
}
ImageIO.write((BufferedImage) itemp, "jpg", f);
TestImage.this.getGraphics().drawImage(getImage(true), this.getX(),
this.getY(), TestImage.this);
} catch (Exception e) {
e.printStackTrace();
}
}

public void paint(Graphics g) {
super.paint(g);
g.drawImage(getImage(false), this.getX(), this.getY(), this);
}

/**
* @param args
*/
public static void main(String[] args) {
new TestImage();
}
}

class MyAdapter extends ComponentAdapter {
TestImage adaptee;

MyAdapter(TestImage adaptee) {
this.adaptee = adaptee;
}

public void componentResized(ComponentEvent e) {
adaptee.resize(adaptee.getHeight(), adaptee.getWidth(), true);
}
}

daisycool 2009-09-25
  • 打赏
  • 举报
回复
不好意思,上面判断四角的坐标可能不对,更正一下
左上:(0,0)--(5,5)
右上:(width,0)--(width-5,5)
左下:(0,height)--(5,height-5)
右下:(width,height)--(width-5,height-5)
daisycool 2009-09-25
  • 打赏
  • 举报
回复
这个有点复杂。因为Component并没有默认的拉伸事件监听。给你提供个思路吧。

建一个继承ImageIcon的类,比如说叫MyImage,里面监听MouseMotionEvent.mouseMoved事件,判断鼠标的坐标,比如说是
(0, 0)--(5, 5)这个范围的话,鼠标光标变成NW箭头,
(width-5, 0)--(width, 5)的话变成NE箭头
(0, height-5)--(5, height)的话变成SW箭头
(width-5, height-5)--(width, height)的话变成SE箭头

这些是鼠标在四角位置的判断。

在调用这个MyImage类的时候,为其添加MouseEvent,监听其MouseDragged事件,根据鼠标所在位置决定是哪一点的坐标如何变化。四个角需要单独照顾到。

逻辑上不复杂,实现起来代码稍微有点繁琐,主要是判断鼠标所在的角,和鼠标移动时图片的尺寸和坐标应该怎么设置。
amdgaming 2009-09-25
  • 打赏
  • 举报
回复
不太会 。。。
Chuyun130 2009-09-25
  • 打赏
  • 举报
回复
再顶一下

62,621

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧