请大神看看,JFrame的双缓冲
不是说swing有双缓冲么,为什么这样不行啊
画的方块移动后是这个样子的
public class MainFrame extends JFrame implements KeyListener{
int x = 0;
int y = 0;
public void paint(Graphics g){
g.setColor(Color.red);
g.drawRect(x, y, 40, 30);
}
public void update(Graphics g) {
paint(g);
}
public void LunchFrame(){
this.setResizable(false);
this.setSize(512, 600);
this.setBackground(Color.black);
this.setTitle("飞机大战暴爽版! xsx");
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
this.setVisible(true);
this.addKeyListener(this);
new Thread(new PaintThread()).start();
}
public static void main(String[] args){
MainFrame mframe = new MainFrame();
mframe.LunchFrame();
}
private class PaintThread implements Runnable {
public void run() {
while(true) {
repaint();
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
int keyCode = e.getKeyCode();
//后面会添加具体按键匹配
switch (keyCode) {
case KeyEvent.VK_SPACE:
break;
case KeyEvent.VK_A:
System.out.println("xxsx");
x -= 1;
break;
case KeyEvent.VK_D:
x += 1;
break;
case KeyEvent.VK_W:
y -= 1;
break;
case KeyEvent.VK_S:
y += 1;
break;
}
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
}
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainPanel extends JPanel implements KeyListener {
private static final longserialVersionUID= 1147736728548400926L;
intx= 0;
inty= 0;
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.red);
g.drawRect(x, y, 40, 30);
}
public static void main(String[] args) {
JFrame f = new JFrame();
MainPanel mPanel = new MainPanel();
f.add(mPanel);
f.setResizable(false);
f.setSize(512, 600);
f.setBackground(Color.black);
f.setTitle("飞机大战暴爽版! xsx");
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setVisible(true);
mPanel.addKeyListener(mPanel);
mPanel.start();
mPanel.requestFocus();
}
private void start() {
new Thread(new PaintThread()).start();
}
private class PaintThread implements Runnable {
public void run() {
while (true) {
repaint();
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
switch (keyCode) {
case KeyEvent.VK_SPACE:
break;
case KeyEvent.VK_A:
System.out.println("xxsx");
x -= 1;
break;
case KeyEvent.VK_D:
x += 1;
break;
case KeyEvent.VK_W:
y -= 1;
break;
case KeyEvent.VK_S:
y += 1;
break;
}
}
@Override
public void keyReleased(KeyEvent e) {
}
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
//绘制天空背景
//绘制飞机
//绘制子弹
//绘制UI
//...
}
public interface IDrawable{
public void paint(Graphics g);
}
public class MainPanel extends JPanel implements KeyListener {
private static final List<IDrawable> drawableList = new LinkList<IDrawable>();
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
for(IDrawable drawable : drawableList){
drawable.paint(g);
}
}
}
然后你的飞机就可以是这样啦:
public class AirPlane implements IDrawable {
private int x;
private int y;
@Override
public void paint(Graphics g) {
g.setColor(Color.RED);
g.drawRect(x, y, 40, 30);
}
}