1. Learn to use the PixelGrabber class;
2. Due to carelessness in a For loop, it has been added more ";", it has not found a program error for a long time.
Import java.awt. *; import java.awt.Event. *; import javax.swing. *; import java.awt.Image. *; import java.io. *; import java.awt.image. *; import java .awt. *;
public class graphicsTest extends JFrame {JPanel contentPane; BorderLayout borderLayout1 = new BorderLayout (); MenuBar menuBar1 = new MenuBar (); Menu menu1 = new Menu (); MenuItem menuItem1 = new MenuItem (); Menu menu2 = new Menu (); MenuItem menuItem2 = new MenuItem (); JLabel lb = new JLabel (); JPanel pl = new JPanel (); MenuItem menuItem3 = new MenuItem (); String strSource = null; String dir = null; MenuItem menuItem4 = new MenuItem (); public Graphicstest () {enableevents (awtevent.window_event_mask); try {jbinit ();} catch (exception e) {E.PrintStackTrace ();}}
Private void Jbinit () throws exception {contentpane = (jPanel) this.getContentPane (); contentpane.setLayout (BorderLayout1); this.settsize (New Dimension (400, 300)); this.SetTitle ("frame title"); Menu1 .setLabel ( "File"); menuItem1.setLabel ( "open"); menuItem1.addActionListener (new Frame1_menuItem1_actionAdapter (this)); menu2.setLabel ( "Edit"); menuItem2.setLabel ( "split"); menuItem2.addActionListener ( new Frame1_menuItem2_actionAdapter (this)); menuItem3.setLabel ( "change"); menuItem3.addActionListener (new Frame1_menuItem3_actionAdapter (this)); menuItem4.setLabel ( "pixels"); menuItem4.addActionListener (new Frame1_menuItem4_actionAdapter (this)); menuBar1.add (menu1); menubar1.add (menu2); menu1.add (menuitem1); menu2.add (Menuitem2); menu2.add (Menuitem3); menu2.add (Menuitem4); setmenubar (menubar1); ContentPane.Add (PL, BorderLayout.center; Pl.Add (lb);} protected void processwindowevent (windowevent e) {super.processWindowEvent ); If (E.GetId () == windowevent.window_closing) {system.exit (0);}}
Void Menuitem1_ActionPerformed (ActionEvent E) {
FileDialog fd = new filedialog (this, "open"); fd.show (); if (fd.getfile ()! = Null) {strsource = fd.getfile (); DIR = fd.getdirectory ();} ImageICON SOURCE = New Imageicon (Dir strsource); lb.seticon (Source); lb.repaint ();
void menuItem4_actionPerformed (ActionEvent e) {ImageIcon source = new ImageIcon (dir strSource); int height = source.getIconHeight (); int width = source.getIconWidth (); Image im = source.getImage (); int height1 = im. GetHeight (NULL); int width1 = im. Getwidth (null); // int i; int [] content = new int [width * height]; PixelGrabber pg = null; try {pg = new PixelGrabber (IM, 0, 0 , Width, Height, Content, 0, Width; IF (pg.grabpixels ()! = true) Try {throw new java.awt.awtexception ("pg error" pg.status ());} catch (Exception EQ ) {Eq.PrintStackTrace ();}} catch (exception ex) {EX.PrintStackTrace (); system.out.print ("xxxx");} try {system.out.print (content.length); system.out .print * width; System.out.print (Height1 * width1); System.Out.Print (Integer.toHexString (Content [479999])); //system.out.println (i); // for (INT i = 0; i <480000; i ); for (int i = 0; i
24) | (Red << 16) | (Green << 8) | Blue;} Image Pic = CreateImage (New MemoryImagesource (Width, Height, Content, 0, Width); ImageICON IC = New Imageicon (PIC); LB .Seticon (IC); icon icon = lb.geticon (); lb.imageUpdate (pic, 0,0,0, width, height); if (icon! = null) {(imageicon) .getImage () .flush ();} lb.repaint ();} catch (Exception EE) {EE.PRINTSTACKTRACE ();}} public static void main (String args []) {new graphicstest (). Show ();}}
Class frame1_menuitem1_actionadapter import.Ava.awt.event.ActionListener {graphicstest adaptee
Frame1_menuItem1_actionAdapter (graphicsTest adaptee) {this.adaptee = adaptee;} public void actionPerformed (ActionEvent e) {adaptee.menuItem1_actionPerformed (e);}} class Frame1_menuItem4_actionAdapter implements java.awt.event.ActionListener {graphicsTest adaptee;
Frame1_Menuitem4_ActionAdapter (graphicstest adaptee) {this.adaptee = adaptee;} public void actionPerformed (ActionEvent E) {adaptee.Menuitem4_ActionPerformed (e);}}