import Javax.swing.*;
import java.awt.Graphics;
import java.awt.*;
import java.awt.event.*;
public class ImageViewer extends JFrame{
Image imageFirst;//初始图像
ImagePanel panel=new ImagePanel();//用来显示图像的panel
JMenuBar jmb=new JMenuBar();
JMenu file=new JMenu("File");
JMenu modify=new JMenu("Modify");
JMenuItem open=new JMenuItem("open");
JMenuItem exit=new JMenuItem("exit");
ButtonGroup bg=new ButtonGroup();
JRadioButtonMenuItem rate1=new JRadioButtonMenuItem("to 50%");//这三个菜单项
JRadioButtonMenuItem rate2=new JRadioButtonMenuItem("to 100%");//用来改变图像
JRadioButtonMenuItem rate3=new JRadioButtonMenuItem("to 200%");//显示的大小
JToolBar toolbar=new JToolBar();//只有一个button简单的toolbar
JButton button=new JButton("A JToolBar Contain A Jbutton");
ImageViewer(){
this.getContentPane().setLayout(null);
this.setBounds(200,200,500,400);
this.setResizable(false);
this.setTitle("ImageViewer");
imageFirst=java.awt.Toolkit.getDefaultToolkit().getImage("images/opean.gif");
panel.setImage(imageFirst);
jmb.add(file);
jmb.add(modify);
open.addActionListener(new OpenAction());
file.add(open);
file.add(exit);
bg.add(rate1); bg.add(rate2); bg.add(rate3);
modify.add(rate1);
modify.add(rate2); rate2.setSelected(true);
modify.add(rate3);
rate1.addItemListener(new ItemListener(){
public void itemStateChanged(ItemEvent e){
if(rate1.isSelected()==true){
panel.setRate(0.5);
System.out.println("rate1 been selected!");
panel.repaint();
}
}
});
rate2.addItemListener(new ItemListener(){
public void itemStateChanged(ItemEvent e){
if(rate2.isSelected()==true){
panel.setRate(1);
System.out.println("rate2 been selected!");
panel.repaint();
}
}
});
rate3.addItemListener(new ItemListener(){
public void itemStateChanged(ItemEvent e){
if(rate3.isSelected()==true){
panel.setRate(2);
System.out.println("rate3 been selected!");
panel.repaint();
}
}
});
exit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.exit(0);
}});
this.setJMenuBar(jmb);
panel.setBounds(0,30,this.getWidth(),this.getHeight()-60);
this.getContentPane().add(panel);
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
JOptionPane.showMessageDialog(null,this);
}
});
toolbar.add(button);
toolbar.setLayout(new GridLayout());
toolbar.setBackground(Color.blue);
toolbar.setBounds(0,0,500,30);
this.getContentPane().add(toolbar);
}//SwingDemo1
//主方法
public static void main(String argv[]){
JFrame.setDefaultLookAndFeelDecorated(true);
ImageViewer app=new ImageViewer();
app.setVisible(true);
app.repaint();
}
//内部类菜单项opean处理事件
class OpenAction implements ActionListener{
public void actionPerformed(ActionEvent e){
JFileChooser fc = new JFileChooser();
int returnVal = fc.showDialog(new ImageViewer(), "Open file");
String file = fc.getSelectedFile().getPath();
panel.setImage(file);
panel.repaint();
System.out.println("open file action performed");
}
}
}
//外部类Panel用来显示image
class ImagePanel extends JPanel
{
private Image image;
private double rate=1;
Dimension offDimension;
Image offImage;
Graphics offGraphics;
ImagePanel(){
this.setBackground(Color.blue);
}
public void update(Graphics g){
if(image!=null){
g.drawImage(image,0,0,
(int)(this.getWidth()*rate/2),(int)(this.getHeight()*rate/2),
this.getRootPane());
}
else
{g.setColor(Color.red);
g.fillRect(0,0,(int)(250*rate),(int)(200*rate));
}
}
public void paint(Graphics g){
update(g);
}
public void setImage(Image image){
this.image=image;
}
public void setRate(double rate){
this.rate=rate;
}
public void setImage(String s){
this.image=java.awt.Toolkit.getDefaultToolkit().getImage(s);
}
}