import java.awt.*; import java.awt.geom.*; import java.awt.event.*; import java.awt.image.*; import java.io.*; import javax.swing.*; import javax.imageio.*; public class Test { public static void main (String[] args) { Graphics2D2 win = new Graphics2D2("Graphics2D クラス,drawImage"); } } class Graphics2D2 extends JFrame { /******************/ /* コンストラクタ */ /******************/ Graphics2D2(String name) { // JFrameクラスのコンストラクタ(Windowのタイトルを引き渡す) super(name); // Windowの大きさ setSize(840, 370); // 画像の読み込み Image im = getToolkit().getImage("hana.gif"); MediaTracker trk = new MediaTracker(this); trk.addImage(im, 0); try { trk.waitForID(0); } catch (InterruptedException e) {} // Graphics2D2_MainPanel オブジェクト Graphics2D2_MainPanel pn = new Graphics2D2_MainPanel(im); // Graphics2D2_MainPanel オブジェクトの生成 getContentPane().add(pn); // Graphics2D2_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 Graphics2D2_MainPanel extends JPanel { Image im1; BufferedImage im0; Graphics2D2_MainPanel(Image im) { im1 = im; setBackground(Color.white); // 背景色の設定 // BufferedImageに変換 int w = im1.getWidth(null); int h = im1.getHeight(null); im0 = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); Graphics g = im0.getGraphics(); g.drawImage(im1, 0, 0, null); g.dispose(); } // 描画 public void paintComponent(Graphics g) { super.paintComponent(g); // 親クラスの描画 // 基の画像 g.drawImage(im1, 50, 50, this); // Graphics2Dの取得 Graphics2D g2 = (Graphics2D)g; // フィルタ(ぼかし) ConvolveOp c = new ConvolveOp(new Kernel(7, 7, new float[] { 1/49f, 1/49f, 1/49f, 1/49f, 1/49f, 1/49f, 1/49f, 1/49f, 1/49f, 1/49f, 1/49f, 1/49f, 1/49f, 1/49f, 1/49f, 1/49f, 1/49f, 1/49f, 1/49f, 1/49f, 1/49f, 1/49f, 1/49f, 1/49f, 1/49f, 1/49f, 1/49f, 1/49f, 1/49f, 1/49f, 1/49f, 1/49f, 1/49f, 1/49f, 1/49f, 1/49f, 1/49f, 1/49f, 1/49f, 1/49f, 1/49f, 1/49f, 1/49f, 1/49f, 1/49f, 1/49f, 1/49f, 1/49f, 1/49f }), ConvolveOp.EDGE_NO_OP, null); g2.drawImage(im0, c, 300, 50); // 45度回転 AffineTransform affin = new AffineTransform(); affin.translate(650, 30); //イメージの移動 affin.rotate(45 * Math.PI / 180, 0, 0); //イメージを画像の左上を中心に回転 g2.drawImage(im1, affin, null); //イメージの描画 } }