2016-03-08 178 views
1

我有我的學習圖形課程。我目前的任務是使用Bresenham算法繪製一個十六進制,並使用基於堆棧的遞歸算法進行洪水填充。所以我需要輪廓的顏色來使用填充。下面如何將Graphics2D轉換爲BufferedImage?

的代碼繪製使用Graphics2D行,我需要得到各顏色畫像素。據我所知,Graphics2D是一個不包含像素的抽象。所以,我需要的十六進制轉換爲BufferedImage,用.getRGB()方法,並得到了像素的顏色。但我無法做到它的頭或尾。

我怎樣才能讓一個十六進制的BufferedImageGraphics2D畫?

public void drawHexagon(int x, int y, int edgeLength, int thickness, Graphics2D g2d) { 
    int cosEdgeLength = (int) (Math.cos(Math.PI/6) * edgeLength); 
    BufferedImage image = new BufferedImage(edgeLength*2, cosEdgeLength*2 + edgeLength, BufferedImage.TYPE_INT_RGB); 
    drawBresenhamLine(x, y, x - edgeLength, y + cosEdgeLength, g2d, thickness); 
    drawBresenhamLine(x, y, x + edgeLength, y + cosEdgeLength, g2d, thickness); 
    drawBresenhamLine(x - edgeLength, y + cosEdgeLength, x - edgeLength, y + cosEdgeLength + edgeLength, g2d, thickness); 
    drawBresenhamLine(x + edgeLength, y + cosEdgeLength, x + edgeLength, y + cosEdgeLength + edgeLength, g2d, thickness); 
    drawBresenhamLine(x + edgeLength, y + cosEdgeLength + edgeLength, x, y + cosEdgeLength + edgeLength + cosEdgeLength, g2d, thickness); 
    drawBresenhamLine(x - edgeLength, y + cosEdgeLength + edgeLength, x, y + cosEdgeLength + edgeLength + cosEdgeLength, g2d, thickness); 
    g2d.drawImage(image, null, 0, 0); 
} 

void drawBresenhamLine (double xstart, double ystart, double xend, double yend, Graphics2D g, int thickness) 
{ 
    double x, y, dx, dy, incx, incy, pdx, pdy, es, el, err; 
    dx = xend - xstart; 
    dy = yend - ystart; 
    incx = sign(dx); 
    incy = sign(dy); 
    if (dx < 0) dx = -dx; 
    if (dy < 0) dy = -dy; 
    if (dx > dy) { 
     pdx = incx; pdy = 0; 
     es = dy; el = dx; 
    } else { 
     pdx = 0; pdy = incy; 
     es = dx; el = dy; 
    } 
    x = xstart; 
    y = ystart; 
    err = el/2; 
    g.setStroke(new BasicStroke(thickness, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_MITER)); 
    g.draw(new Line2D.Double(x, y, x, y)); 
    for (int t = 0; t < el; t++) { 
     err -= es; 
     if (err < 0) { 
      err += el; 
      x += incx; 
      y += incy; 
     } else { 
      x += pdx; 
      y += pdy; 
     } 
     g.draw(new Line2D.Double(x, y, x, y)); 
    } 
} 
+0

看一看[繪製圖像(https://docs.oracle.com/javase/tutorial/2d/images/drawimage.html) – MadProgrammer

回答

2

這是非常簡單的,創建再一個BufferedImage,其上描繪

int width = 500; // example value 
    int height = 500; // example value 
    BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); 


    Graphics2D g2d = bi.createGraphics(); 

使用該顯卡在:

drawHexagon(int x, int y, int edgeLength, int thickness, Graphics2D g2d) 

執行後,你將有此BufferedImage

繪圖
+2

考慮使用'createGraphics'過'getGraphics',因爲它返回一個'Graphics2D'(所以沒有鑄造)和'getGraphics'提供向後兼容性 – MadProgrammer

+0

@MadProgrammer這是一個很好的提示,謝謝:) –

+2

*「這是一個很好的提示「* [編輯答案](http://stackoverflow.com/posts/35861776/edit)會更好。 ;) –

0

如果您想在BufferedImage上繪製而不是在面板上繪圖,請獲取Graphics屬性從BufferedImage的y和在其上繪製:

class DrawingSpace extends JPanel{  
    private BufferedImage buf; 

    public DrawingSpace(){    
     //Initialization of variables and dimensions not shown 
     buf = new BufferedImage(100, 100, BufferedImage.TYPE_INT_ARGB); 
    } 

    public void drawOnBuffer(){ 
     Graphics g2d = buf.createGraphics(); 
     g2d .setColor(Color.BLUE); //draw the things you want 
     g2d .fillOval(0,0,200,200); //draw the things you want 
     g2d .dispose(); 
    } 
} 
+1

'擴展JPanel'不需要那個。說到顯示圖像,使用帶圖標的「JLabel」更容易。 –

+0

@AndrewThompson我被輸送,如果他使用的JPanel自定義繪製的想法,他依然可以用圖形來吸取其他地方(如對BufferedImage和不一定的JPanel)。 – user3437460

+0

你的榜樣調用'的createGraphics時拋出'java.lang.NullPointerException'()'。 – trashgod

相關問題