Graphics2D爲我們帶來了什麼

   如果使用Graphics g.fillOval(int,int,int,int)這裏只是簡單支持int類型的數據。意思就說會忽略到小數部分,如果你要畫的圓都在0~1內那麼所有的都會被畫成一個點啦~當然你也不必要爲此自己去寫一個函數來實現這個功能。看看Graphics2D爲我們帶來了什麼?

 

public void drawCircle(Graphics g){
       Graphics2D g2=(Graphics2D)g;
       double diameter = 2*radius;
       Color ys=g.getColor();
       g.setColor(this.getColor());
       Ellipse2D circle=new Ellipse2D.Double();
       circle.setFrameFromCenter(pointX, pointY, pointX+radius, pointY+radius);
       ((Graphics2D) g).fill(circle);
      
// g.fillOval(pointX-radius,pointY-radius,diameter,diameter);

   }
}

 

    圓只是特殊的橢圓,我們設定好圓的中心以及a.b就可以完成圓的畫法。更重要的是這裏支持double類型的數據,以及不同的畫法,fill()或者draw()。使用起來也非常方便~

    下面給出Graphics2D中其他圖形的方法

 

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

// 1. Graphics2D中繪製各種圖形的方法

// 2. 矩形 橢圓 線 圓

// 3. GUI中組件 顏色 的設定


public class DrawTest {
  public static void main(String[] args)
     {
      EventQueue.invokeLater(new Runnable()
//事件調度線程

      {
      public void run()
      {
       DrawFrame fram=new DrawFrame();
       fram.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//關閉

       fram.setVisible(true);
//可見

      }
      });
     }

}
class DrawFrame extends JFrame
{
 public DrawFrame()
 {
  setTitle("DrawTest");
  setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);
  
//setBackground(Color.RED);//可用此方法設置組件背景色

  DrawComponent component=new DrawComponent();
  add(component);
  
 }
 public static final int DEFAULT_WIDTH=400;
 public static final int DEFAULT_HEIGHT=400;
}
class DrawComponent extends JComponent
{
 public void paintComponent(Graphics g)
//自動獲取一個Graphics2D類對象

 {
  Graphics2D g2=(Graphics2D)g;
  
  
//畫 矩形

  double leftX=100;
//左上點+寬高

  double topY=100;
  double width=200;
  double height=150;
  
  Rectangle2D rect=new Rectangle2D.Double(leftX,topY,width, height);
  g2.draw(rect);
//首先創建一實現了shape接口的類對象rect,然後調用Graphics2D的draw

               
//另一方法--對角線

               
//Rectangle2D rect=new Rectangle2D.Double();

              
// rect.setFrameFromDiagonal(x1, y1, x2, y2);

               
//亦有用中心的,rect.setFrameCenter

  
  
//畫 橢圓

  Ellipse2D ellipse=new Ellipse2D.Double();
  ellipse.setFrame(rect);
//設置外接矩形

  g2.setPaint(Color.BLUE); 
//設置當前繪製顏色,Graphics2D的對象

                             
//Color.blue可用new Color(int redness,int greenness,int blueness)

    
  g2.draw(ellipse);
  
  
//畫 對角線

  g2.draw(new Line2D.Double(leftX,topY,leftX+width,topY+height));
  
  
//畫 圓(特殊的橢圓)

  double centerX=rect.getCenterX();
//獲取某形狀的中心座標

  double centerY=rect.getCenterY();
  double radius=50;
  
  Ellipse2D circle=new Ellipse2D.Double();
  circle.setFrameFromCenter(centerX, centerY, centerX+radius, centerY+radius);
//設置橢圓中點及a,b

  g2.fill(circle);
//用當前顏色填充一圖形 g2.setPaint(Color.BLUE)設置當前顏色

  
//g2.draw(circle);

  
 }
}

// Graphics2D中繪製各種圖形的方法

// 主要方法:

// Graphics2D g2=(Graphics2D)g;

// g2.draw--------

// 

// 1. Rectangle2D rect=new Rectangle2D.Double(leftX,topY,width, height);

// 2. Ellipse2D ellipse=new Ellipse2D.Double();

// ellipse.setFrame(rect);//設置外接矩形

// 3. g2.draw(new Line2D.Double(leftX,topY,leftX+width,topY+height));

// 4. Ellipse2D circle=new Ellipse2D.Double();

// circle.setFrameFromCenter(centerX, centerY, centerX+radius, centerY+radius);


// 設置橢圓中點及a,b
發佈了7 篇原創文章 · 獲贊 3 · 訪問量 13萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章