import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
public class cricle extends JFrame { int x=80,y=80;
public void launch(){
//setLocation(400, 300);
setSize(700,800);
setLocationRelativeTo(null); //jframe居中显示
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false); //jframe不可改变大小
setBackground(Color.YELLOW);
setVisible(true);
Thread t=new Thread(new RunTank());
t.start();
}
//画物体
public void paint(Graphics g){
Color c=g.getColor();
g.setColor(Color.red);
g.fillOval(x,y, 40, 40);
g.setColor(c);
y+=50;
}
//物体运动
class RunTank implements Runnable{ public void run() {
while(true) {
repaint();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) { cricle tan= new cricle();
tan.launch(); }
}
我是想实现一个物体的运动,模拟游戏人物的走动效果。
在这段代码中为什么这个物体之前的运动轨迹都会留下来?
import java.awt.Graphics;
import javax.swing.JFrame;
public class cricle extends JFrame { int x=80,y=80;
public void launch(){
//setLocation(400, 300);
setSize(700,800);
setLocationRelativeTo(null); //jframe居中显示
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false); //jframe不可改变大小
setBackground(Color.YELLOW);
setVisible(true);
Thread t=new Thread(new RunTank());
t.start();
}
//画物体
public void paint(Graphics g){
Color c=g.getColor();
g.setColor(Color.red);
g.fillOval(x,y, 40, 40);
g.setColor(c);
y+=50;
}
//物体运动
class RunTank implements Runnable{ public void run() {
while(true) {
repaint();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) { cricle tan= new cricle();
tan.launch(); }
}
我是想实现一个物体的运动,模拟游戏人物的走动效果。
在这段代码中为什么这个物体之前的运动轨迹都会留下来?
解决方案 »
- 用正则表达式如何表示合法的括号序列?
- Java 用awt包 中如何设置Button 大小?
- 请教一个网址有汉字的问题。
- 菜鸟送分问题。。。。关于缓存的问题
- 怎么在java中自动创建多级文件夹
- 一个输入输出问题请大家指导一下,谢谢哦!
- 在一个项目中如何使用Logger进行记录所有信息,然后保存至数据!
- 学java到了瓶颈了,应该怎么办?
- 变量退出作用域是会自动析构吗?
- jtree的addTreeExpansionListener、addTreeWillExpandListener、addTreeSelectionListener都是什么意思?
- 【求救】这个图用java怎么写。
- netbeans中怎么加滚动面板
public void paint(Graphics g){
super.paint(g);
.........................
}
就ok了。
这样不行啊。。
有个疑问:1.出现这种现象的原因是不是背景没有重刷,更新的原因?
2.如果这样的话,我在线程中调用了repaint()方法,而repaint()方法在jvm内部不是会自动update()更新操作,然后在paint()?
import java.awt.event.*;public class TankClient extends Frame {
int x = 50, y = 50;
public void paint(Graphics g) {
Color c = g.getColor();
g.setColor(Color.RED);
g.fillOval(x, y, 30, 30);
g.setColor(c);
y += 5;
} public void lauchFrame() {
this.setLocation(400, 300);
this.setSize(800, 600);
this.setTitle("TankWar");
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
this.setResizable(false);
this.setBackground(Color.GREEN);
setVisible(true);
new Thread(new PaintThread()).start();
} public static void main(String[] args) {
TankClient tc = new TankClient();
tc.lauchFrame();
}
private class PaintThread implements Runnable { public void run() {
while(true) {
repaint();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}}
这是马士兵老师写的,这段代码可以实现想要的效果我写的和这段有什么区别吗?
你所能用到
public void paint(Graphics g){
}
方法 是Compent自带的。。
你对界面所进行拖到 放大 缩小 等等操作等要触发paint这个方法
super.paint(g);就是相当于每次都重绘界面。。
repaint()方法不是会自动调用paint()方法吗?
public void update(Graphics g){
g.setColor(getBackground());
g.fillRect(getSize().getWidth(),getSize().getHeight());
g.setColor(getForeground());
paint(g);
}但有种更方便的做法,即使用swing包中的组件,因为这个包中的组件都实现了双缓冲。即改刷屏为贴图,可小小防止画面闪烁现象。
但面板就不要使用JFrame了,而是使用JComponent,这个组件虽然很原始,但希望你多研究研究,很有来头的!搞懂了这个组件,Java中的所有组件是怎么来的,他们的设计思想怎样的,都会明朗。