import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
class game1003 extends Frame implements ActionListener {
PixelGrabber pg;
Button button1,button2;
Image img,img2;
int w=20, h=20;
boolean button1push = false;//画像読み込み[[ボタン]]
boolean button2push = false;//処理開始ボタン
int pixels[]=[[new]] int[w*h];//ピクセルデータ(入力)配列
int pixels2[]=new int[w*h];//色変更したピクセル配列
game1003() {
setLayout(new FlowLayout(FlowLayout.LEFT));
img = createImage(w,h);//画像読み込み用 Image を生成
button1 = new Button("画像読込");
add(button1);
button1.addActionListener(this);
button2 = new Button("変色コピー");
add(button2);
button2.addActionListener(this);
}
public void paint(Graphics g) {
if(button1push)g.drawImage(img,20,[[100]],this);
if(button2push)g.drawImage(img2,100,100,this);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == button1){
button1push = true;
button2push=false;
img = Toolkit.getDefaultToolkit().getImage("harada.jpg");
pg = new PixelGrabber(img,0,0,w,h,pixels,0,w);
try{
pg.grabPixels();
}catch(InterruptedException ie){}
repaint();
}
//ボタン2が押された時の処理(ピクセルごとの処理)
if(e.getSource() == button2){
button2push = true;
button1push=false;
//単に読出して書戻し(やらなくとも同じ)
for(int i=0;i<w*h;i++){
int p = pixels[i];
int red = 0xff & (p>>16);
int green = 0xff & (p>>8);
int blue = 0xff & p;
pixels2[i] = (0xff000000 | red << 8 | green << 16 | blue);
}
img2 = createImage(new MemoryImageSource(w, h, pixels2, 0, w));
repaint();
}
}
public static void main(String[] args) {
game1003 f = new game1003();
f.setSize(300,300);
f.addWindowListener(new WindowAdapter(){public void
windowClosing(WindowEvent e){System.exit(0);}});
f.setVisible(true);
}
}
最終更新:2011年03月06日 00:42