お絵かきコラボレーション
戻る
そのうちにサーバを介してネットワーク対応版を作ってみようかな。。。
::::::::::::::
ClientFrame.java
::::::::::::::
import java.awt.*;
import java.awt.event.*;
import java.awt.dnd.*;
import java.awt.geom.*;
import javax.swing.event.*;
import javax.swing.*;
import javax.swing.border.*;
/**
* $Id: Collaboration.html,v 1.1 2009/06/22 16:11:40 kishi Exp kishi $
* @author KISHI Yasuhiro
*/
public class ClientFrame extends JFrame {
private DrawingPanel panel = null;
public ClientFrame ( int x, int y, String userName ) {
super( "お絵かきコラボレーションのテスト(" + userName + ")" );
try {
// 外観を設定します
UIManager.setLookAndFeel( "com.sun.java.swing.plaf.windows.WindowsLookAndFeel" );
// 外観を変更します
SwingUtilities.updateComponentTreeUI( this );
} catch ( Exception e ) {
e.printStackTrace();
System.exit( -1 );
}
setBounds( x, y, 640, 480 );
panel = new DrawingPanel();
Container container = getContentPane();
container.add( panel );
setVisible( true );
addWindowListener( new WindowAdapter() {
public void windowClosing( WindowEvent ev ) {
dispose();
System.exit( 0 );
}
}
);
}
public DrawingPanel getDrawingPanel() {
return this.panel;
}
public void setCollaborator( ClientFrame frame ) {
DrawingPanel cPanel = frame.getDrawingPanel();
panel.setCollaboratorPanel( cPanel );
}
private static void createAndShowGUI() {
ClientFrame frame1 = new ClientFrame( 0, 0, "ユーザA" );
ClientFrame frame2 = new ClientFrame( 0, 500, "ユーザB" );
frame1.setCollaborator( frame2 );
frame2.setCollaborator( frame1 );
}
public static void main( String[] args ) {
javax.swing.SwingUtilities.invokeLater( new Runnable() {
public void run() {
createAndShowGUI();
}
}
);
}
}
::::::::::::::
DrawingPanel.java
::::::::::::::
import java.awt.*;
import java.awt.event.*;
import java.awt.dnd.*;
import java.awt.geom.*;
import javax.swing.event.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.imageio.*;
import java.awt.image.*;
import java.io.*;
/**
* $Id: Collaboration.html,v 1.1 2009/06/22 16:11:40 kishi Exp kishi $
* @author KISHI Yasuhiro
*/
public class DrawingPanel extends JPanel implements ActionListener {
private BufferedImage offImage = null;
private Point currentPoint;
private DrawingPanel cPanel;
public DrawingPanel() {
super();
setLayout( null );
// 透明にする
setOpaque( false );
JButton saveButton = new JButton( "画像を保存" );
saveButton.setLocation( 500, 350 );
saveButton.setSize( 100, 50 );
saveButton.setActionCommand( "SAVE" );
saveButton.addActionListener( this );
this.add( saveButton );
this.addMouseMotionListener( new MouseMotionAdapter() {
public void mouseDragged( MouseEvent e ) {
// マウスの位置をセット
Point p = e.getPoint();
if ( offImage == null ) {
// ここは注意が必要 ⇒ BufferedImage.TYPE_INT_ARGB
// BufferedImage.TYPE_INT_RGBを使うと下のレイヤーの画像が塗り消されてしまいます
// アルファチャネルも追加しておく
offImage = new BufferedImage( getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB );
}
Graphics2D g2d = ( Graphics2D ) offImage.createGraphics();
g2d.setStroke( new BasicStroke( 3.0F ) );
g2d.setPaint( Color.black );
g2d.drawLine( currentPoint.x, currentPoint.y, p.x, p.y );
g2d.dispose();
repaint();
// コラボレータのオフ画像を取得
BufferedImage cOffImage = cPanel.getOffImage();
if ( cOffImage == null ) {
// ここは注意が必要 ⇒ BufferedImage.TYPE_INT_ARGB
// BufferedImage.TYPE_INT_RGBを使うと下のレイヤーの画像が塗り消されてしまいます
// アルファチャネルも追加しておく
cOffImage = new BufferedImage( getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB );
}
Graphics2D g2d_other = ( Graphics2D ) cOffImage.createGraphics();
g2d_other.setStroke( new BasicStroke( 3.0F ) );
g2d_other.setPaint( Color.red );
g2d_other.drawLine( currentPoint.x, currentPoint.y, p.x, p.y );
g2d_other.dispose();
cPanel.repaint();
// マウスの位置をセット
currentPoint = e.getPoint();
}
}
);
this.addMouseListener( new MouseAdapter() {
public void mousePressed( MouseEvent e ) {
currentPoint = e.getPoint();
}
}
);
}
public void paintComponent( Graphics g ) {
super.paintComponent( g );
if ( offImage != null ) {
( ( Graphics2D ) g ).drawImage( offImage, 0, 0, this );
}
}
public void setCollaboratorPanel( DrawingPanel cPanel ) {
this.cPanel = cPanel;
}
public BufferedImage getOffImage() {
return this.offImage;
}
public void actionPerformed( ActionEvent event ) {
String command = event.getActionCommand();
if ( "SAVE".equals( command ) ) {
System.out.println( "画像をファイルに保存しています..." );
try {
boolean result = ImageIO.write( offImage, "png", new File( "mytest" + this.toString() + ".png" ) );
System.out.println( "画像をファイルに保存しました!" );
} catch ( Exception e ) {
e.printStackTrace();
}
}
}
}
戻る