编写一个具有图形用户界面的Java程序
编写一个具有图形用户界面的Java程序,Applet或Application均可,完成下述功能:让一个图片缩小成原来的一半...
编写一个具有图形用户界面的Java程序,Applet或Application均可,完成下述功能:让一个图片缩小成原来的一半
展开
1个回答
展开全部
import javax.imageio.*;
import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
import java.io.*;
/**
* @author Hardneedl
*/
class ImageZoom extends JFrame {
private static final Dimension minSize = new Dimension(300, 200);
private static final Dimension maxSize = new Dimension(1024, 768);
private static final Dimension preferredSize = new Dimension(600, 400);
public Dimension getMaximumSize() {return maxSize;}
public Dimension getMinimumSize() {return minSize;}
public Dimension getPreferredSize() {return preferredSize;}
public String getTitle() {return "Frame Title";}
private class ImageCanvas extends JComponent{
private Image img;
private AffineTransform af=new AffineTransform();
private ImageCanvas(Image g,float s) {setImage(g);setScale(s);}
private void setImage(Image img){
this.img = img;
if (isVisible()) paintImmediately(getBounds());
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (img!=null) {
Graphics2D g2d = (Graphics2D)g.create();
g2d.drawImage(img,af,this);
}
}
private void setScale(float scale){
af = AffineTransform.getScaleInstance(scale,scale);
if (isVisible()) paintImmediately(getBounds());
}
};
private static ImageCanvas canvas;
private Image img;private float s;
ImageZoom(Image g,float s) throws HeadlessException {
img =g;this.s=s;
init();
doLay();
attachListeners();
}
private void init() {
canvas = new ImageCanvas(img,s);
}
private void doLay() {
Container container = getContentPane();
container.add(canvas,BorderLayout.CENTER);
pack();
}
private void attachListeners() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(final String[] args) {
SwingUtilities.invokeLater(
new Runnable(){
public void run() {
try {
new ImageZoom(ImageIO.read(new FileInputStream(args[0])),Float.parseFloat(args[1])).setVisible(true);
} catch (IOException e) {
e.printStackTrace();
}
}
}
);
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
import java.io.*;
/**
* @author Hardneedl
*/
class ImageZoom extends JFrame {
private static final Dimension minSize = new Dimension(300, 200);
private static final Dimension maxSize = new Dimension(1024, 768);
private static final Dimension preferredSize = new Dimension(600, 400);
public Dimension getMaximumSize() {return maxSize;}
public Dimension getMinimumSize() {return minSize;}
public Dimension getPreferredSize() {return preferredSize;}
public String getTitle() {return "Frame Title";}
private class ImageCanvas extends JComponent{
private Image img;
private AffineTransform af=new AffineTransform();
private ImageCanvas(Image g,float s) {setImage(g);setScale(s);}
private void setImage(Image img){
this.img = img;
if (isVisible()) paintImmediately(getBounds());
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (img!=null) {
Graphics2D g2d = (Graphics2D)g.create();
g2d.drawImage(img,af,this);
}
}
private void setScale(float scale){
af = AffineTransform.getScaleInstance(scale,scale);
if (isVisible()) paintImmediately(getBounds());
}
};
private static ImageCanvas canvas;
private Image img;private float s;
ImageZoom(Image g,float s) throws HeadlessException {
img =g;this.s=s;
init();
doLay();
attachListeners();
}
private void init() {
canvas = new ImageCanvas(img,s);
}
private void doLay() {
Container container = getContentPane();
container.add(canvas,BorderLayout.CENTER);
pack();
}
private void attachListeners() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(final String[] args) {
SwingUtilities.invokeLater(
new Runnable(){
public void run() {
try {
new ImageZoom(ImageIO.read(new FileInputStream(args[0])),Float.parseFloat(args[1])).setVisible(true);
} catch (IOException e) {
e.printStackTrace();
}
}
}
);
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询