テクスチャーパターンの描画サンプル

Javaグラフィックサンプル(アプレット版):テクスチャーに関する図形描画サンプルです。いろいろなテクスチャーを図形に貼り付けるサンプルです。

Home > サンプル集 > アプレットサンプル集 > いろいろなテクスチャーパターン


いろいろなテクスチャーパターン

このページには、以下のサンプルを掲載しています。 下の項目をクリックをすると各サンプルにジャンプします。 ※2週間以内の新着記事はNewアイコン、更新記事はUpアイコンが表示されます。
  1. テクスチャーパターン(市松模様) ( TextureSample01.java )  
  2. テクスチャーパターン(格子模様) ( TextureSample02.java )  
  3. テクスチャーパターン(ドットパターン) ( TextureSample03.java )  
  4. テクスチャーパターン(斜めの格子模様) ( TextureSample04.java )  
  5. テクスチャーパターン(十字模様) ( TextureSample05.java )  
  6. テクスチャーパターン(ハート模様) ( TextureSample06.java )  
  7. テクスチャーパターン(文字列) ( TextureSample07.java )  

■テクスチャーパターン(市松模様)

[ サンプルプログラムのソースコード - TextureSample01.java - ]
  1. package sample.applet;
  2. import javax.swing.JApplet;
  3. import java.awt.Color;
  4. import java.awt.Graphics;
  5. import java.awt.Graphics2D;
  6. import java.awt.Rectangle;
  7. import java.awt.TexturePaint;
  8. import java.awt.image.BufferedImage;
  9. public class TextureSample01 extends JApplet {
  10.   public void paint(Graphics g){
  11.     Graphics2D g2 = (Graphics2D)g;
  12.     BufferedImage bi = new BufferedImage(20,20,BufferedImage.TYPE_INT_RGB);
  13.     Graphics2D bg = bi.createGraphics();
  14.     Rectangle r = new Rectangle(0,0,20,20);
  15.     bg.setColor(Color.ORANGE);
  16.     bg.fillRect(0, 0, 10, 10);
  17.     bg.fillRect(10, 10, 10, 10);
  18.     bg.setColor(Color.GREEN);
  19.     bg.fillRect(10, 0, 10, 10);
  20.     bg.fillRect(0, 10, 10, 10);
  21.     TexturePaint tp = new TexturePaint(bi,r);
  22.     g2.setPaint(tp);
  23.     g2.fillOval(10, 10, 100, 100);
  24.   }
  25. }
[ サンプルプログラムの実行結果 ]



■テクスチャーパターン(格子模様)

[ サンプルプログラムのソースコード - TextureSample02.java - ]
  1. package sample.applet;
  2. import javax.swing.JApplet;
  3. import java.awt.Color;
  4. import java.awt.Graphics;
  5. import java.awt.Graphics2D;
  6. import java.awt.Rectangle;
  7. import java.awt.TexturePaint;
  8. import java.awt.image.BufferedImage;
  9. public class TextureSample02 extends JApplet {
  10.   public void paint(Graphics g){
  11.     Graphics2D g2 = (Graphics2D)g;
  12.     BufferedImage bi = new BufferedImage(10,10,BufferedImage.TYPE_INT_RGB);
  13.     Graphics2D bg = bi.createGraphics();
  14.     Rectangle r = new Rectangle(0,0,10,10);
  15.     bg.setColor(Color.CYAN);
  16.     bg.fillRect(0, 0, 10, 10);
  17.     bg.setColor(Color.BLUE);
  18.     bg.drawLine(0, 5, 10, 5);
  19.     bg.drawLine(5, 0, 5, 10);
  20.     TexturePaint tp = new TexturePaint(bi,r);
  21.     g2.setPaint(tp);
  22.     g2.fillOval(10, 10, 100, 100);
  23.   }
  24. }
[ サンプルプログラムの実行結果 ]



■テクスチャーパターン(ドットパターン)

[ サンプルプログラムのソースコード - TextureSample03.java - ]
  1. package sample.applet;
  2. import javax.swing.JApplet;
  3. import java.awt.Color;
  4. import java.awt.Graphics;
  5. import java.awt.Graphics2D;
  6. import java.awt.Rectangle;
  7. import java.awt.TexturePaint;
  8. import java.awt.image.BufferedImage;
  9. public class TextureSample03 extends JApplet {
  10.   public void paint(Graphics g){
  11.     Graphics2D g2 = (Graphics2D)g;
  12.     BufferedImage bi = new BufferedImage(10,10,BufferedImage.TYPE_INT_RGB);
  13.     Graphics2D bg = bi.createGraphics();
  14.     Rectangle r = new Rectangle(0,0,10,10);
  15.     bg.setColor(Color.ORANGE);
  16.     bg.fillRect(0, 0, 10, 10);
  17.     bg.setColor(Color.BLUE);
  18.     bg.fillRect(2, 2, 2, 2);
  19.     bg.fillRect(7, 2, 2, 2);
  20.     bg.fillRect(2, 7, 2, 2);
  21.     bg.fillRect(7, 7, 2, 2);
  22.     TexturePaint tp = new TexturePaint(bi,r);
  23.     g2.setPaint(tp);
  24.     g2.fillOval(10, 10, 100, 100);
  25.   }
  26. }
[ サンプルプログラムの実行結果 ]



■テクスチャーパターン(斜めの格子模様)

[ サンプルプログラムのソースコード - TextureSample04.java - ]
  1. package sample.applet;
  2. import javax.swing.JApplet;
  3. import java.awt.Color;
  4. import java.awt.Graphics;
  5. import java.awt.Graphics2D;
  6. import java.awt.Rectangle;
  7. import java.awt.TexturePaint;
  8. import java.awt.image.BufferedImage;
  9. public class TextureSample04 extends JApplet {
  10.   public void paint(Graphics g){
  11.     Graphics2D g2 = (Graphics2D)g;
  12.     BufferedImage bi = new BufferedImage(10,10,BufferedImage.TYPE_INT_RGB);
  13.     Graphics2D bg = bi.createGraphics();
  14.     Rectangle r = new Rectangle(0,0,10,10);
  15.     bg.setColor(Color.ORANGE);
  16.     bg.fillRect(0, 0, 10, 10);
  17.     bg.setColor(Color.BLUE);
  18.     bg.drawLine(0, 0, 10, 10);
  19.     bg.drawLine(10, 0, 0, 10);
  20.     TexturePaint tp = new TexturePaint(bi,r);
  21.     g2.setPaint(tp);
  22.     g2.fillOval(10, 10, 100, 100);
  23.   }
  24. }
[ サンプルプログラムの実行結果 ]



■テクスチャーパターン(十字模様)

[ サンプルプログラムのソースコード - TextureSample05.java - ]
  1. package sample.applet;
  2. import javax.swing.JApplet;
  3. import java.awt.Color;
  4. import java.awt.Graphics;
  5. import java.awt.Graphics2D;
  6. import java.awt.Rectangle;
  7. import java.awt.TexturePaint;
  8. import java.awt.image.BufferedImage;
  9. public class TextureSample05 extends JApplet {
  10.   public void paint(Graphics g){
  11.     BufferedImage bi = new BufferedImage(10,10,BufferedImage.TYPE_INT_RGB);
  12.     Graphics2D bg = bi.createGraphics();
  13.     Rectangle r = new Rectangle(0,0,10,10);
  14.     bg.setColor(Color.ORANGE);
  15.     bg.fillRect(0, 0, 10, 10);
  16.     bg.setColor(Color.BLUE);
  17.     bg.drawLine(5, 3, 5, 7);
  18.     bg.drawLine(3, 5, 7, 5);
  19.     TexturePaint tp = new TexturePaint(bi,r);
  20.     g2.setPaint(tp);
  21.     g2.fillOval(10, 10, 100, 100);
  22.   }
  23. }
[ サンプルプログラムの実行結果 ]



■テクスチャーパターン(ハート模様)

[ サンプルプログラムのソースコード - TextureSample06.java - ]
  1. package sample.applet;
  2. import javax.swing.JApplet;
  3. import java.awt.Color;
  4. import java.awt.Graphics;
  5. import java.awt.Graphics2D;
  6. import java.awt.RadialGradientPaint;
  7. import java.awt.Rectangle;
  8. import java.awt.TexturePaint;
  9. import java.awt.geom.Arc2D;
  10. import java.awt.geom.Area;
  11. import java.awt.geom.Ellipse2D;
  12. import java.awt.image.BufferedImage;
  13. public class TextureSample06 extends JApplet {
  14.   private static final long serialVersionUID = 1L;
  15.   public void paint(Graphics g){
  16.     Graphics2D g2 = (Graphics2D)g;
  17.     BufferedImage bi = new BufferedImage(30,30,BufferedImage.TYPE_INT_RGB);
  18.     Graphics2D bg = bi.createGraphics();
  19.     Rectangle r = new Rectangle(0,0,30,30);
  20.     Ellipse2D e1 = new Ellipse2D.Double( 0, 0, 10, 10);
  21.     Ellipse2D e2 = new Ellipse2D.Double(10, 0, 10, 10);
  22.     Arc2D arc = new Arc2D.Double(0, 4, 20, 25,34,112,Arc2D.PIE);
  23.     Area a1 = new Area(e1);
  24.     Area a2 = new Area(e2);
  25.     Area a3 = new Area(arc);
  26.     a1.add(a2);
  27.     a1.add(a3);
  28.     bg.setColor(Color.cyan);
  29.     bg.fillRect(0, 0, 50, 50);
  30.     float[] dist = {0.0f, 0.5f, 1.0f};
  31.     Color[] colors = {Color.WHITE, Color.PINK, Color.MAGENTA};
  32.     RadialGradientPaint rgp =
  33.         new RadialGradientPaint( 10, 20, 30, dist, colors);
  34.     bg.setPaint(rgp);
  35.     bg.fill(a1);
  36.     bg.setColor(Color.MAGENTA);
  37.     bg.draw(a1);
  38.     TexturePaint tp = new TexturePaint(bi,r);
  39.     g2.setPaint(tp);
  40.     g2.fillOval(10, 10, 100, 100);
  41.     g2.setColor(Color.BLUE);
  42.     g2.drawOval(10, 10, 100, 100);
  43.   }
  44. }
[ サンプルプログラムの実行結果 ]



■テクスチャーパターン(文字列)

[ サンプルプログラムのソースコード - TextureSample07.java - ]
  1. import javax.swing.JApplet;
  2. import java.awt.Color;
  3. import java.awt.Graphics;
  4. import java.awt.Graphics2D;
  5. import java.awt.Rectangle;
  6. import java.awt.TexturePaint;
  7. import java.awt.font.TextAttribute;
  8. import java.awt.geom.Ellipse2D;
  9. import java.awt.image.BufferedImage;
  10. import java.text.AttributedCharacterIterator;
  11. import java.text.AttributedString;
  12. public class TextureSample07 extends JApplet {
  13.   private static final long serialVersionUID = 1L;
  14.   public void paint(Graphics g){
  15.     Graphics2D g2 = (Graphics2D)g;
  16.     BufferedImage bi = new BufferedImage(35,40,BufferedImage.TYPE_INT_RGB);
  17.     Graphics2D bg = bi.createGraphics();
  18.     bg.setBackground(Color.BLUE);
  19.     bg.clearRect(0, 0, bi.getWidth(), bi.getHeight());
  20.     Rectangle r = new Rectangle(0,0,35,40);
  21.     AttributedString as = new AttributedString("Java");
  22.     as.addAttribute(TextAttribute.WEIGHT, 8);
  23.     as.addAttribute(TextAttribute.SIZE, 16, 0, 1);
  24.     as.addAttribute(TextAttribute.FOREGROUND, Color.ORANGE,0,1);
  25.     as.addAttribute(TextAttribute.FOREGROUND, Color.GREEN,1,4);
  26.     AttributedCharacterIterator aci = as.getIterator();
  27.     bg.drawString(aci,2, 18);
  28.     bg.drawString(aci,20, 38);
  29.     bg.drawString(aci,-14, 38);
  30.     TexturePaint tp = new TexturePaint(bi,r);
  31.     g2.setPaint(tp);
  32.     Ellipse2D ellipse = new Ellipse2D.Double();
  33.     ellipse.setFrame(10,10,100,100);
  34.     g2.fill(ellipse);
  35.   }
  36. }
[ サンプルプログラムの実行結果 ]





最終更新日:2019/02/13

2015-03-01からの訪問者数
  1411 人