1 / 14

Image の描画

Image の描画. 画像を読み込んで表示すること。 import java.awt.*; が必要。 getImage で 画面に表示できる Image オブジェクトを返す。. drawImage. drawImage(Image img, int x, int y, int width, int height ,Color bgcolor, ImageObserver observer) Img …… 描画する Image クラス型オブジェクト。 getImage() メソッドで作成する。

kateb
Download Presentation

Image の描画

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Imageの描画 画像を読み込んで表示すること。 import java.awt.*;が必要。 getImageで 画面に表示できるImageオブジェクトを返す。

  2. drawImage • drawImage(Image img, int x, int y, intwidth, intheight ,Color bgcolor, ImageObserver observer) Img……描画するImageクラス型オブジェクト。getImage()メソッドで作成する。 x,y……描画する領域の座標、実行アプレットの左隅が0,0となり、x軸は右に、y軸は下方向が正となる。 width……描画領域の幅を指定する。 Height……描画領域の高さを指定する。 bgcolor……背景色を示す。 observer……画像が更新されるときにその情報を通知するオブジェクト。通常はアプレットがイメージ・オブサーバーになるため、thisを指定することになる。

  3. Image1.javaの説明 import java.applet.Applet; import java.awt.*; public class Image1 extends Applet{ public void paint(Graphics g){ Image c = getImage(getDocumentBase(), “pii.gif"); g.drawImage(c, 100, 100, this); } } アプレットが組み込まれているドキュメントのURLを返す。 画像と、その画像を表示するための座標、幅、高さなどをここで決められる。 座標を決める数字、x、y座標の順番

  4. 実行例

  5. 演習をやってみよう • 演習8-1 Image1.javaを縮小して表示するImage2.javaを書け。

  6. 実行例

  7. BufferedImageについて • Image直径のサブクラス • アクセス可能なバッファを備えたImageを記述。画像に文字などを書き加えることが出来る。 • import java.awt.image.BufferedImage;が必要。

  8. TYPE_INT_RGB • int型ピクセルに格納された 8 ビット RGB 色成分によるイメージを表す。

  9. Buffered.javaの説明 import java.applet.*; import java.awt.*; import java.awt.image.BufferedImage; public class Buffered extends Applet{ public void paint(Graphics g){ Image image = getImage(getDocumentBase(), "finger.jpg"); BufferedImagebi=newBufferedImage(250,250,BufferedImage.TYPE_IN_RGB); bi.getGraphics().drawImage(image, 0, 0, null); g.drawImage(bi, 0, 0, this); • } • } このふたつでBufferedImageを初期化し、その上に画像を展開している。 ここで画像の幅と高さを決める

  10. Pixel.javaの説明 import java.applet.*; import java.awt.*; import java.awt.image.BufferedImage; public class Pixel extends Applet{ public void paint(Graphics g){ Image image = getImage(getDocumentBase(), "pii.gif"); BufferedImage bi = new BufferedImage(250, 250, BufferedImage.TYPE_INT_RGB); bi.getGraphics().drawImage(image, 0, 0, null); int c = bi.getRGB(100, 100); short r0 = (short)((c >> 16 )& 0xff); short g0 = (short)((c >> 8 )& 0xff); short b0 = (short)((c )& 0xff); System.out.printf("%2x%2x%2x ", r0, g0, b0); bi.setRGB(100, 110, 0xffff0000); g.drawImage(bi, 0, 0, this); } } ここで画素の値をintcに読み込んでいる。 ここで読み込んだ画素の値をRGBに展開するためにビット演算を求める ここで16進数の表示を行っている。 ここで逆にドットを塗っている。

  11. 宿題 • テキストの9.1をといてください。 • クラス名はMywork.java

  12. オリジナル問題1 • パックマンの色をデータの引き渡しで指定するプログラムPacColor.javaとPacColor.htmlを完成させよ。 • パックマンの色を数値で指定する方法についてはPac2.javaを参考にすること。

  13. Pac2.java import java.awt.*; import java.applet.Applet; public class Pac2 extends Applet{ public void paint(Graphics g){ g.setColor(new Color(50,50,200)); g.fillArc(100,100,80,80,30,300); } } 色をR,G,Bで表している

  14. オリジナル問題2 • Image2.javaを改良し、小さい画像、中くらいの画像、大きい画像の三つの違う画像を画像同士が重ならないように表示するプログラムImage3.javaを作成せよ。 • ただし、表示させる画像は各自で用意すること。

More Related