画绘制方法之外显卡 [英] draw graphics outside of paint method

查看:172
本文介绍了画绘制方法之外显卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

private void draw_shape() {                                         
    Graphics g = getGraphics();
    g.drawLine(0, 0, 100, 100);
    repaint();
}                                        

在paint方法只有那些图形绘制这是paint方法的一部分,因为它的
我想画paint方法之外的形状。
这code绘制线,但它立刻disappeares,我不明白为什么会这样。请大家帮忙

In paint method only those graphics are drawn which is a part of paint method because of which I wanted to draw shapes outside of paint method. This code draws the line but it immediately disappeares, I don't understand why this is happening. please help

推荐答案

该好好尝试一下工作,因为你所得到的电流图形中的秋千重绘线程。基本上是:

This doens't work because you are getting the current Graphics outside of the Swing repaint thread. Basically:


  • 您获得当前图形

  • 您画了东西

  • 然后调用重绘()将调用的paint()从而放弃你做的所有组件

  • you get the current Graphics
  • you draw something on it
  • then you call repaint() that will call the paint() of the component thus discarding all you did

要使它工作,你应该重写油漆的对象的paintComponent 的摆动)方法:

To make it work you should override the paint (paintComponent for Swing) method of your object:

@Override
public void paint(Graphics g) {
  super.paint(g); // if you have children to the component
  g.drawLine(..)
}

然后就叫重绘()当事情已被修改。

这篇关于画绘制方法之外显卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆