import java.awt.*; import java.awt.event.*; import javax.swing.*; public class GSample01 extends JFrame { public GSample01() { 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 GSample01(); } public class GraphicJPanel extends JPanel{ public GraphicJPanel() { setBackground( Color.white ); } public void paintComponent( Graphics g ) { //ここから描画命令を追加 g.setColor( new Color( 255, 51, 0 ) ); g.drawLine( 100, 275, 700, 275 ); g.setColor( new Color( 0x33, 0xcc, 0x00 ) ); g.drawOval( 450, 270, 100, 100 ); g.fillOval( 475, 295, 50, 50 ); g.setColor( new Color( 0x3366ff ) ); g.drawRect( 200, 200, 400, 200 ); g.fillRect( 200, 200, 50, 50 ); //ここまでに描画命令を追加 } } } ********************************************************************** GSample01.javaをコンパイルし、実行すると以下のような描画が行われます。 |
|