java语言写代码: 要求:1.可以在界面显示图片 2.可以将图片通过加号减号进行放大缩小功能 100
2个回答
展开全部
按照你的要求编写的可缩放图片的Java程序如下:
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
public class LoadFromAppli extends JFrame implements KeyListener{
private static final long serialVersionUID = 1L;
ImageIcon im;
static int imgHight=0;
static int imgWidth=0;
public LoadFromAppli(){
im=new ImageIcon("C:\\tmp\\1\\src\\Penguins.jpg");//这里把路径换成你的图片的路径
addKeyListener(this);
imgWidth=im.getIconWidth();
imgHight=im.getIconHeight();
setSize(imgWidth, imgHight);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}
public void paint(Graphics g){
g.drawImage(im.getImage(), 0,0, imgWidth,imgHight,this);
}
public static void main(String[] args) {
new LoadFromAppli();
}
@Override
public void keyPressed(KeyEvent ke) {
}
@Override
public void keyReleased(KeyEvent ke) {
}
@Override
public void keyTyped(KeyEvent ke) {
if(ke.getKeyChar()=='+'){
imgWidth=imgWidth+10;
imgHight=imgHight+10;
repaint();
this.setSize(imgWidth, imgHight);
}
if(ke.getKeyChar()=='-'){
imgWidth=imgWidth-10;
imgHight=imgHight-10;
repaint();
this.setSize(imgWidth, imgHight);
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询