如何更改超类方法的参数?

huangapple go评论84阅读模式
英文:

How to change parameters for super class method?

问题

以下是翻译好的内容:

因此,我尝试将Canvas类的paint方法更改为接受一个数组作为参数之一。这是我尝试过的:

int[] array = createArray(10, 1, 10);

JFrame frame = new JFrame("My Drawing");
Canvas canvas = new Draw();
canvas.setSize(600, 600);
frame.add(canvas);
frame.pack();
frame.setVisible(true);
canvas.paint(canvas.getGraphics(), array);

这是Draw类的内容:

public class Draw extends Canvas {

    private static final long serialVersionUID = 1L;

    public void paint(Graphics g, int[] array) {
        for (int x = 0; x < array.length; x++) {
            g.fillRect(x, 0, 10, array[x]);
        }
    }
}

我尝试将canvas对象更改为Draw canvas = new Draw(),虽然没有出现错误,但在这样做时无法在画布上绘制任何内容。我还尝试使用@Override,但这不允许我向该方法添加任何额外的参数。

英文:

So I'm trying to change the paint method from the Canvas class to accept an array as one of the parameters. This is what I tried:

    int[] array = createArray(10, 1, 10);
	
	JFrame frame = new JFrame(&quot;My Drawing&quot;);
	Canvas canvas = new Draw();
	canvas.setSize(600, 600);
	frame.add(canvas);
	frame.pack();
	frame.setVisible(true);
	canvas.paint(canvas.getGraphics(), array);

and this is the Draw class:

public class Draw extends Canvas
{

/**Draws the graphs
 * 
 */

private static final long serialVersionUID = 1L;


public void paint(Graphics g, int[] array)
 {
	for(int x = 0; x&lt;array.length; x++)
	{
		g.fillRect(x, 0, 10, array[x]);
	}
 }

}

I tried changing the canvas object to Draw canvas = new Draw() and that didn't result in an error but it wasn't able to draw anything on the canvas when I did that. I also tried @Override but that doesn't allow me to add any extra parameters to the method.

答案1

得分: 2

<s>目前你的``new Draw()``被声明为一个``Canvas``对象。如果你将声明更改为``Draw canvas = new Draw();``,你就可以调用你的方法。</s>

编辑:
问题在于,``paint(Graphics g)``是由Swing在组件将由框架渲染时调用的内部方法。你的``paint(Graphics g, int[] array)``方法并不改变渲染过程。你希望在调用``paint(Graphics g)``之前提供用于创建绘图的参数。以下是一个非常简单的示例:

```java
public class Draw extends Canvas {

    private static final long serialVersionUID = 1L;

    private int[] array;

    public Draw(int[] array) {
        this.array = array;
    }

    @Override
    public void paint(Graphics g) {
        for (int x = 0; x < array.length; x++) {
            g.fillRect(x, 0, 10, array[x]);
        }
    }
}

然后你可以用Canvas draw = new Draw(array)来创建你的绘图。这个简单的示例适用于在Gui运行时绘图不需要显示不同的数组的情况(例如,如果通过按钮按下创建了一个新数组,并且你想要显示它)。


<details>
<summary>英文:</summary>

&lt;s&gt;Currently your ``new Draw()`` is declared as a ``Canvas`` object. If you change the declaration to ``Draw canvas = new Draw();`` you can call your method.&lt;/s&gt;

Edit:
The problem is, that ``paint(Graphics g)`` is an internal method called by Swing, when the component will be rendered by the framework. Your ``paint(Graphics g, int[] array)`` method doesn&#39;t change the render process. You want to provide the parameters for creating the Drawing before ``paint(Graphics g)`` is called. Here is an very simple example:

        public class Draw extends Canvas {

        private static final long serialVersionUID = 1L;

        private int[] array;

        public Draw(int[] array) {
            this.array = array;
        }

        @Override
        public void paint(Graphics g) {
            for (int x = 0; x &lt; array.length; x++) {
                g.fillRect(x, 0, 10, array[x]);
            }
        }
    }

And then you create your drawing with ``Canvas draw = new Draw(array)`` instead. This simple example works as long as the Drawing doesn&#39;t have to display a different array while the Gui is running (for example if a new array was created with a button press and you want to display it).

</details>



huangapple
  • 本文由 发表于 2020年3月4日 02:48:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/60513748.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定