1,学会了使用PixelGrabber类;
2,由于粗心在一个for循环后面多加了一个“;”,导致很久没找到程序错误。
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.setSize(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(e);
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(height*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<height*width;i++)
{
//System.out.print(i);
int red=(int)(((content[i]>>16)&0xff)*0.3);
int green = (int)(((content[i] >> 8) & 0xff)*0.4);
int blue = (int)(((content[i]) & 0xff)*0.11);
blue = blue + 20;
if (blue > 255)
blue = 255;
/*System.out.println ("i="+i);
System.out.println ("content["+i+"]"+Integer.toHexString(content[i]));
System.out.println ("green"+Integer.toHexString(green));
System.out.println ("blue"+Integer.toHexString(blue));*/
content[i] = (255<< 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)icon).getImage().flush();
}
lb.repaint();
}catch(Exception ee)
{ee.printStackTrace();}
}
public static void main(String args[])
{
new graphicsTest().show();
}
}
class Frame1_menuItem1_actionAdapter implements java.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);
}
}