【问题标题】:image is drawn half second later then other paint compontents图像比其他油漆组件晚半秒绘制
【发布时间】:2016-11-01 15:08:23
【问题描述】:

在我启动我的小程序之后,除了我的背景图像延迟大约半秒来绘制之外,每个组件都被绘制好了。我删除了我的线程,认为这可能是我的问题的原因,但事实并非如此,所以我没有在此处包含它......我使用双缓冲,因为我会闪烁由线程重新绘制的组件。我试图提供尽可能少的代码......

public class balg extends Applet implements Runnable {


   private Image i;
   private Graphics doubleG;
   URL url;
   Image city;  //background image


   public void init(){
      setSize(800, 600);

      try{
          url = getDocumentBase();
      }catch(Exception e){

      }
      city = getImage(url , "multiplen/images/SPACE.png");
   }


   public void start(){

        Thread thread = new Thread(this);
        thread.start();

   }

   public void run(){

     // here goes the repiant();

   }

   public void stop(){


   }

   public void destroy(){


   }

   @Override
   public void update(Graphics g) {
      if(i == null){
           i = createImage(this.getSize().width, this.getSize().height);
           doubleG = i.getGraphics();
      }

      doubleG.setColor(getBackground());
      doubleG.fillRect(0, 0, this.getSize().width, this.getSize().height);

      doubleG.setColor(getForeground());
      paint(doubleG);

      g.drawImage(i, 0,0, this);
   }

   public void paint(Graphics g){

      g.drawImage(city,(int) 800 , 0 , this); // it's drawn here

      String s = "15";  
      g.setColor(Color.BLACK);
      g.drawString(s, getWidth() - 150, 50);    
   }

 }

【问题讨论】:

  • 我看到你正在声明一个新的Graphics 对象并传递给paint 方法。 update 方法已经传入了一个Graphics g 对象。您是否尝试过使用该对象而不是您声明的doubleG 变量来绘制您的组件?
  • 可以帮助您调试的东西,在每个 drawImagestart 调用之前(和之后)放置打印语句。你看到了什么?或者,使用调试器并逐步检查以确保您的代码以正确的顺序运行。
  • 我会 1) 将代码从 update 移动到 init 方法中。 2) 移除被覆盖的update 方法。 3) 将public void paint(Graphics g){ 更改为public void paint(Graphics g){ super.paint(g); ..
  • 1) 为什么要编写小程序?如果是老师指定的,请参考Why CS teachers should stop teaching Java applets。 2) 见Java Plugin support deprecatedMoving to a Plugin-Free Web。 ..
  • .. 3) 为什么使用 AWT?请参阅this answer,了解许多放弃 AWT 使用支持 Swing 的组件的充分理由。

标签: java image graphics applet awt


【解决方案1】:

读取图像需要这么长的时间,大约 100-200 毫秒。

【讨论】:

    猜你喜欢
    • 2018-11-23
    • 1970-01-01
    • 1970-01-01
    • 2013-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多