import java.awt.*; import java.awt.event.*; import javax.swing.*; public class GSample02 extends JFrame { public GSample02() { setSize( 800, 600 ); setTitle( "プログラムで絵を描こう" ); addWindowListener( new WindowAdapter(){ public void windowClosing( WindowEvent e ) { System.exit( 0 ); } }); GraphicJPanel jp = new GraphicJPanel(); Container cp = getContentPane(); cp.add( jp ); setVisible( true ); } public static void main( String[] args ) { JFrame w = new GSample02(); } public class GraphicJPanel extends JPanel{ public GraphicJPanel() { setBackground( Color.white ); } public void paintComponent( Graphics g ) { int i = 0; g.setColor( new Color( 0xFF6600 ) ); while( i < 10 ) { g.drawOval( 100 + i * 60, 150, 30, 30 ); i++; } for( i = 0; i < 10; i++ ) { if( i % 2 == 0 ) { g.setColor( new Color( 0x33cc00 ) ); } else { g.setColor( new Color( 0x0033ff ) ); } g.drawLine( 100, 275 + i * 10, 700, 275 + i * 10 ); } } } } ********************************************************************** GSample02.javaをコンパイルし、実行すると以下のような描画が行われます。 |
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class GSample03 extends JFrame { public GSample03() { setSize( 800, 600 ); setTitle( "プログラムで絵を描こう" ); addWindowListener( new WindowAdapter(){ public void windowClosing( WindowEvent e ) { System.exit( 0 ); } }); GraphicJPanel jp = new GraphicJPanel(); Container cp = getContentPane(); cp.add( jp ); setVisible( true ); } public static void main( String[] args ) { JFrame w = new GSample03(); } public class GraphicJPanel extends JPanel{ public GraphicJPanel() { setBackground( Color.white ); } public void paintComponent( Graphics g ) { double y_point; double x_point; double x1_point = 100; double x2_point = 700; double y1_point = 100; double y2_point = 500; int count = 0; g.drawLine( ( int )x1_point, ( int )y1_point, ( int )x2_point, ( int )y2_point ); for( x_point = 100; x_point <= 700; x_point += 20 ) { y_point = ( ( ( y2_point - y1_point ) / ( x2_point - x1_point ) ) * ( x_point - x1_point ) ) + y1_point; if( count % 2 == 1 ) { g.setColor( new Color( 0x009933 ) ); } else { g.setColor( new Color( 0xff0033 ) ); } g.drawOval( ( int )x_point - 15, ( int ) y_point - 15, 30, 30 ); count++; } } } } ********************************************************************** GSample03.javaをコンパイルし、実行すると以下のような描画が行われます。 |
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class GSample04 extends JFrame { public GSample04() { setSize( 800, 600 ); setTitle( "プログラムで絵を描こう" ); addWindowListener( new WindowAdapter(){ public void windowClosing( WindowEvent e ) { System.exit( 0 ); } }); GraphicJPanel jp = new GraphicJPanel(); Container cp = getContentPane(); cp.add( jp ); setVisible( true ); } public static void main( String[] args ) { JFrame w = new GSample04(); } public class GraphicJPanel extends JPanel{ public GraphicJPanel() { setBackground( Color.white ); } public void paintComponent( Graphics g ) { int offsetx = 100; int[] xarray1 = new int[ 50 ]; int[] xarray2 = new int[ 50 ]; for( int i = 0; i < xarray1.length; i++ ) { xarray1[ i ] = ( int )( Math.random() * 600.0 ); xarray2[ i ] = ( int )( Math.random() * 600.0 ); if( i % 2 == 0 ) { g.setColor( new Color( 0x66ff33 ) ); g.drawLine( xarray1[ i ] + offsetx, 80, xarray2[ i ] + offsetx, 500 ); } else { g.setColor( new Color( 0xff9933 ) ); g.drawLine( xarray1[ i ] + offsetx, 80, xarray2[ i ] + offsetx, 500 ); } } } } } ********************************************************************** GSample04.javaをコンパイルし、実行すると以下のような描画が行われます。 |