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

📄 painter.java

📁 用JAVA编的画图编辑器的代码
💻 JAVA
字号:
//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 boolean Rok;

public String path;

public File f;

private JLabel label;

//private BufferedImage biabc;
//protected JPanel textPnl;
//private int elementType = DEFAULT_ELEMENT_TYPE;  
//private TypeAction lineAction, rectangleAction, circleAction, curveAction, textAction;   
//private SketchView view;  
   

JTextField 
    theFilename = new JTextField(6),
    dir = new JTextField(6);
	    
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);
	
	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){   
				System.exit(0);	  
			    	}
		    	}		  
			);
			
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);
     
      //path=c.getCurrentDirectory().toString()+"\\"+c.getSelectedFile().getName();
      //System.out.println(path);
      //f=new File(path);
	
      try {
      /*ObjectInputStreamFileInputStream in = new FileInputStream(f);/*new ObjectInputStream(new BufferedInputStream(
                                       new FileInputStream(f)));*/
      
      //System.out.println(path);
         FileInputStream in=new FileInputStream(path);
			      Image img=ImageIO.read(in);
			      label.setIcon(new ImageIcon(img));
			      setImage(img);
			      in.close();
    		} catch(Exception exc) {
      				System.out.println(exc);
      		}
       	}
       }
	});
			
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 int getElementType() { 
  //  return elementType; 
 // }

/*class TypeAction extends AbstractAction {    
    TypeAction(String name, int typeID) {
      super(name);
      this.typeID = typeID;
      String iconFileName = "Images/" + name + ".gif";
      if(new File(iconFileName).exists())
        putValue(SMALL_ICON, new ImageIcon(iconFileName));
    }
    
    TypeAction(String name, int typeID, String tooltip) {
      this(name, typeID);
      if(tooltip != null)                               // If there is a tooltip
        putValue(SHORT_DESCRIPTION, tooltip);           // ...squirrel it away
    }

    public void actionPerformed(ActionEvent e) {
      elementType = typeID;  
      //statusBar.setTypePane(typeID);
    }

    private int typeID;
  }
  
 private JMenuItem addMenuItem(JMenu menu, Action action) {
    JMenuItem item = menu.add(action);                  // Add the menu item

    KeyStroke keystroke = (KeyStroke)action.getValue(action.ACCELERATOR_KEY);
    if(keystroke != null)
      item.setAccelerator(keystroke);
      
    // remove this comments to disable icons
    // item.setIcon(null);                                 // Remove the icon
    return item;                                        // Return the menu item
  }*/
	

public class Draw extends JPanel{
		public void paintComponent(Graphics g){
			super.paintComponent(g);
		//	g.setColor(Color.black);
		//	g.drawLine(2,30,50,80);	
			}
		
	}



class RWindow extends JDialog{
	public RWindow(JFrame frame){
		super(frame, "rotate", true);
	    Container cp = getContentPane();
	    ButtonGroup g = new ButtonGroup();
	    JRadioButton 
	    	flow = new JRadioButton("flow turn", false),
	    	vertical = new JRadioButton("vertical turn", false),
	    	choose = new JRadioButton("choose an angle turn ", false);
	    g.add(flow);
	    g.add(vertical);
	    g.add(choose);
	    
	    ButtonGroup angle = new ButtonGroup();
	        A90= new JRadioButton("90 degree              ", false);
	    	A180 = new JRadioButton("180 degree              ", false);
	    	A270 = new JRadioButton("270 degree              ", false);
	    angle.add(A90);
	    angle.add(A180);
	    angle.add(A270);
	    
	    cp.setLayout(new FlowLayout(FlowLayout.LEADING));
	    cp.add(flow); 
	    cp.add(vertical); 
	    cp.add(choose);
	    
	    cp.add(A90);
	    cp.add(A180);
	    cp.add(A270);
	    A90.setEnabled(false);
	    A180.setEnabled(false);
	    A270.setEnabled(false); 
	    
	    ok=new JButton("OK");
	    cancel=new JButton("Cancel");
	    
		cancel.addActionListener(new ActionListener(){
					public void actionPerformed(ActionEvent e){
							setVisible(false);
						}
				});	    
	    
	    cp.add(ok);
	    cp.add(cancel);
	    
	    setSize(180,300);
	    
	flow.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
					A90.setEnabled(false);
					A180.setEnabled(false);
					A270.setEnabled(false);
				}
		});   
		
	vertical.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
					A90.setEnabled(false);
					A180.setEnabled(false);
					A270.setEnabled(false);
				}
		}); 
	    
	choose.addActionListener(new ActionListener(){
	  	public void actionPerformed(ActionEvent e){
	    	A90.setEnabled(true);
	    	A180.setEnabled(true);
	    	A270.setEnabled(true);
	    	}
	    });
	    
	A90.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				setAngle(1.57);
				
					
				}
		});
	    
	ok.addActionListener(new ActionListener(){
		public void actionPerformed(ActionEvent e){
				Blur blur=new Blur(getImage(),getAngle());
					WindowListener l = new WindowAdapter() {
	    public void windowClosing(WindowEvent e) {System.exit(0);}
	};
	Frame f = new Frame("Blur");
	f.addWindowListener(l);
	f.add("Center", blur);
	f.pack();
	f.setSize(new Dimension(600, 300));
	f.show();
				
				
				
			}
		});
	
	cancel.addActionListener(new ActionListener(){
		public void actionPerformed(ActionEvent e){
			
			}
		});
	   
	   }
	}

class SWindow extends JDialog{
		public SWindow(JFrame frame){
				super(frame,"resize",true);
				Container cp=getContentPane();
				JLabel f=new JLabel("flow:     ");
				JLabel v=new JLabel("vertical: ");
				
				JTextField fl=new JTextField(6);
				JTextField vtc=new JTextField(6);
				
				JLabel p=new JLabel(" %                ");
				JLabel p1=new JLabel(" %                ");
				
				Sok=new JButton("OK");
				Scancel=new JButton("Cancel");
				
				cp.setLayout(new FlowLayout(FlowLayout.CENTER));
				
				Scancel.addActionListener(new ActionListener(){
					public void actionPerformed(ActionEvent e){
							setVisible(false);
						}
				});
				/*public void actionPerformed(ActionEvent e){
						Object source = e.getSource(); 
						if(source==Scancel)
						setVisible(false);
					}*/
				
				cp.add(f);
				cp.add(fl);
				cp.add(p);
				
				cp.add(v);
				cp.add(vtc);
				cp.add(p1);
				
				cp.add(Sok);
				cp.add(Scancel);
				
				setSize(210,150);
			}
	}
	
class HTWindow extends JDialog{
		public HTWindow(JFrame frame){
				super(frame,"team information",true);
				Container cp=getContentPane();
				JButton ok=new JButton("OK");
				JTextArea text=new JTextArea("Sunjingwan 03080203"+"\nChukongna 03080207"+"\nGenglu 03080214",3,3);
				cp.setLayout(new FlowLayout(FlowLayout.CENTER));
				cp.add(text);
				cp.add(ok);
				ok.addActionListener(new ActionListener(){
						public void actionPerformed(ActionEvent e){
								setVisible(false);
							}
					});
				setSize(150,150);
				
			}
	}
	
public void setAngle(Double dou){
		angle=dou;
	}
public Double getAngle(){
		return angle;
	}
public void setImage(Image ima){
	image=ima;
}
public Image getImage(){
	return image;
}

public static void main(String[] args){
		painter paint=new painter();

		
	}



}

⌨️ 快捷键说明

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