如何使用Canvas在Java中绘制一个圆

时间:2017-03-19 16:00:07

标签: java canvas

我必须使用java和canvas为Coursework构建一个弹球式游戏,但是我无法设法绘制圆圈,我收到以下错误: "非静态方法fillCircle(int,int,int)不能从静态上下文引用" 这是我目前的代码,位置和直径类别设置并完美运行:

public void drawPinball1()
{
    Canvas.fillCircle(currentXLocation, currentYLocation, getDiameter());
}

2 个答案:

答案 0 :(得分:3)

图形类'绘图方法

// Drawing (or printing) texts on the graphics screen:
drawString(String str, int xBaselineLeft, int yBaselineLeft);

// Drawing lines:
drawLine(int x1, int y1, int x2, int y2);
drawPolyline(int[] xPoints, int[] yPoints, int numPoint);

// Drawing primitive shapes:
drawRect(int xTopLeft, int yTopLeft, int width, int height);
drawOval(int xTopLeft, int yTopLeft, int width, int height);
drawArc(int xTopLeft, int yTopLeft, int width, int height, int startAngle, int arcAngle);
draw3DRect(int xTopLeft, int, yTopLeft, int width, int height, boolean raised);
drawRoundRect(int xTopLeft, int yTopLeft, int width, int height, int arcWidth, int arcHeight)
drawPolygon(int[] xPoints, int[] yPoints, int numPoint);

// Filling primitive shapes:
fillRect(int xTopLeft, int yTopLeft, int width, int height);
fillOval(int xTopLeft, int yTopLeft, int width, int height);
fillArc(int xTopLeft, int yTopLeft, int width, int height, int startAngle, int arcAngle);
fill3DRect(int xTopLeft, int, yTopLeft, int width, int height, boolean raised);
fillRoundRect(int xTopLeft, int yTopLeft, int width, int height, int arcWidth, int arcHeight)
fillPolygon(int[] xPoints, int[] yPoints, int numPoint);

// Drawing (or Displaying) images:
drawImage(Image img, int xTopLeft, int yTopLeft, ImageObserver obs);  // draw image with its size
drawImage(Image img, int xTopLeft, int yTopLeft, int width, int height, ImageObserver o);  // resize image on screen

在您的情况下,您将使用drawOval(int xTopLeft, int yTopLeft, int width, int height);

this 教程可能会对您有所帮助。

参考:https://www.ntu.edu.sg/home/ehchua/programming/java/J4b_CustomGraphics.html

答案 1 :(得分:0)

创建Canvas对象,然后使用它。

Canvas canvas = new Canvas(300, 250);
    GraphicsContext gc = canvas.getGraphicsContext2D();

    gc.fillOval(10, 60, 30, 30);
    gc.strokeOval(60, 60, 30, 30);