如何修复闪烁/闪烁

时间:2014-04-28 16:31:10

标签: java swing graphics

每次点击都会计算的简单程序,但是点击太快时图像似乎闪烁/闪烁。

import java.awt.*;
import java.awt.event.*;
import java.awt.Graphics;
import javax.swing.*;
import java.lang.Object;
import java.net.URL;
import java.lang.String;
import java.awt.Font;


public class Spaceship extends Canvas
{
    private Font font1;
    int myX = 100;
    int myY = 400;
    int count = 0;

这些是闪现的2张图像,但使用的图像并不重要。

    Image img1 = Toolkit.getDefaultToolkit ().getImage ("spaceship.jpg");
    Image img2 = Toolkit.getDefaultToolkit ().getImage ("background.jpg");

    public Spaceship ()
    {
        setSize (new Dimension (1272, 960));
        addMouseListener (new MouseAdapter ()
        {
            public void mousePressed (MouseEvent evt)
            {
                moveIt (evt);
                count = count + 1;
            }
        }
        );
    }


    public void moveIt (MouseEvent evt)
    {
        myY = evt.getY () - 100;
        myX = evt.getX () - 150;
        update ();
    }


    public void update ()
    {
        repaint ();
    }

我认为这个问题在这里,但不确定如何修复它。

    public void paint (Graphics g)
    {
        font1 = new Font ("Serif", Font.BOLD, 36);            
        g.drawImage (img2, 0, 0, this);
        g.drawImage (img1, myX, myY, this);
        g.drawRect (1000, 0, 272, 96);
        g.drawRect (1000, 96, 272, 192);
        g.drawRect (1000, 192, 272, 288);
        g.drawRect (1000, 288, 272, 384);
        g.drawRect (1000, 384, 272, 480);
        g.drawRect (1000, 480, 272, 576);
        g.drawRect (1000, 576, 272, 672);
        g.drawRect (1000, 672, 272, 768);
        g.drawRect (1000, 768, 272, 864);
        g.drawRect (1000, 864, 272, 960);
        g.setFont (font1);
        g.drawString (Integer.toString (count), 500, 100);

    }

    public static void main (String[] args)
    {
        JFrame frame = new JFrame ("Basic Game");
        frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
        Spaceship ex = new Spaceship ();
        frame.getContentPane ().add (ex);
        frame.pack ();
        frame.setResizable (false);
        frame.setVisible (true);
        ex.requestFocus ();
    }
}

我做错了究竟是什么意思?

1 个答案:

答案 0 :(得分:3)

建议:

  • 首先,使用Swing而不是AWT,因为绘制时,Swing JComponents会给你自动双缓冲 - 缺少它可能会导致你的问题。
  • 您甚至不应该将AWT(Canvas)组件与Swing(JFrame)组件混合。再次,坚持使用所有Swing组件。
  • 使用JPanel的protected void paintComponent(Graphics g)方法绘制。
  • 别忘了在paintComponent覆盖中调用super方法。
  • 将背景绘制到BufferedImage中,然后在paintComponent方法中显示BufferedImage。

修改
一些有用的链接:


编辑2
你说:

  

让它完美运行,感谢您的帮助

非常好,欢迎您!既然你已经解决了,我觉得发布我的版本还行。随意提出任何问题或根据需要提出批评:

import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.awt.Graphics;

import javax.imageio.ImageIO;
import javax.swing.*;

import java.io.IOException;
import java.lang.Object;
import java.net.MalformedURLException;
import java.net.URL;
import java.lang.String;
import java.awt.Font;

public class Spaceship extends JPanel {
   private static final String BACKGROUND_PATH = "http://www.thatsreallypossible.com/"
         + "wp-content/uploads/2012/12/Space-Colonialisation.jpg";
   private static final String SPRITE_PATH = "http://www.pd4pic.com/"
         + "images250_/ufo-flying-saucer-spacecraft-spaceship-alien.png";

   private Font font1;
   int myX = 100;
   int myY = 400;
   int count = 0;
   private BufferedImage background;
   private BufferedImage sprite;

   public Spaceship() throws IOException {
      URL backgroundUrl = new URL(BACKGROUND_PATH);
      URL spriteUrl = new URL(SPRITE_PATH);
      background = ImageIO.read(backgroundUrl);
      sprite = ImageIO.read(spriteUrl);

      MouseAdapter myMouseAdapter = new MouseAdapter() {
         public void mousePressed(MouseEvent evt) {
            moveIt(evt);
            count = count + 1;
         }

         @Override
         public void mouseDragged(MouseEvent evt) {
            moveIt(evt);
         }
      };
      addMouseListener(myMouseAdapter);
      addMouseMotionListener(myMouseAdapter);
   }

   @Override
   public Dimension getPreferredSize() {
      if (background != null) {
         return new Dimension(background.getWidth(), background.getHeight());
      }
      return super.getPreferredSize();
   }

   public void moveIt(MouseEvent evt) {
      myY = evt.getY() - sprite.getHeight() / 2;
      myX = evt.getX() - sprite.getWidth() / 2;
      repaint();
   }


   @Override
   protected void paintComponent(Graphics g) {
      super.paintComponent(g);
      font1 = new Font("Serif", Font.BOLD, 36);
      g.drawImage(background, 0, 0, this);
      g.drawImage(sprite, myX, myY, this);
      g.setColor(Color.yellow);
      int rectCount = 10;
      //g.drawImage(img1, myX, myY, this);
      int height = getHeight() / rectCount;
      int width = 272;
      int x = getWidth() - width;
      for (int i = 0; i < rectCount; i++) {
         int y = i * height;
         g.drawRect(x, y, width, height);
      }
      g.setFont(font1);
      g.drawString(Integer.toString(count), 500, 100);
   }

   public static void main(String[] args) {
      JFrame frame = new JFrame("Basic Game");
      frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
      Spaceship ex;
      try {
         ex = new Spaceship();
         frame.getContentPane().add(ex);
         frame.pack();
         frame.setResizable(false);
         frame.setVisible(true);
         ex.requestFocus();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}