Swing/PaintPanel のバックアップ(No.9)
- バックアップ一覧
- 差分 を表示
- 現在との差分 を表示
- 現在との差分 - Visual を表示
- ソース を表示
- Swing/PaintPanel へ行く。
- 1 (2007-03-13 (火) 20:47:32)
- 2 (2007-04-14 (土) 23:40:59)
- 3 (2008-02-01 (金) 18:25:07)
- 4 (2010-01-12 (火) 16:16:59)
- 5 (2010-01-12 (火) 17:24:11)
- 6 (2010-01-13 (水) 22:59:41)
- 7 (2010-03-08 (月) 12:34:33)
- 8 (2010-03-08 (月) 13:43:19)
- 9 (2010-04-30 (金) 18:11:55)
- 10 (2010-04-30 (金) 19:26:20)
- 11 (2010-06-07 (月) 15:22:31)
- 12 (2011-04-04 (月) 20:32:20)
- 13 (2013-03-21 (木) 15:49:34)
- 14 (2014-07-04 (金) 15:23:29)
- 15 (2014-11-06 (木) 01:07:18)
- 16 (2015-12-08 (火) 15:35:18)
- 17 (2016-01-16 (土) 01:50:26)
- 18 (2016-09-07 (水) 13:59:55)
- 19 (2016-11-18 (金) 14:38:26)
- 20 (2017-11-25 (土) 17:42:01)
- 21 (2019-06-27 (木) 16:43:37)
- 22 (2021-03-10 (水) 14:36:12)
TITLE:JPanelにマウスで自由曲線を描画
Posted by terai at 2005-12-19
JPanelにマウスで自由曲線を描画
マウスをドラッグしてパネル上に自由曲線を描画します。
- &jnlp;
- &jar;
- &zip;
#screenshot
サンプルコード
class PaintPanel extends JPanel implements MouseMotionListener, MouseListener {
private Point startPoint = new Point(-1,-1);
private Point endPoint = new Point(-1,-1);
public PaintPanel() {
super();
addMouseMotionListener(this);
addMouseListener(this);
}
public void paintComponent(Graphics g) {
//super.paintComponent(g);
Graphics2D g2d = (Graphics2D)g;
g2d.setStroke(new BasicStroke(3.0F));
g2d.setPaint(Color.BLACK);
g2d.drawLine(startPoint.x, startPoint.y,
endPoint.x, endPoint.y);
startPoint = endPoint;
}
public void mouseDragged(MouseEvent e) {
endPoint = e.getPoint();
repaint();
}
public void mousePressed(MouseEvent e) {
startPoint = e.getPoint();
}
public void mouseMoved(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseClicked(MouseEvent e) {}
}
解説
上記のサンプルでは、パネル上でマウスがドラッグされている場合、その軌跡を短い直線でつなぎ合わせて描画することで、お絵かきしています。
- マウスがクリックされた場所を始点にする
- ドラッグされた時の位置を終点にしてパネルをrepaint
- paintComponentをオーバーライドして、上記の始点、終点で直線を描画
- 次の直線のための始点を現在の終点に変更
コメント
- マウス右ボタンをドラッグで消しゴム…のテスト -- terai
import java.awt.*; import java.awt.event.*; import java.awt.geom.*; import java.awt.image.*; import javax.swing.*; class PaintPanel2 extends JPanel implements MouseMotionListener, MouseListener { public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); } public static void createAndShowGUI() { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); frame.getContentPane().add(new PaintPanel2()); frame.setSize(320, 240); frame.setLocationRelativeTo(null); frame.setVisible(true); } private final Polygon polygon = new Polygon(); private Point startPoint = new Point(-1,-1); private BufferedImage offImage = null; private BufferedImage backImage = null; private TexturePaint texture = makeTexturePaint(); private int[] pix = new int[320 * 240]; public PaintPanel2() { super(); addMouseMotionListener(this); addMouseListener(this); offImage = new BufferedImage(320, 240, BufferedImage.TYPE_4BYTE_ABGR); PixelGrabber pg = new PixelGrabber(offImage, 0, 0, 320, 240, pix, 0, 320); try { pg.grabPixels(); } catch (Exception e) { e.printStackTrace(); } backImage = new BufferedImage(320, 240, BufferedImage.TYPE_4BYTE_ABGR); Graphics2D g2 = backImage.createGraphics(); g2.setPaint(texture); g2.fillRect(0,0,320,240); g2.dispose(); } private static BufferedImage makeBGImage() { Color color = new Color(200,150,100,50); int cs = 6, sz = cs*cs; BufferedImage img = new BufferedImage(sz,sz,BufferedImage.TYPE_4BYTE_ABGR); Graphics2D g2 = img.createGraphics(); g2.setPaint(color); g2.fillRect(0,0,sz,sz); for (int i=0; i*cs<sz; i++) { for (int j=0; j*cs<sz; j++) { if ((i+j)%2==0) g2.fillRect(i*cs, j*cs, cs, cs); } } g2.dispose(); return img; } private static TexturePaint makeTexturePaint() { BufferedImage img = makeBGImage(); int w = img.getWidth(), h = img.getHeight(); Rectangle2D r2d = new Rectangle2D.Float(0,0,w,h); return new TexturePaint(img, r2d); } @Override public void paintComponent(Graphics g) { super.paintComponent(g); if (backImage!=null) { ((Graphics2D)g).drawImage(backImage, 0, 0, this); } if (offImage!=null) { MemoryImageSource producer = new MemoryImageSource(320, 240, pix, 0, 320); g.drawImage(createImage(producer), 0, 0, null); g.dispose(); } } int penc = 0x0; public void mouseDragged(MouseEvent e) { Point pt = e.getPoint(); double xDelta = e.getX() - startPoint.getX(); double yDelta = e.getY() - startPoint.getY(); double delta = Math.max(Math.abs(xDelta), Math.abs(yDelta)); double xIncrement = xDelta / delta; double yIncrement = yDelta / delta; double xStart = startPoint.x; double yStart = startPoint.y; for (int i=0; i<delta; i++) { Point p = new Point((int)xStart, (int)yStart); if (p.x<0 || p.y<0 || p.x>=320 || p.y>=240) break; pix[p.x + p.y * 320] = penc; for(int n=-1;n<=1;n++) { for(int m=-1;m<=1;m++) { int t = (p.x+n) + (p.y+m) * 320; if(t>=0 && t<320*240) { pix[t] = penc; } } } repaint(p.x-2, p.y-2, 4, 4); xStart += xIncrement; yStart += yIncrement; } startPoint = pt; } private Rectangle getRepaintRectangle(Point srcPoint, Point destPoint) { polygon.reset(); polygon.addPoint(srcPoint.x, srcPoint.y); polygon.addPoint(destPoint.x, srcPoint.y); polygon.addPoint(destPoint.x, destPoint.y); polygon.addPoint(srcPoint.x, destPoint.y); return polygon.getBounds(); } public void mousePressed(MouseEvent e) { startPoint = e.getPoint(); penc = (e.getButton()==MouseEvent.BUTTON1)?0xff000000:0x0; } public void mouseMoved(MouseEvent e) {} public void mouseExited(MouseEvent e) {} public void mouseEntered(MouseEvent e) {} public void mouseReleased(MouseEvent e) {} public void mouseClicked(MouseEvent e) {} }