在JPanel上面怎么画图?
import java.awt.Color;import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Gamef extends JPanel{
int x = 100,y = 100;
JFrame f;
private final static int WIDTH = 680, HEIGHT = 600;
public static void main(String[] args) {
new Gamef().newFrame();
}
public void newFrame() {
f = new JFrame();
f.setBounds(500,120,WIDTH,HEIGHT);
f.setVisible(true);
f.setLayout(null);
f.setResizable(false);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Gamef p = new Gamef();
p.setBounds(1,0,360,560);
p.setBackground(Color.BLACK);
f.add(p);
}
public void paintComponent(Graphics g) {
Color c = g.getColor();
g.setColor(Color.RED);
g.fillRect(50,100,x,y);
g.setColor(c);
}
}
=======================================
在上面代码中不管我是重写paintComponent或者paint方法,结果图是画出来了,不过JPanel却不知道是没有出来还是隐藏了,不知道怎么作才能在JPanel上即画出图又能显示出JPanel的背景颜色来?
[ 本帖最后由 vecomwa 于 2009-11-16 21:51 编辑 ]