FC2ブログ

Javaグラフィック その2

今回は、描画領域の指定、背景、リサイズ時の描画位置の移動、
ショートカットキーCTRL+Z、CTRL+Yによる戻る、進む機能
を実装。
描画処理をイベントドリブン型へ変更

これをJFrameにセットコンテントペイン

package d2_paint;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.InputEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.awt.image.Raster;
import java.util.LinkedList;

import javax.swing.JPanel;


public class D2Draw extends JPanel implements Runnable {
private MouseEventAction mea;
private ResizeEventAction rea;
private KeyEventAction kea;
private BufferedImage bi;
private Point start;
private Point end;
private LinkedList<Raster> pxl_buffer;
private LinkedList<Raster> pxl_buck;
private int inputButton = InputEvent.BUTTON1_DOWN_MASK;
private boolean buck_flg=false;
private boolean mouse1_flg=false;
private boolean move_flg=false;
private int xx,yy;
private Dimension dim_paint;

public D2Draw(Dimension paint_s){
super();
mea = new MouseEventAction();
rea = new ResizeEventAction();
kea = new KeyEventAction();
start = new Point();
end = new Point();
xx=(this.getWidth()-paint_s.width)>>1;
yy=(this.getHeight()-paint_s.height)>>1;
pxl_buffer =new LinkedList<Raster>();
pxl_buck =new LinkedList<Raster>();
bi=null;
dim_paint=paint_s;
this.setOpaque(false);
this.setDoubleBuffered(true);
this.addMouseListener(mea);
this.addMouseMotionListener(mea);
this.addComponentListener(rea);
this.addKeyListener(kea);
this.setVisible(true);
}

public class ResizeEventAction extends ComponentAdapter{
public void componentResized(ComponentEvent e) {
xx=(D2Draw.this.getWidth()-dim_paint.width)>>1;
yy=(D2Draw.this.getHeight()-dim_paint.height)>>1;
}
}

public class KeyEventAction extends KeyAdapter {
public void keyPressed(KeyEvent e){
boolean ctrl_flg=false;
switch(e.getModifiersEx()){
case InputEvent.CTRL_DOWN_MASK :
ctrl_flg=true;
break;
default : break;
}
switch(e.getKeyCode()){
case KeyEvent.VK_Z :
if(ctrl_flg){
if(pxl_buck.isEmpty()){
pxl_buck.push(bi.getData());
}
if(!pxl_buffer.isEmpty() && pxl_buffer.size()<=63){
Raster rst = pxl_buffer.pop();
bi.setData(rst);
pxl_buck.push(rst);
}
buck_flg=true;
}
break;
case KeyEvent.VK_Y :
if(ctrl_flg){
if(pxl_buck.size()>=2){
Raster rst = pxl_buck.pop();
pxl_buffer.push(rst);
bi.setData(pxl_buck.peek());
}
buck_flg=true;
}
break;
default : break;
}
D2Draw.this.repaint();

}
}

public class MouseEventAction extends MouseAdapter {

public void mousePressed(MouseEvent e) {
if((e.getModifiersEx()&(inputButton))==inputButton){
start=e.getPoint();
end=e.getPoint();
mouse1_flg=true;
new Thread(D2Draw.this).start();
}
}

public void mouseDragged(MouseEvent e) {
if((e.getModifiersEx()&(inputButton))==inputButton){
start=end;
end=e.getPoint();
}
}

public void mouseReleased(MouseEvent e) {
if(mouse1_flg){
move_flg=true;
mouse1_flg=false;
}
}
}

public void update(Graphics g){
this.paint(g);
}

public void paint(Graphics g){
Graphics2D gbi=(Graphics2D)g;
this.requestFocusInWindow(true);
if(bi==null){
bi=initImage();
gbi=bi.createGraphics();
gbi.setColor(Color.cyan);
gbi.fillRect(0, 0, dim_paint.width,dim_paint.height);
}
if(buck_flg){
gbi=bi.createGraphics();
buck_flg=false;
}
if(mouse1_flg){
gbi=bi.createGraphics();
gbi.setColor(Color.BLACK);
gbi.drawLine(start.x-xx, start.y-yy,end.x-xx,end.y-yy);
}
g.drawImage(bi, xx, yy,this);
}

public BufferedImage initImage(){
return ((BufferedImage)this.createImage(dim_paint.width,dim_paint.height));
}


public void run() {
pxl_buffer.push(bi.getData());
pxl_buck.clear();
if(pxl_buffer.size()==64)
pxl_buffer.removeLast();
while(!move_flg){
this.repaint();
}
move_flg=false;
}
}


スポンサーサイト



テーマ : プログラミング
ジャンル : コンピュータ

コメントの投稿

非公開コメント

ぶろぐかんりしゃ

SmartWoods
最近MoEは・・・
一休み

***** ひとこと *****

MoEの後継ともいわれる
Resonance gamez
完全スキルMMOが
気になるところ



********************


↓2016/3/26更新
My MoE









**********

NEWとらっくばっく
あーかいぶ
かてごりー
リンク
ぶろぐないけんさく
RSSふぃーど
おともだちになろ

この人とブロともになる