java 画图


import java.awt.*;
import javax.swing.JFrame;

public class Test extends JFrame {

public Test() {
  super();
  setTitle("FontMetrics Demo");
  setSize(800, 600);
  setVisible(true);
}

public void paint(Graphics g) {
  ((Graphics2D)g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);// ラインを滑らかにとする
  g.setColor(Color.BLUE);
  int r= 4;
  Symbol.paintDiamond(g,100,200,r,false);
  Symbol.paintDiamond(g,100,250,r,true);
  Symbol.paintRect(g,120,200,r,false);
  Symbol.paintRect(g,120,250,r,true);
  Symbol.paintOval(g,140,200,r,false);
  Symbol.paintOval(g,140,250,r,true);
  Symbol.paintUpTriangle(g,160,200,r,false);
  Symbol.paintUpTriangle(g,160,250,r,true);
  Symbol.paintRightTriangle(g,180,200,r,false);
  Symbol.paintRightTriangle(g,180,250,r,true);
  Symbol.paintDownTriangle(g,200,200,r,false);
  Symbol.paintDownTriangle(g,200,250,r,true);
  Symbol.paintLeftTriangle(g,220,200,r,false);
  Symbol.paintLeftTriangle(g,220,250,r,true);
  Symbol.paintCross(g,240,200,r,false);
  Symbol.paintCross(g,240,250,r,true);
  Symbol.paintObliqueCross(g,260,200,r,false);
  Symbol.paintObliqueCross(g,260,250,r,true);
  Symbol.paintYen(g,280,200,r,false);//\
  Symbol.paintYen(g,280,250,r,true);//\
  Symbol.paintStar(g,300,200,r,false);
  Symbol.paintStar(g,300,250,r,true);
  //wujiaoxing2(g,300,250,4,false);
}

public static void main(String[] args) {
  Test fmd = new Test();
  fmd.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

/**
* シンボルのクラス
* @author S-Shou
* @version 2011-12-21
*
*/
final class Symbol{

/**
  * 菱形シンボルを描画する
  * 
  * @param g
  * @param x  センターのX座標
  * @param y  センターのY座標
  * @param radius シンボルの半径
  * @param setBackgroundColorWhite   既定カラー又はワイトを塗潰しする
  */
public static void paintDiamond(Graphics graphics, int x, int y, int radius, boolean setBackgroundColorWhite){
  Point up,down,left,right;
  up = new Point(x, y - radius);
  down = new Point(x, y + radius);
  left = new Point(x - radius, y);
  right = new Point(x + radius, y);
  Polygon p = new Polygon(new int[] { up.x, right.x, down.x, left.x },
    new int[] { up.y, right.y, down.y, left.y }, 4);
  if(setBackgroundColorWhite){
   Color originalColor = graphics.getColor();
   graphics.setColor(Color.WHITE);
   graphics.fillPolygon(p);
   graphics.setColor(originalColor);
  }
  else
   graphics.fillPolygon(p);
  graphics.drawPolygon(p);
}

/**
  * 四角シンボルを描画する
  * 
  * @param graphics
  * @param x  センターのX座標
  * @param y  センターのY座標
  * @param radius シンボルの半径
  * @param setBackgroundColorWhite   既定カラー又はワイトを塗潰しする
  */
public static void paintRect(Graphics graphics, int x, int y, int radius, boolean setBackgroundColorWhite) {
  if(setBackgroundColorWhite){
   Color originalColor = graphics.getColor();
   graphics.setColor(Color.WHITE);
   graphics.fillRect(x - radius , y - radius , radius * 2, radius * 2);
   graphics.setColor(originalColor);
  }
  else
   graphics.fillRect(x - radius , y - radius , radius * 2, radius * 2);
  graphics.drawRect(x - radius , y - radius , radius * 2, radius * 2);
}

/**
  * ドットシンボルを描画する
  * 
  * @param graphics
  * @param x  センターのX座標
  * @param y  センターのY座標
  * @param radius シンボルの半径
  * @param setBackgroundColorWhite   既定カラー又はワイトを塗潰しする
  */
public static void paintOval(Graphics graphics, int x, int y, int radius, boolean setBackgroundColorWhite) {
  if(setBackgroundColorWhite){
   Color originalColor = graphics.getColor();
   graphics.setColor(Color.WHITE);
   graphics.fillOval(x - radius , y - radius , radius * 2, radius * 2);
   graphics.setColor(originalColor);
  }
  else
   graphics.fillOval(x - radius , y - radius , radius * 2, radius * 2);
  graphics.drawOval(x - radius , y - radius , radius * 2, radius * 2);
}

/**
  * 三角(上向き)シンボルを描画する
  * 
  * @param graphics
  * @param x  センターのX座標
  * @param y  センターのY座標
  * @param radius シンボルの半径
  * @param setBackgroundColorWhite   既定カラー又はワイトを塗潰しする
  */
public static void paintUpTriangle(Graphics graphics, int x, int y, int radius, boolean setBackgroundColorWhite) {
  int temp = (int) Math.sqrt(Math.pow(radius * 2, 2) / 3);
  Point up,left,right;
  up = new Point(x, y - radius);
  left = new Point(x - temp, y + radius);
  right = new Point(x + temp, y + radius);
  Polygon p = new Polygon(new int[] { up.x, right.x, left.x }, new int[] {
    up.y, right.y, left.y }, 3);
  if(setBackgroundColorWhite){
   Color originalColor = graphics.getColor();
   graphics.setColor(Color.WHITE);
   graphics.fillPolygon(p);
   graphics.setColor(originalColor);
  }
  else
   graphics.fillPolygon(p);
  graphics.drawPolygon(p);
}

/**
  * 三角(右向き)シンボルを描画する
  * 
  * @param graphics
  * @param x  センターのX座標
  * @param y  センターのY座標
  * @param radius シンボルの半径
  * @param setBackgroundColorWhite   既定カラー又はワイトを塗潰しする
  */
public static void paintRightTriangle(Graphics graphics, int x, int y, int radius, boolean setBackgroundColorWhite) {
  int temp = (int) Math.sqrt(Math.pow(radius * 2, 2) / 3);
  Point up,down,right;
  up = new Point(x - radius, y - temp);
  down = new Point(x - radius, y + temp);
  right = new Point(x + radius, y);
  
  Polygon p = new Polygon(new int[] { up.x, right.x, down.x }, new int[] {
    up.y, right.y, down.y }, 3);
  if(setBackgroundColorWhite){
   Color originalColor = graphics.getColor();
   graphics.setColor(Color.WHITE);
   graphics.fillPolygon(p);
   graphics.setColor(originalColor);
  }
  else
   graphics.fillPolygon(p);
  graphics.drawPolygon(p);
}

/**
  * 三角(下向き)シンボルを描画する
  * 
  * @param graphics
  * @param x  センターのX座標
  * @param y  センターのY座標
  * @param radius シンボルの半径
  * @param setBackgroundColorWhite   既定カラー又はワイトを塗潰しする
  */
public static void paintDownTriangle(Graphics graphics, int x, int y, int radius, boolean setBackgroundColorWhite) {
  int temp = (int) Math.sqrt(Math.pow(radius * 2, 2) / 3);
  Point left,right,down;
  left = new Point(x - temp, y - radius);
  right = new Point(x + temp, y - radius);
  down = new Point(x, y + radius);
  Polygon p = new Polygon(new int[] { right.x, left.x, down.x },
    new int[] { right.y, left.y, down.y }, 3);
  if(setBackgroundColorWhite){
   Color originalColor = graphics.getColor();
   graphics.setColor(Color.WHITE);
   graphics.fillPolygon(p);
   graphics.setColor(originalColor);
  }
  else
   graphics.fillPolygon(p);
  graphics.drawPolygon(p);
}

/**
  * 三角(左向き)シンボルを描画する
  * 
  * @param graphics
  * @param x  センターのX座標
  * @param y  センターのY座標
  * @param radius シンボルの半径
  * @param setBackgroundColorWhite   既定カラー又はワイトを塗潰しする
  */
public static void paintLeftTriangle(Graphics graphics, int x, int y, int radius, boolean setBackgroundColorWhite) {
  int temp = (int) Math.sqrt(Math.pow(radius * 2, 2) / 3);
  Point up,left,down;
  up = new Point(x + radius, y - temp);
  left = new Point(x - radius, y);
  down = new Point(x + radius, y + temp);
  Polygon p = new Polygon(new int[] { up.x, down.x, left.x }, new int[] {
    up.y, down.y, left.y }, 3);
  if(setBackgroundColorWhite){
   Color originalColor = graphics.getColor();
   graphics.setColor(Color.WHITE);
   graphics.fillPolygon(p);
   graphics.setColor(originalColor);
  }
  else
   graphics.fillPolygon(p);
  graphics.drawPolygon(p);
}

/**
  * クロスシンボルを描画する
  * 
  * @param graphics
  * @param x  センターのX座標
  * @param y  センターのY座標
  * @param radius シンボルの半径
  * @param setBackgroundColorWhite   既定カラー又はワイトを塗潰しする
  */
public static void paintCross(Graphics graphics, int x, int y, int radius, boolean setBackgroundColorWhite){
  if(setBackgroundColorWhite){
   Color originalColor = graphics.getColor();
   graphics.setColor(Color.WHITE);
   graphics.fillRect(x-radius, y-radius, radius * 2, radius * 2);
   graphics.setColor(originalColor);
  }
  graphics.drawLine(x, y - radius, x, y + radius);
  graphics.drawLine(x - radius, y , x + radius, y);
}

/**
  * 斜めクロスシンボルを描画する
  * 
  * @param graphics
  * @param x  センターのX座標
  * @param y  センターのY座標
  * @param radius シンボルの半径
  * @param setBackgroundColorWhite   既定カラー又はワイトを塗潰しする
  */
public static void paintObliqueCross(Graphics graphics, int x, int y, int radius, boolean setBackgroundColorWhite){
  if(setBackgroundColorWhite){
   Color originalColor = graphics.getColor();
   graphics.setColor(Color.WHITE);
   graphics.fillRect(x-radius, y-radius, radius * 2, radius * 2);
   graphics.setColor(originalColor);
  }
  graphics.drawLine(x-radius,y-radius,x+radius,y+radius);
  graphics.drawLine(x-radius,y+radius,x+radius,y-radius);
}

/**
  * 星シンボル(★)を描画する
  * 
  * @param graphics
  * @param x  センターのX座標
  * @param y  センターのY座標
  * @param radius シンボルの半径
  * @param setBackgroundColorWhite   既定カラー又はワイトを塗潰しする
  */
public static void paintStar(Graphics g, int x, int y, int radius, boolean setBackgroundColorWhite) {
  radius =(int) Math.round(Math.sqrt(2*radius*radius));
  double ch = 72 * Math.PI / 180;
  int x1 = x;
  int x2 = (int) (x - Math.sin(ch) * radius); 
  int x3 = (int) (x + Math.sin(ch)* radius);
  int x4 = (int) (x - Math.sin(ch / 2) * radius);
  int x5 = (int) (x + Math.sin(ch / 2) * radius);
  int y1 = y - radius, 
  y2 = (int) (y - Math.cos(ch) * radius), 
  y3 = y2, 
  y4 = (int) (y + Math.cos(ch / 2)* radius), 
  y5 = y4;
  int bx = (int) (x + Math.cos(ch) * Math.tan(ch / 2) * radius);
  int by = y2;
  Polygon a = new Polygon();
  Polygon b = new Polygon();
  a.addPoint(x2, y2);
  a.addPoint(x5, y5);
  a.addPoint(bx, by);
  b.addPoint(x1, y1);
  b.addPoint(bx, by);
  b.addPoint(x3, y3);
  b.addPoint(x4, y4);
  if (setBackgroundColorWhite) {
   Color originalColor = g.getColor();
   g.setColor(Color.WHITE);
   g.fillPolygon(a);
   g.fillPolygon(b);
   g.setColor(originalColor);
   g.drawPolygon(a);
   g.drawPolygon(b);
  }else{
   g.fillPolygon(a);
   g.fillPolygon(b);
  }
}

/**
  * 円シンボル(¥)を描画する
  * 
  * @param graphics
  * @param x  センターのX座標
  * @param y  センターのY座標
  * @param radius シンボルの半径
  * @param setBackgroundColorWhite   既定カラー又はワイトを塗潰しする
  */
public static void paintYen(Graphics graphics, int x, int y, int radius, boolean setBackgroundColorWhite) {
  if(setBackgroundColorWhite){
   Color originalColor = graphics.getColor();
   graphics.setColor(Color.WHITE);
   graphics.fillRect(x-radius, y-radius, radius * 2, radius * 2);
   graphics.setColor(originalColor);
  }
  graphics.drawLine(x - radius, y - radius, x, y - radius / 3);
  graphics.drawLine(x + radius, y - radius, x, y - radius / 3);
  graphics.drawLine(x - radius, y - radius / 3, x + radius, y - radius / 3);
  graphics.drawLine(x - radius, y + radius / 3, x + radius, y + radius / 3);
  graphics.drawLine(x, y - radius / 3, x, y + radius);
}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值