问题描述:

I am doing a self-study AP Java course and I just need some quick help. I have this issue with the paintComponent. I am always receiving messages to override it and create other things for it. This is my code for that class:

package pkghello;

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.Rectangle;

import javax.swing.JComponent;

/**

*

* @author ap

*/

public class RectangleComponent extends JComponent {

public void paintComponent (Graphics g) //It only highlights "paintComponent"

{

Graphics2D g2 = (Graphics2D) g;

//

Rectangle box = new Rectangle (5,10,20,30);

g2.draw(box);

box.translate(90, 90);

box.setSize(150,150);

g2.draw(box);

}

}

网友答案:

Two things. It probably wants you to add the @Override annotation and, unless you really need it to be otherwise, paintComponent should remain protected

For example.

@Override
protected void paintComponent (Graphics g)
相关阅读:
Top