如题
解决方案 »
- System.exist(0)问题
- Exception in thread "main" java.lang.NoClassDefFoundError: com/adventnet/afp/log/AbstractLogger
- 想学J2EE入门,看什么书好
- 请教:匿名类!
- 一个关于"this();"用法调用另外一个构造方法的困惑!
- 谁知道哪里可以找到《张孝祥java培训课程》的光盘啊
- 指定字符是否在文件中存在,怎么实现,(在线等待)
- 请教关于环境变量长度的问题
- 关于编译java文件加密问题?
- 使用Quartz,如何动态的增加Job(运行时添加新的Job)
- 虚心请教!
- hibernate hql 加减乘除 计算
(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事件,根据鼠标所在位置决定是哪一点的坐标如何变化。四个角需要单独照顾到。逻辑上不复杂,实现起来代码稍微有点繁琐,主要是判断鼠标所在的角,和鼠标移动时图片的尺寸和坐标应该怎么设置。
左上:(0,0)--(5,5)
右上:(width,0)--(width-5,5)
左下:(0,height)--(5,height-5)
右下:(width,height)--(width-5,height-5)
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);
}
}