简体   繁体   中英

java.awt.Shape is playing tricks on me

![enter image description here][1]I'm in my first year of Java programming at school and I'm looking ahead in my book a little bit. I've come across a class (called Canvas ) that has a draw(Shape shape) method.

For some reason I can't figure out how to get any shapes to draw onto the canvas. I've searched through the Java API and I can't get the syntax right. I'm pissed off because I know its something super simple. Any help would be greatly appreciated.

Here is the code for the method that I'm stuck with:

/**
 * 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();
}

When I call the method from the object it gives me something like this:

canvas1.draw(->Shape shape<-)

I've tried:

java.awt.Shape circle
java.awt.Shape Circle
Circle circle
Shape circle

The list goes on forever.

EDIT:

here's the meat and potatoes of the class... pretty straightfoward stuff

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

-- If I had enough rep to post a screen shot I would have --

It might help to see more of your code, but I'm guessing from the fact that graphics is not a parameter, that you have somehow created a graphics object on your own. This tidbit from the javadoc for canvas should help you out:

An application must subclass the Canvas class in order to get useful functionality such as creating a custom component.

I would normally expect to see you subclassing Canvas and then overriding paint() something like this...

public Class MyCanvas extends java.awt.Canvas {

  public void paint(Graphics g) {  
    super.paint(g);

    // some code to create the shape here... such as...
    // A rectangle with UL corner at 10,10, 30 wide, 50 high
    Shape myRectangle = new Rectangle2D.Float(10,10,30,50); 

    g.draw(myRectangle);

  }
}

This way you get the graphics object that belongs to the canvas, and draw the shape on to that. What I suspect you are doing is drawing it onto some other graphics object (not the one that belongs to the canvas). Usually this means that somewhere you've created an image, pulled a graphics object out of that and drawn into the image. But that image probably isn't getting drawn to any component... Hard to say for sure without more code.

You can put the canvas wherever you want in the UI just like any other component.

myFrame.add(new MyCanvas()); // example if you are adding it a frame called myFrame.

Java will call the paint method whenever Java decides it needs to redraw the MyCanvas object. You don't ever need to call paint.

Hope that helps!

Use, for example:

    drawShape(new java.awt.Rectangle(10, 10, 40, 40))

where a paramenter to pass in Bluej Method Call Window is

    new java.awt.Rectangle(10, 10, 40, 40)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM