我想畫一個圓,它在屏幕的中間產地:繪圖圈手工的方式在Java返回奇怪的結果
width = canvas.getWidth();
height = canvas.getHeight();
BufferStrategy bufferStrategy = canvas.getBufferStrategy();
if(bufferStrategy == null){//If bufferStrategy is not initialized yet
canvas.createBufferStrategy(3);
bufferStrategy = canvas.getBufferStrategy();
}
Graphics graphics = bufferStrategy.getDrawGraphics();
public int[] pixels = new int[width * height];
int radius = height/6;
for(int theta = 0; theta < 360; theta++){
double rads = Math.toRadians(theta);
double x = (width/2) + (Math.cos(rads) * radius);
double y = (height/2) + (Math.sin(rads) * radius);
pixels[(int)(x + y * width)] = 0xFFFF00FF;
}
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
image.setRGB(0, 0, width, height, pixels, 0, width);
graphics.drawImage(image, 0, 0, width, height, null);
但我越來越怪異的結果:
感謝您的幫助!
您可以創建一個[最小,**完整**和可驗證的示例](http://stackoverflow.com/help/mcve)?例如,你從哪裏得到'graphics'實例? –
@ToddSewell完成! –
如果將'BufferedImage'保存到文件,圖像看起來是否正確? (如果你不知道怎麼做,請嘗試[本教程](https://docs.oracle.com/javase/tutorial/2d/images/saveimage.html) –