import java.awt.*; import java.awt.event.*; import java.awt.geom.*; import javax.swing.*; import java.awt.font.*; import java.text.*; public class Test { public static void main (String[] args) { Graphics2D1 win = new Graphics2D1("Graphics2D クラスのメソッド setStroke 等"); } } class Graphics2D1 extends JFrame { /******************/ /* コンストラクタ */ /******************/ Graphics2D1(String name) { // JFrameクラスのコンストラクタ(Windowのタイトルを引き渡す) super(name); // Windowの大きさ setSize(340, 400); // Graphics2D1_MainPanel オブジェクト Graphics2D1_MainPanel pn = new Graphics2D1_MainPanel(); // Graphics2D1_MainPanel オブジェクトの生成 getContentPane().add(pn); // Graphics2D1_MainPanel オブジェクトを ContentPane に追加 // ウィンドウを表示 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 Graphics2D1_MainPanel extends JPanel { Graphics2D1_MainPanel() { setBackground(Color.white); // 背景色の設定 } // 描画 public void paintComponent(Graphics g) { super.paintComponent(g); // 親クラスの描画 // Graphics2Dの取得 Graphics2D g2 = (Graphics2D)g; // 文字列(最下段に描画) // 通常の文字列 Font f = new Font("TimesRoman", Font.BOLD, 30); g2.setFont(f); g2.drawString("FontColor", 50, 250); // 属性付き文字列 AttributedString str = new AttributedString("FontColor"); str.addAttribute(TextAttribute.FONT,new Font("TimesRoman", Font.BOLD, 50), 0, 4); str.addAttribute(TextAttribute.FOREGROUND, Color.RED, 0, 4); str.addAttribute(TextAttribute.FONT,new Font("TimesRoman", Font.BOLD, 30), 4, 9); str.addAttribute(TextAttribute.FOREGROUND, Color.GREEN, 4, 9); AttributedCharacterIterator ac = str.getIterator(); g2.drawString(ac, 50, 300); // 線幅が5ピクセルの矩形(左上) g2.setStroke(new BasicStroke(5.0f)); g2.draw(new Rectangle(20, 20, 100, 50)); // 塗りつぶした矩形(赤)(左中) g2.setColor(Color.red); g2.fill(new Rectangle(20, 90, 100, 50)); // 塗りつぶした矩形(グラデーション)(左下) g2.setPaint(new GradientPaint(20f, 160f, Color.yellow, 120f, 160f, Color.red)); g2.fill(new Rectangle(20, 160, 100, 50)); // 移動(右上) g2.setColor(Color.green); g2.setStroke(new BasicStroke(5.0f)); g2.transform(new AffineTransform()); g2.translate(140.0, 0.0); g2.draw(new Rectangle(20, 20, 100, 50)); // 回転(右中) g2.translate(30, 70); g2.rotate(45.0 * Math.PI / 180.0); g2.draw(new Rectangle(30, 0, 100, 50)); // 縮小(右下) g2.rotate(-45.0 * Math.PI / 180.0); g2.scale(0.7, 0.7); g2.draw(new Rectangle(0, 150, 100, 50)); } }