当前位置: 技术问答>java相关
如何更新我的.....(高分相送,我会再加贴子的)
来源: 互联网 发布时间:2015-05-16
本文导语: 各位,小弟最近用java来做了一个小动画 用的是paintComponent()方法画 在一个线程中调用Repaint方法重画 可是,重画时并不清除原有的内容,而是画出来有拖影,有时候甚至是 直接在原有的基础上添加,而不清除原有内...
各位,小弟最近用java来做了一个小动画
用的是paintComponent()方法画
在一个线程中调用Repaint方法重画
可是,重画时并不清除原有的内容,而是画出来有拖影,有时候甚至是
直接在原有的基础上添加,而不清除原有内容,
亲吻如何解决
原码:
package myprojects.model;
import java.awt.*;
import java.awt.geom.Rectangle2D;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.awt.event.*;
public class Model extends JFrame implements KeyListener
{
DrawLinePane pane = new DrawLinePane();
public Model() {
super("This is Cartoon");
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
System.exit(0);
}
});
setSize(300, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addKeyListener(this);
getContentPane().add(pane);
show(true);
}
public void keyTyped(KeyEvent e)
{
int i = 1;
}
public void keyReleased(KeyEvent e)
{
int i =1;
}
public void keyPressed(KeyEvent e)
{
if (e.getKeyCode() == KeyEvent.VK_UP){
pane.up();
}else if (e.getKeyCode() == KeyEvent.VK_DOWN){
pane.down();
}else if (e.getKeyCode() == KeyEvent.VK_LEFT){
pane.left();
}else if (e.getKeyCode() == KeyEvent.VK_RIGHT){
pane.right();
}
}
public static void main(String args[]) {
System.out.println("Starting Model...");
DrawLine line = new DrawLine();
}
}
class DrawLinePane extends JPanel implements Runnable
{
private Thread threadRunner = null;
double xPos = 0;
double yPos = 0;
double xMove = 1;
double yMove = 1;
DrawLinePane()
{
Thread threadRunner = new Thread(this);
threadRunner.start();
}
public void run()
{
while(true)
{
xPos += xMove;
yPos += yMove;
if(xPos > 300 || xPos 300 || yPos 300 || xPos 300 || yPos
用的是paintComponent()方法画
在一个线程中调用Repaint方法重画
可是,重画时并不清除原有的内容,而是画出来有拖影,有时候甚至是
直接在原有的基础上添加,而不清除原有内容,
亲吻如何解决
原码:
package myprojects.model;
import java.awt.*;
import java.awt.geom.Rectangle2D;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.awt.event.*;
public class Model extends JFrame implements KeyListener
{
DrawLinePane pane = new DrawLinePane();
public Model() {
super("This is Cartoon");
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
System.exit(0);
}
});
setSize(300, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addKeyListener(this);
getContentPane().add(pane);
show(true);
}
public void keyTyped(KeyEvent e)
{
int i = 1;
}
public void keyReleased(KeyEvent e)
{
int i =1;
}
public void keyPressed(KeyEvent e)
{
if (e.getKeyCode() == KeyEvent.VK_UP){
pane.up();
}else if (e.getKeyCode() == KeyEvent.VK_DOWN){
pane.down();
}else if (e.getKeyCode() == KeyEvent.VK_LEFT){
pane.left();
}else if (e.getKeyCode() == KeyEvent.VK_RIGHT){
pane.right();
}
}
public static void main(String args[]) {
System.out.println("Starting Model...");
DrawLine line = new DrawLine();
}
}
class DrawLinePane extends JPanel implements Runnable
{
private Thread threadRunner = null;
double xPos = 0;
double yPos = 0;
double xMove = 1;
double yMove = 1;
DrawLinePane()
{
Thread threadRunner = new Thread(this);
threadRunner.start();
}
public void run()
{
while(true)
{
xPos += xMove;
yPos += yMove;
if(xPos > 300 || xPos 300 || yPos 300 || xPos 300 || yPos