2013-09-23 85 views
-1

有人可以帮助我的评论行,除了它要求删除的东西。谢谢!线程帮助。油漆

public class ex1011c extends JApplet implements ActionListener 
{ 
     // get rid of all winkbutton code 

    JButton winkbutton = new JButton("Wink At You"); 
    boolean wink = false, first = true; 
    Container c; 

    public void init() 
    { 
     c = getContentPane(); 
     c.setLayout(new FlowLayout()); 
     c.setBackground(Color.blue); 
     winkbutton.setForeground(Color.cyan); 
     c.add(winkbutton); 
     winkbutton.addActionListener(this); 
    } 

     // get rid of actionPerformed 

    public void actionPerformed(ActionEvent e) 
    { 
     wink = !wink; 
     repaint(); 
    } 

    public void paint(Graphics g) 
    { 
     /* if first time, draw the face and non winking eye, 
      set first to false */ 

     super.paint(g); 
     g.setColor(Color.yellow); 
     g.fillOval(50, 50, 100, 100); 
     g.setColor(Color.black); 
     g.fillOval(85, 80, 10, 20); 

     /* cover just the eye that winks (if winking or not, but do not 
     cover anything else), switch the wink boolean */ 

     // draw the full eye or winking eye 

     if (wink) 
      g.fillOval(105, 88, 10, 5); 
     else 
      g.fillOval(105, 80, 10, 20); 

     // go to sleep for a second 

     // call repaint 
    } 

     // override update to lesson flicker 

} 
+2

什么问题? – raffian

+0

不要在绘画事件中放置逻辑。相反,使用计时器并存储状态。 – SLaks

+0

@raffian整个代码中都有注释行。我困在他们身上。我会很感激任何帮助 – user2805435

回答

2

简单,不要睡觉/暂停/阻止或以其他方式阻止事件调度线程。

EDT是负责处理绘画请求的东西,任何阻止其运行的东西(如Thread.sleep)都会阻止它更新屏幕。

请记住,仅仅因为您将某些东西绘制到Graphics上下文中,并不意味着它会呈现给输出。

相反,使用javax.swing.Timer

Concurrency in SwingPerforming Custom PaintingPainting in AWT and Swing看看更多的细节。

我还强烈建议您不要重写顶级容器的任何paint方法,如JApplet。相反,使用类似JPanel的东西,并改写它的paintComponent方法。

除了饮用性,您将获得其中顶层容器没有双缓冲的好处...

例如...

随着简单示例

enter image description here

import java.awt.BorderLayout; 
import java.awt.Dimension; 
import java.awt.EventQueue; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.Timer; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 

public class Blinky { 

    public static void main(String[] args) { 
     new Blinky(); 
    } 

    public Blinky() { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 
       } 

       JFrame frame = new JFrame("Testing"); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.setLayout(new BorderLayout()); 
       frame.add(new BlinkPane()); 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 
     }); 
    } 

    public class BlinkPane extends JPanel { 

     private boolean wink; 

     public BlinkPane() { 
      Timer timer = new Timer(1000, new ActionListener() { 
       @Override 
       public void actionPerformed(ActionEvent e) { 
        wink = !wink; 
        repaint(); 
       } 
      }); 
      timer.start(); 
     } 

     @Override 
     public Dimension getPreferredSize() { 
      return new Dimension(200, 200); 
     } 

     @Override 
     protected void paintComponent(Graphics g) { 
      super.paintComponent(g); 
      Graphics2D g2d = (Graphics2D) g.create(); 
      int x = (getWidth() - 10)/2; 
      int eyeHeight = 20; 
      if (wink) { 
       eyeHeight = 5; 
      } 
      int y = (getWidth() - eyeHeight)/2; 
      g.fillOval(x, y, 10, eyeHeight); 
      g2d.dispose(); 
     } 
    } 
} 
+0

我将如何减少闪烁? – user2805435

+0

使用'JPanel'(覆盖它的'paintComponent'方法,你有自定义绘画那里),将它添加到你的小程序,闪烁不见了... – MadProgrammer