2014-10-28 64 views
2

而不是使用普通的plot.setSectionPaint()方法來填充餡餅部分的顏色,是否可以添加圖案或圖像,如下所示。將圖像/圖案應用於餡餅部分而不是普通的顏色

我有這個要求,因爲當用黑色&白色打印機打印時,顏色很難區分。

最壞的情況下,如果不可能的,那麼我將不得不使用2種非常不同的顏色這是乙級以上容易區分/ W打印機

pie chart

回答

0

setSectionPaint允許接口java.awt.Paint的任何實現傳遞。 Color是一個實現Paint的類,但也有其他類,如TexturePaint

此代碼將創建一個TexturePaint像在您的示例藍線模式:

BufferedImage texture = new BufferedImage(1, 30, BufferedImage.TYPE_INT_RGB); 
Graphics2D g = texture.createGraphics(); 
g.setColor(Color.white); 
g.fillRect(0, 0, 1, 30); 
g.setColor(new Color(0x26A2D6)); 
g.fillRect(0, 0, 1, 20); 
g.dispose(); 

TexturePaint texturePaint = new TexturePaint(texture, 
    texture.getRaster().getBounds()); 

然後,您可以傳遞texturePaint作爲參數來setSectionPaint

+0

你簡直太棒了!我不能再多謝你了。你一定是Java大師 – 2014-10-28 15:07:25