计数你点击每一次简单的程序,但是形象似乎点击太快时闪烁/闪光。
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;
这些都是flash的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 ();
}
}
究竟是什么,我做错了吗?
建议:
- 首先,使用Swing不是AWT,因为绘制iwth摇摆JComponents会给你自动双缓冲 - 缺乏这很可能导致您的问题。
- 你不应该,即使你的挥杆(JFrame的)组件来混合您的AWT(Canvas)的组成部分。 同样,只要坚持与所有Swing组件。
- 画在JPanel的
protected void paintComponent(Graphics g)
方法。 - 不要忘记调用父类的方法,在你的paintComponent覆盖。
- 做背景绘制成一个BufferedImage,然后显示在该BufferedImage中
paintComponent
方法。
编辑
一些有用的链接:
- 秋千一般信息和链接
- Swing指南
- 教训:执行自定义绘制
- 绘画在AWT和Swing
编辑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();
}
}
}