![在此处输入图片描述][1]我正在学校学习 Java 编程的第一年,我正在为我的书向前看一点。我遇到了一个Canvas
有方法的类(称为 )draw(Shape shape)
。
出于某种原因,我无法弄清楚如何将任何形状绘制到画布上。我已经搜索了 Java API,但我无法获得正确的语法。我很生气,因为我知道它非常简单。任何帮助将不胜感激。
这是我坚持使用的方法的代码:
/**
* Draw the outline of a given shape onto the canvas.
* @param shape the shape object to be drawn on the canvas
*/
public void draw(Shape shape)
{
graphic.draw(shape);
canvas.repaint();
}
当我从对象调用方法时,它给了我这样的东西:
canvas1.draw(->Shape shape<-)
我试过了:
java.awt.Shape circle
java.awt.Shape Circle
Circle circle
Shape circle
这个名单永远存在。
编辑:
这是班上的肉和土豆……很简单的东西
import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
/**
* Class Canvas - a class to allow for simple graphical
* drawing on a canvas.
*
* @author Michael Kölling (mik)
* @author Bruce Quig
*
* @version 2011.07.31
*/
public class Canvas
{
private JFrame frame;
private CanvasPane canvas;
private Graphics2D graphic;
private Color backgroundColor;
private Image canvasImage;
/**
* Create a Canvas with default height, width and background color
* (300, 300, white).
* @param title title to appear in Canvas Frame
*/
public Canvas(String title)
{
this(title, 300, 300, Color.white);
}
/**
* Create a Canvas with default background color (white).
* @param title title to appear in Canvas Frame
* @param width the desired width for the canvas
* @param height the desired height for the canvas
*/
public Canvas(String title, int width, int height)
{
this(title, width, height, Color.white);
}
/**
* Create a Canvas.
* @param title title to appear in Canvas Frame
* @param width the desired width for the canvas
* @param height the desired height for the canvas
* @param bgClour the desired background color of the canvas
*/
public Canvas(String title, int width, int height, Color bgColor)
{
frame = new JFrame();
canvas = new CanvasPane();
frame.setContentPane(canvas);
frame.setTitle(title);
canvas.setPreferredSize(new Dimension(width, height));
backgroundColor = bgColor;
frame.pack();
setVisible(true);
-- 如果我有足够的代表发布屏幕截图,我会 --