• 追加された行はこの色です。
  • 削除された行はこの色です。
TITLE:DoubleBufferingで自由曲線を描画
//TITLE:DoubleBufferingで自由曲線を描画
#navi(../)
*DoubleBufferingで自由曲線を描画 [#bc55cccd]
>編集者:[[Terai Atsuhiro>terai]]~
作成日:2005-12-19~
更新日:&lastmod;

#contents
Swingのコンポーネントでは自動的にDouble Bufferingが行われるため、意味のないサンプルになっていました。
- 参考
-- [[Painting in AWT and Swing>http://java.sun.com/products/jfc/tsc/articles/painting/]]
-- [[se-ji-cafe翻訳日記 - AwtとSwingにおける描画処理>http://d.hatena.ne.jp/se-ji-cafe/20070306/1173231679]]

**概要 [#mba2e720]
チラつかないようにDouble Bufferingを行ってパネルにお絵かきします。
以前ここに在ったサンプルは修正して、[[JPanelにマウスで自由曲線を描画>Swing/PaintPanel]]に移動しました。

#screenshot
//*DoubleBufferingで自由曲線を描画 [#bc55cccd]
//>編集者:[[Terai Atsuhiro>terai]]~
//作成日:2005-12-19~
//更新日:&lastmod;
//
//#contents
//
//**概要 [#mba2e720]
//チラつかないようにDouble Bufferingを行ってパネルにお絵かきします。
//
//#screenshot
//
//**サンプルコード [#cc955806]
// panel = new JPanel() {
//   public void paintComponent(Graphics g) {
//     super.paintComponent(g);
//     if(offImage!=null) ((Graphics2D)g).drawImage(offImage, 0, 0, this);
//   }
// };
// panel.setBorder(BorderFactory.createEmptyBorder());
// panel.addMouseMotionListener(new MouseMotionAdapter() {
//   public void mouseDragged(MouseEvent e) {
//     Point p = e.getPoint();
//     if(offImage==null) {
//       offImage = (BufferedImage)createImage(getWidth(), getHeight());
//     }
//     Graphics2D g2d = (Graphics2D)offImage.createGraphics();
//     g2d.setStroke(new BasicStroke(3.0F));
//     g2d.setPaint(Color.black);
//     g2d.drawLine(startPoint.x, startPoint.y, p.x, p.y);
//     g2d.dispose();
//     repaint();
//     startPoint = e.getPoint();
//   }
// });
// panel.addMouseListener(new MouseAdapter() {
//   public void mousePressed(MouseEvent e) {
//     startPoint = e.getPoint();
//   }
// });
//
//-&jnlp;
//-&jar;
//-&zip;
//
//**解説 [#t35e18f2]
//上記のサンプルでは、マウスリスナーを使ってパネル上にお絵かきをしています。
//
//画面がチラつかないようにするためによく使われるダブルバッファリングの手法を使っています。
//
//**参考リンク [#b81f9b54]
//-[[Painting in AWT and Swing>http://java.sun.com/products/jfc/tsc/articles/painting/#swing]]
//
//**コメント [#p0458ab1]
//- Swingでは自動的にダブルバッファリングしてくれるはずなので、このTipは実は意味がありません。AWTのPanelなどの場合のテクニックなので、そのうちページを消すかもしれません。 -- [[terai]] &new{2006-10-18 (水) 17:54:12};
//
//#comment

**サンプルコード [#cc955806]
 panel = new JPanel() {
   public void paintComponent(Graphics g) {
     super.paintComponent(g);
     if(offImage!=null) ((Graphics2D)g).drawImage(offImage, 0, 0, this);
   }
 };
 panel.setBorder(BorderFactory.createEmptyBorder());
 panel.addMouseMotionListener(new MouseMotionAdapter() {
   public void mouseDragged(MouseEvent e) {
     Point p = e.getPoint();
     if(offImage==null) {
       offImage = (BufferedImage)createImage(getWidth(), getHeight());
     }
     Graphics2D g2d = (Graphics2D)offImage.createGraphics();
     g2d.setStroke(new BasicStroke(3.0F));
     g2d.setPaint(Color.black);
     g2d.drawLine(startPoint.x, startPoint.y, p.x, p.y);
     g2d.dispose();
     repaint();
     startPoint = e.getPoint();
   }
 });
 panel.addMouseListener(new MouseAdapter() {
   public void mousePressed(MouseEvent e) {
     startPoint = e.getPoint();
   }
 });

-&jnlp;
-&jar;
-&zip;

**解説 [#t35e18f2]
上記のサンプルでは、マウスリスナーを使ってパネル上にお絵かきをしています。

画面がチラつかないようにするためによく使われるダブルバッファリングの手法を使っています。

**参考リンク [#b81f9b54]
-[[Painting in AWT and Swing>http://java.sun.com/products/jfc/tsc/articles/painting/#swing]]

**コメント [#p0458ab1]
- Swingでは自動的にダブルバッファリングしてくれるはずなので、このTipは実は意味がありません。AWTのPanelなどの場合のテクニックなので、そのうちページを消すかもしれません。 -- [[terai]] &new{2006-10-18 (水) 17:54:12};

#comment