java的新特性:我重写了run (),并在其中添加了一些内容;然后我重写了paintComponent(),并且我注意到put ()中的代码没有被运行。我是忘了做什么还是我只是无知?(我知道使用@重写是一种很好的做法,但这是其中的一天)
private Image dbImage;
private Graphics dbg;
public void paint(Graphics g)
dbImage = createImage(getWidth(), getHeight());
dbg = dbImage.getGraphics();
paintComponent(dbg);
g.drawImage(dbImage, 0, 0, this);
g.fillOval(0, 0, 10, 12);
}
public void paintComponent(Graphics g) {
setBackground(Color.CYAN);
double i = .25;
if (du) {
y -= i;
}
if (dr) {
x += i;
}
if (dd) {
y += i;
}
if (dl) {
x -= i;
}
if (x < 0) {
x = 0;
}
if (x > getWidth() - 25) {
x = getWidth() - 25;
}
if (y < 25) {
y = 25;
}
if (y > getHeight() - 25) {
y = getHeight() - 25;
}
g.drawOval( (int) x, (int) y, 25, 25);
repaint();
}
public static void main(String[] args) {
}在一个不相关的主题上: repaint()是做什么的?
发布于 2015-10-25 00:17:17
不要覆盖paint(),也不要直接调用绘图方法。摆动式喷绘机构将确保在适当的时间使用合适的绘画方法。
自定义绘画是通过重写paintComonent(...)的JPanel (或JComponent)来完成的。然后将面板添加到框架中。
有关更多信息和工作示例,请参阅Swing教程中的部分。
重新油漆()是做什么的?
它计划对组件进行重新绘制。RepaintManager将将重绘请求合并为单个画图请求,以提高绘制效率。
您不应该在绘图方法中调用repaint()。在更改类的属性时,可以在setter方法中调用repaint()。例如,使用setForeground()之类的方法,setBackground()将导致组件的重绘()。
java的新特性:
我建议您保留一个Swing教程的链接,方便您学习Swing基础知识。
https://stackoverflow.com/questions/33324680
复制相似问题