gpt4 book ai didi

java.awt.Shape 正在捉弄我

转载 作者:行者123 更新时间:2023-11-30 04:27:34 25 4
gpt4 key购买 nike

![在此处输入图像描述][1]我在学校学习 Java 编程,现在是第一年,我在书中对 future 做了一些展望。我遇到了一个具有 draw(Shape shape) 方法的类(称为 Canvas)。

出于某种原因,我无法弄清楚如何将任何形状绘制到 Canvas 上。我搜索了 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

这个列表会永远持续下去。

编辑:

这是类(class)的主要内容......非常简单的东西

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

-- 如果我有足够的代表来发布屏幕截图,我会 --

最佳答案

查看更多代码可能会有所帮助,但我猜测图形不是参数,您已经以某种方式自己创建了图形对象。来自 Canvas javadoc 的这个花絮应该可以帮助您:

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

我通常希望看到你对 Canvas 进行子类化,然后重写 paint() 像这样的东西......

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

}
}

通过这种方式,您可以获得属于 Canvas 的图形对象,并在其上绘制形状。我怀疑你正在做的是将其绘制到其他一些图形对象上(而不是属于 Canvas 的图形对象)。通常这意味着您在某处创建了图像,从中提取图形对象并将其绘制到图像中。但该图像可能不会被任何组件吸引...如果没有更多代码,很难确定。

您可以将 Canvas 放置在 UI 中任何您想要的位置,就像任何其他组件一样。

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

只要 Java 决定需要重绘 MyCanvas 对象,Java 就会调用 Paint 方法。您不需要调用paint。

希望有帮助!

关于java.awt.Shape 正在捉弄我,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15490025/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com