⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 painter.java

📁 用JAVA编的画图编辑器的代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
//package fairysun.java;
import java.awt.*;        //Windowing events
import java.awt.event.*;  // Event-Handling - ActionEvent
import java.io.*;         //Input/Output
import javax.swing.*;     //User Interface component Libary
import java.lang.*;
import java.util.*;

import java.awt.*;
import java.applet.Applet;
import java.awt.image.*;
import java.awt.geom.AffineTransform;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.event.WindowAdapter;
import javax.imageio.ImageIO;

import java.io.*;
import java.util.Hashtable;
import java.net.*;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.filechooser.FileFilter;
import javax.imageio.ImageIO;



class painter extends JFrame{
private BufferedImage bi;	

private JPanel showP;
	
//the menu bar
protected JMenuBar menuBar;
	
//the menus
protected JMenu file;
protected JMenu edit;
protected JMenu help;

//the menu items
protected JMenuItem thenew;
protected JMenuItem open;
protected JMenuItem save;
protected JMenuItem saveAs;
protected JMenuItem exit;
protected JMenuItem resize;
protected JMenuItem rotate;
protected JMenuItem aboutP;
protected JMenuItem teamInfo;

protected JRadioButton	A90;
protected JRadioButton 	A180;
protected JRadioButton 	A270;

protected JButton ok;
protected JButton cancel;
protected JButton Sok;
protected JButton Scancel;

private String DEFAULT_FILENAME="new";
private String filename = DEFAULT_FILENAME; // Current model file name
private String frameTitle;
private File modelFile;

public Double angle;
private Image image;
public String path;
private Double sizex;
private Double sizey;

private JTextField fl;
private JTextField vtc;
private JLabel label;
//private JLabel la;

private boolean changed=false;
private Frame transformW;
private Frame transform;

private File outFile;
private String savefilename = "picture";
private JFileChooser files;
File DEFAULT_DIRECTORY = new File("C:/pictures");

public painter(){
	super("My painter");
	GUIconstructor();	
	}
public void GUIconstructor(){
	
	menuBar=new JMenuBar();
	showP=new JPanel();
	this.setLayout(new GridLayout());
	label=new JLabel();
	add(label,BorderLayout.CENTER);
	
		
	file=new JMenu("file");
	edit=new JMenu("edit");
	help=new JMenu("help");
	
	thenew=new JMenuItem("new");
	open=new JMenuItem("open");
	save=new JMenuItem("save");
	saveAs=new JMenuItem("save as");
	exit=new JMenuItem("exit");
	resize=new JMenuItem("resize");
	rotate=new JMenuItem("rotate");
	aboutP=new JMenuItem("about the painter");
	teamInfo=new JMenuItem("team information");
	
	setJMenuBar(menuBar);
		
	//Add accelerators
	file.setMnemonic('F');
	edit.setMnemonic('E');
	help.setMnemonic('H');
	
	thenew.setAccelerator(KeyStroke.getKeyStroke('N',Event.CTRL_MASK));
	open.setAccelerator(KeyStroke.getKeyStroke('O',Event.CTRL_MASK));
	save.setAccelerator(KeyStroke.getKeyStroke('S',Event.CTRL_MASK));
	saveAs.setAccelerator(KeyStroke.getKeyStroke('A',Event.CTRL_MASK));
	exit.setAccelerator(KeyStroke.getKeyStroke('E',Event.CTRL_MASK));
	resize.setAccelerator(KeyStroke.getKeyStroke('S',Event.CTRL_MASK));
	rotate.setAccelerator(KeyStroke.getKeyStroke('R',Event.CTRL_MASK));
	aboutP.setAccelerator(KeyStroke.getKeyStroke('P',Event.CTRL_MASK));
	teamInfo.setAccelerator(KeyStroke.getKeyStroke('T',Event.CTRL_MASK));
	
	//Add menu to menu bar
	menuBar.add(file);
	menuBar.add(edit);
	menuBar.add(help);
	
	//Add menu items to menu
	file.add(thenew);
	file.add(open);
	file.add(save);
	file.add(saveAs);
	file.add(exit);
	
	edit.add(resize);
	edit.add(rotate);
	
	//addMenuItem(edit, lineAction = new TypeAction("Line", LINE, "Draw lines")); 
	
	
	help.add(aboutP);
	help.add(teamInfo);
	
	if(!DEFAULT_DIRECTORY.exists())
      if(!DEFAULT_DIRECTORY.mkdirs())
        JOptionPane.showMessageDialog(this,
                                      "Error creating default directory",
                                      "Directory Creation Error",
                                      JOptionPane.ERROR_MESSAGE);
	files = new JFileChooser(DEFAULT_DIRECTORY);
	
	addEventHendler();
	
	}	
	

public void addEventHendler(){
		Toolkit theKit = this.getToolkit();
		Dimension wndSize = theKit.getScreenSize();
		this.setBounds(wndSize.width/4, wndSize.height/4,        // Position
                     wndSize.width/2, wndSize.height/2);
        this.setVisible(true);
			
   		// Window Code to kill the window at the top-right of the screen
   		this.addWindowListener(new WindowAdapter(){
			public void windowClosing( WindowEvent e){
				if(e.getID()==WindowEvent.WINDOW_CLOSING){
											checkForSave();
					  						dispose();
					  						System.exit(0);  
			      	  					}   
					  
			    	}
		    	});
		    	
thenew.addActionListener(new ActionListener(){
	public void actionPerformed(ActionEvent e){
		checkForSave();
		label.setIcon(null);
		changed=false;
	}
});
			
open.addActionListener(new ActionListener(){
	  public void actionPerformed(ActionEvent e) {
      JFileChooser c = new JFileChooser();
        c.setMultiSelectionEnabled(true);
        c.setFileFilter(new FileFilter(){
        		public String getDescription(){
                         return "*.JPG, *.GIF";
                     }
                public boolean accept(File f){
                         String name;
                         if (f.isDirectory()) return true;
                         name=f.getName().toLowerCase();
                         if (name.endsWith(".jpg")||name.endsWith(".jpeg")||name.endsWith(".gif")) 
                             return true;
                         else
                             return false;
                     }
        	});
        	
        int rVal = c.showOpenDialog(painter.this);
        if(rVal!=JFileChooser.APPROVE_OPTION) return;
        File[] f=c.getSelectedFiles();
        for (int i=0;i<f.length;i++){
                     path=f[i].getAbsolutePath();
                     System.out.print(path);
     
     	try {
         	FileInputStream in=new FileInputStream(path);
			      Image img=ImageIO.read(in);
			      label.setIcon(new ImageIcon(img));
			      setImage(img);
			      /*Sharpen sharpen=new Sharpen(getImage());
			      			WindowListener l = new WindowAdapter() {
	    public void windowClosing(WindowEvent e) {System.exit(0);}
	};
	Frame fr = new Frame("Show");
	fr.addWindowListener(l);
	fr.add("Center", Show);
	fr.pack();
	fr.setSize(new Dimension(600, 300));
	fr.show();*/
			      
			      
			      
			      in.close();
    		} catch(Exception exc) {
      				System.out.println(exc);
      		}
       	}
       }
	});
	
save.addActionListener(new ActionListener(){
	public void actionPerformed(ActionEvent e){
				
	}
});
			
saveAs.addActionListener(new ActionListener(){
	public void actionPerformed(ActionEvent e){
		JFileChooser c = new JFileChooser();
		c.setMultiSelectionEnabled(true);
        c.setFileFilter(new FileFilter(){
        		public String getDescription(){
                         return "*.JPG, *.GIF";
                     }
                public boolean accept(File f){
                         String name;
                         if (f.isDirectory()) return true;
                         name=f.getName().toLowerCase();
                         if (name.endsWith(".jpg")||name.endsWith(".jpeg")||name.endsWith(".gif")) 
                             return true;
                         else
                             return false;
                     }
        	});
        int rVal = c.showSaveDialog(painter.this);
        
	}
});

exit.addActionListener(new ActionListener(){
	public void actionPerformed(ActionEvent e){
		checkForSave();
		System.exit(0);
	}
});

resize.addActionListener(new ActionListener(){
		public void actionPerformed(ActionEvent e){
				SWindow wid=new SWindow(null);
				wid.show();
			}
	});
			
rotate.addActionListener(new ActionListener(){
		public void actionPerformed(ActionEvent e){
			RWindow wid=new RWindow(null);
			wid.show();
			}
	});
	
teamInfo.addActionListener(new ActionListener(){
		public void actionPerformed(ActionEvent e){
			HTWindow wid=new HTWindow(null);
			wid.show();
			}
	});
}

public void checkForSave() {
    if(changed)
      if(JOptionPane.YES_OPTION ==
            JOptionPane.showConfirmDialog(painter.this,
                             "Current file has changed. Save current file?",
                             "Confirm Save Current File",
                             JOptionPane.YES_NO_OPTION,
                             JOptionPane.WARNING_MESSAGE))
        System.out.println("successful");
        saveOperation();
  }

// Save the sketch if it is necessary
  private void saveOperation() {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -