新手求教,怎么打开电脑里的图片和保存图像…需要用到什么知识

maoplay 2012-10-11 02:06:35
代码如下:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.*;
import java.io.File;

public class Pic extends JFrame implements ActionListener
{
private JButton
b1 = new JButton("打开"),
b2 = new JButton("button2"),
b3 = new JButton("button3"),
b4 = new JButton("button4"),
b6 = new JButton("保存"),
b5 = new JButton("button5");
private Panel panel1 = new Panel();
private JPanel panel2 = new JPanel();

public Pic()
{
this.setTitle("图像处理");
this.setSize(900, 700);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

panel1.img = Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/Desert.jpg"));
panel2.add(b1);
panel2.add(b2);
panel2.add(b3);
panel2.add(b4);
panel2.add(b5);
panel2.add(b6);
panel1.repaint();
panel1.setBackground(Color.darkGray);
panel2.setBackground(Color.CYAN);
this.add(panel1,BorderLayout.CENTER);
this.add(panel2,BorderLayout.SOUTH);

}

public static void main(String[] args)
{
Pic pic = new Pic();
}


public void actionPerformed(ActionEvent arg0) {


}
}

class Panel extends JPanel
{
Image img = null;
int height,width;
public void paint(Graphics g)
{
height = img.getHeight(null);
width = img.getWidth(null);
if(height > 600&&width > 900)
{
height = height/2;
width = width/2;
}
super.paint(g);
g.drawImage(img,(this.getWidth()-width)/2,(this.getHeight()-height)/2,width,height,this);
System.out.print("width="+width+" height="+height);
}
}
...全文
71 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

62,616

社区成员

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

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