簡體   English   中英

如何使用Canvas在Java中繪制圓

[英]How to draw a circle in Java using Canvas

我必須使用Java和canvas進行課程作業來構建彈球風格的游戲,但是我什至無法繪制圓,但出現以下錯誤:“無法從靜態引用非靜態方法fillCircle(int,int,int)上下文”,這是我當前擁有的代碼,否則將設置位置和直徑類並使其完美運行:

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

圖形類的繪制方法

// 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);

教程將為您提供幫助。

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

創建一個Canvas對象,然后使用它。

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM