新手求教,怎么打开电脑里的图片和保存图像…需要用到什么知识
代码如下:
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);
}
}