| 情報学部 | 菅沼ホーム | Java 目次 | 基礎技術目次 | 索引 |

01 import java.awt.*;
02 import java.awt.event.*;
03 import javax.swing.*;
04
05 public class Test {
06 public static void main (String[] args)
07 {
08 Win win = new Win("描画例1");
09 }
10 }
11
12 class Win extends JFrame
13 {
14 /******************/
15 /* コンストラクタ */
16 /******************/
17 Win(String name)
18 {
19 // JFrameクラスのコンストラクタ(Windowのタイトルを引き渡す)
20 super(name);
21 // Windowの大きさ
22 setSize(310, 200); // +40, +70
23 // ContentPane の取得と MainPanel の追加
24 MainPanel pn = new MainPanel(); // MainPanel オブジェクトの生成
25 getContentPane().add(pn); // MainPanel オブジェクトを ContentPane に追加
26 pn.setSize(270, 130);
27 // ウィンドウを表示
28 setVisible(true);
29 // イベントアダプタ
30 addWindowListener(new WinEnd());
31 }
32
33 /******************************/
34 /* 上,左,下,右の余白の設定 */
35 /******************************/
36 public Insets getInsets()
37 {
38 return new Insets(50, 20, 20, 20);
39 }
40
41 /************/
42 /* 終了処理 */
43 /************/
44 class WinEnd extends WindowAdapter
45 {
46 public void windowClosing(WindowEvent e) {
47 System.exit(0);
48 }
49 }
50 }
51
52 class MainPanel extends JPanel
53 {
54 MainPanel()
55 {
56 setBackground(new Color(238, 255, 238)); // 背景色の設定
57 }
58 // 描画
59 public void paintComponent(Graphics g)
60 {
61 super.paintComponent(g); // 親クラスの描画
62 // Graphics2Dの取得
63 Graphics2D g2 = (Graphics2D)g;
64 // 線幅が5ピクセルの矩形
65 g2.setStroke(new BasicStroke(5.0f));
66 g2.draw(new Rectangle(20, 15, 100, 100));
67 // 塗りつぶされた円
68 g.setColor(Color.green);
69 g.fillOval(30, 25, 80, 80);
70 // 線幅が5ピクセルの矩形
71 g.setColor(Color.red);
72 g2.draw(new Rectangle(150, 15, 100, 100));
73 }
74 }
Container cP = getContentPane(); cP.add(pn);

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Test {
public static void main (String[] args)
{
Win win = new Win("描画例2");
}
}
class Win extends JFrame
{
/******************/
/* コンストラクタ */
/******************/
Win(String name)
{
// JFrameクラスのコンストラクタ(Windowのタイトルを引き渡す)
super(name);
// Windowの大きさ
setSize(390, 200); // +40, +70
// ContentPane の取得と MainPanel の追加
MainPanel pn = new MainPanel(); // MainPanel オブジェクトの生成
getContentPane().add(pn); // MainPanel オブジェクトを ContentPane に追加
pn.setSize(350, 130);
// ウィンドウを表示
setVisible(true);
// イベントアダプタ
addWindowListener(new WinEnd());
}
/******************************/
/* 上,左,下,右の余白の設定 */
/******************************/
public Insets getInsets()
{
return new Insets(50, 20, 20, 20);
}
/************/
/* 終了処理 */
/************/
class WinEnd extends WindowAdapter
{
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
}
class MainPanel extends JPanel
{
MainPanel()
{
setBackground(new Color(238, 255, 238)); // 背景色の設定
}
// 描画
public void paintComponent(Graphics g)
{
super.paintComponent(g); // 親クラスの描画
// Graphics2Dの取得
Graphics2D g2 = (Graphics2D)g;
// 線幅が5ピクセルの角が丸い矩形
g2.setStroke(new BasicStroke(5.0f));
g2.drawRoundRect(20, 15, 100, 100, 50, 50) ;
// 塗りつぶされた楕円
g.setColor(Color.green);
g.fillOval(130, 40, 100, 50);
// 三角形
g.setColor(Color.red);
g2.drawLine(240, 115, 290, 15);
g2.drawLine(340, 115, 290, 15);
g2.drawLine(240, 115, 340, 115);
}
}
| 情報学部 | 菅沼ホーム | Java 目次 | 基礎技術目次 | 索引 |