Swingでグラフィック!
戻る
<?xml version="1.0"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta name="generator" content=
"HTML Tidy for Cygwin (vers 1st September 2004), see www.w3.org" />
<title>Hello World Applet</title>
</head>
<body>
<applet code="DoubleBufferingTest" width="600" height="600"></applet>
</body>
</html>
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/**
<pre>
Swingでは勝手にダブルバッファリングしてくれるので便利!
</pre>
$Id: SwingGraphics.html,v 1.1 2009/06/22 16:11:58 kishi Exp kishi $
@author KISHI Yasuhiro
*/
public class DoubleBufferingTest extends JApplet implements ItemListener {
public void init() {
Container contentPane = getContentPane();
contentPane.setLayout( new FlowLayout() );
GlobalPainter gp = new GlobalPainter();
gp.setPreferredSize( new Dimension( 400 , 400 ) );
contentPane.add( gp );
Checkbox checkbox = new Checkbox( "色を変えます" , true );
//------------------------------------------------------
// ItemListenerをチェックボックスに対して追加
//------------------------------------------------------
checkbox.addItemListener( gp );
checkbox.addItemListener( this );
contentPane.add( checkbox );
}
public void itemStateChanged( ItemEvent e ) {
Checkbox checkbox = ( Checkbox ) e.getItemSelectable();
System.out.println( "checkbox status = " + checkbox.getState() );
/* ------- ダブルバッファリングしているかどうかのテストコード ---------------
getRootPane().setDoubleBuffered( ch.getState() );
( ( JComponent ) getContentPane() ).setDoubleBuffered( ch.getState() );
----------------------------------------------------------------------------*/
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/**
<pre>
様々な図形などの描画をコントロールするクラス
</pre>
$Id: SwingGraphics.html,v 1.1 2009/06/22 16:11:58 kishi Exp kishi $
@author KISHI Yasuhiro
*/
class GlobalPainter extends JComponent implements Runnable, ItemListener {
private int count = 0;
private int x = 0;
private Color color = Color.blue;
private Dimension dim = getSize();
/** 矩形を別スレッドで描画するクラス */
private RectFiller rf1;
private RectFiller rf2;
public GlobalPainter() {
( new Thread( this ) ).start();
rf1 = new RectFiller( new Color( 0x33, 0x00, 0x99 ), 30, 40 );
rf1.start();
rf2 = new RectFiller( Color.yellow, 50, 60 );
rf2.start();
}
public void run() {
while ( true ) {
x += 3;
if ( x > 100 ) {
x = 0;
}
/************* 【重要】 ****************/
SwingUtilities.invokeLater(
new Runnable() {
public void run() {
repaint();
}
}
);
try {
Thread.sleep( 10 );
} catch ( Exception exception ) {}
}
}
public void paintComponent( Graphics g ) {
g.setColor( color );
g.fillRect( 10 , 10 , 10 + x , 20 );
/* このオブジェクトは別スレッドで動いている */
rf1.paint( g );
rf2.paint( g );
}
public void itemStateChanged( ItemEvent e ) {
// チェックボックスの状態で色を変える
Checkbox checkbox = ( Checkbox ) e.getItemSelectable();
count++;
System.out.println( "count=" + count );
if ( checkbox.getState() ) {
this.color = Color.blue;
} else {
this.color = Color.red;
}
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/**
$Id: SwingGraphics.html,v 1.1 2009/06/22 16:11:58 kishi Exp kishi $
@author KISHI Yasuhiro
*/
public class RectFiller extends Thread {
private int x = 0;
private Color color;
private int sx;
private int sy;
public RectFiller( Color color, int sx, int sy ) {
this.color = color;
this.sx = sx;
this.sy = sy;
}
public void paint( Graphics g ) {
g.setColor( color );
g.fillRect( sx , sy , 10 + x , 20 );
}
public void run() {
while ( true ) {
if ( x++ > 100 ) {
x = 0;
}
try {
sleep( 100 );
} catch ( Exception exception ) {}
System.out.println( "x=" + x );
}
}
}
戻る