To save the pixel matrix for the current operation
private int currentpixarray[] = null;
The path of the image
Private String filestring = null;
Label used to display the image
Private JLabel Imagelabel = null;
Loaded images
Private BufferedImage newimage;
The height and width of the image
private int h,w;
Save History operation Image Matrix
private LinkedList < int [] > imagestack = new LinkedList < int [] > ();
private LinkedList < int [] > tempimagestack = new LinkedList < int [] > ();
Public Myshowimage (String title) {
Super (title);
this. SetSize (800, 600);
this. Setdefaultcloseoperation (Jframe.exit_on_close);
Create a Menu
JMenuBar JB = new JMenuBar ();
JMenu filemenu = new JMenu ("file");
Jb.add (Filemenu);
JMenuItem Openimagemenuitem = new JMenuItem ("open image");
Filemenu.add (Openimagemenuitem);
Openimagemenuitem.addactionlistener (New Openlistener ());
JMenuItem exitmenu = new JMenuItem ("Exit");
Filemenu.add (Exitmenu);
Exitmenu.addactionlistener (new ActionListener () {
public void actionperformed (ActionEvent e) {
System.exit (0);
}
});
JMenu operatemenu = new JMenu ("image processing");
Jb.add (Operatemenu);
JMenuItem Rgbtograymenuitem = new JMenuItem ("Grayscale image conversion");
Operatemenu.add (Rgbtograymenuitem);
Rgbtograymenuitem.addactionlistener (New Rgbtograyactionlistener ());
JMenuItem Balancemenuitem = new JMenuItem ("balanced");
Operatemenu.add (Balancemenuitem);
Balancemenuitem.addactionlistener (New Balanceactionlistener ());
JMenu frontandbackmenu = new JMenu ("Historical operation");
Jb.add (Frontandbackmenu);
JMenuItem Backmenuitem = new JMenuItem ("Back");
Frontandbackmenu.add (Backmenuitem);
Backmenuitem.addactionlistener (New Backactionlistener ());
JMenuItem Frontmenuitem = new JMenuItem ("forward");
Frontandbackmenu.add (Frontmenuitem);
Frontmenuitem.addactionlistener (New Frontactionlistener ());
this. Setjmenubar (JB);
Imagelabel = new JLabel ("");
JScrollPane pane = new JScrollPane (Imagelabel);
this. Add (Pane,borderlayout.center);
this. setvisible (true);
}
Private class Openlistener implements actionlistener{
public void actionperformed (ActionEvent e) {
JFileChooser JC = new JFileChooser ();
int returnvalue = Jc.showopendialog (null);
if (returnvalue = = jfilechooser.approve_option) {
File selectedfile = Jc.getselectedfile ();
if (selectedfile!= null) {
filestring = Selectedfile.getabsolutepath ();
try {
NewImage = Imageio.read (new File (filestring));
W = newimage.getwidth ();
h = newimage.getheight ();
Currentpixarray = Getpixarray (newimage,w,h);
Imagestack.clear ();
Tempimagestack.clear ();
Imagestack.addlast (Currentpixarray);
Imagelabel.seticon (New ImageIcon (newimage));
Private class Backactionlistener implements actionlistener{
public void actionperformed (ActionEvent e) {
if (Imagestack.size () <= 1) {
Joptionpane.showmessagedialog (NULL, "The processing of this picture has no back history operation", "hint",
Joptionpane.information_message);
} else {
Tempimagestack.addlast (Imagestack.removelast ());
Currentpixarray = Imagestack.getlast ();
ShowImage (Currentpixarray);
}
}
}
Private class Frontactionlistener implements actionlistener{
public void actionperformed (ActionEvent e) {
if (Tempimagestack.size () < 1) {
Joptionpane.showmessagedialog (NULL, "The processing of this picture has no advance history operation", "hint",
Joptionpane.information_message);
} else {
Currentpixarray = Tempimagestack.removefirst ();
Imagestack.addlast (Currentpixarray);
ShowImage (Currentpixarray);
}
}
}
Get Image pixel matrix/////////
private int []getpixarray (Image im, int w, int h) {
int [] pix = new int [w * h];
PixelGrabber pg = NULL;
try {
pg = new PixelGrabber (IM, 0, 0, W, h, Pix, 0, W);
if (Pg.grabpixels ()!= true)
try {
throw new Java.awt.AWTException ("PG Error" + pg.status ());
catch (Exception eq) {
Eq.printstacktrace ();
}
catch (Exception ex) {
Ex.printstacktrace ();
}
return pix;
}
Show Picture///////////
private void ShowImage (int [] srcpixarray) {
Image pic = createimage (new MemoryImageSource (W,h,srcpixarray, 0
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.