![ここに画像の説明を入力してください][1]私は学校で Java プログラミングを習っている 1 年生で、本で少し先を見据えています。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);
-- スクリーン ショットを投稿するのに十分な担当者がいたら、私はそうするでしょう --