下面是我的程序的一部分
我希望使用timer每隔1秒便画一个刺并以同样的速度移动,
希望有人能帮我写出我所缺的部分
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
public class a extends JFrame{
public static void main(String[] args) {
// TODO Auto-generated method stub
a frame = new a();
jump jump = new jump();
frame.add(jump);
frame.setTitle("跳刺");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800, 250);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setResizable(false);
jump. setBackground(Color.white);
}
}
class jump extends JPanel {
//定义常量
final static int ball = 16;
final static int line = 20;
int move = 20;
int movespeed = 80;
int stab=80;
int refreshspeed =50 ;
int y1 = 186 ;
int a = 794 ;
//实时刷新
private Timer timer = new Timer(refreshspeed, new ActionListener() {
public void actionPerformed(ActionEvent e) {
repaint();
// System.out.print(a+" ");
}
});
//刺的移动
Timer timer5 = new Timer(stab, new ActionListener() {
public void actionPerformed(ActionEvent e) {
a-=10;
// System.out.print(1+" ");
}
});
//键盘控制
public jump(){
setFocusable(true);
this.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e){
if (e.getKeyCode() == KeyEvent.VK_SPACE) {
timer.start();
timer5.start();
}
}
});
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
//定义坐标
int x = getWidth()/2-20;
int y = getHeight();
//画刺
int t1 = y-40;
int t2 = a-line;
int t3 = a+line;
int t4 = a;
g.drawLine(t4, t1,t2, y);
g.drawLine(t4, t1,t3, y);
g.drawString("i ",t4,getHeight()/2);
}
}
------解决思路----------------------
在 public static void main(String[] args) 中,用
Task task = new Task();
java.util.Timer tr = new java.util.Timer();
tr.schedule(task, 1000, 1000);,可以使用Util的Timer,而原来的swing的Timer不变,,Task中调用jump。。
public void paintComponent(Graphics g) ,需要改哈,,你写的只能画一个而已。
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.TimerTask;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
public class A extends JFrame{
public static void main(String[] args) {
// TODO Auto-generated method stub
A frame = new A();
Task task = new Task();
task.getsA(frame);
java.util.Timer tr = new java.util.Timer();
// frame.add(jump);
tr.schedule(task, 1000, 1000);
}
}
class Task extends TimerTask{
A sA;
void getsA(A s){
this.sA = s;
}
public void run() {
jump jump = new jump();
sA.add(jump);
sA.setTitle("跳刺");
sA.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
sA.setSize(800, 250);
sA.setLocationRelativeTo(null);
sA.setVisible(true);
sA.setResizable(false);
jump. setBackground(Color.white);
/*
jump jump = new jump();
// jump jump = new jump();
sA.add(jump);
jump. setBackground(Color.white);*/
}
}
class jump extends JPanel {
//定义常量
final static int ball = 16;
final static int line = 20;
int move = 20;
int movespeed = 80;
int stab=80;
int refreshspeed =50 ;
int y1 = 186 ;
int a = 794 ;
//实时刷新
private Timer timer = new Timer(refreshspeed, new ActionListener() {
public void actionPerformed(ActionEvent e) {
repaint();
// System.out.print(a+" ");
}
});
//刺的移动
Timer timer5 = new Timer(stab, new ActionListener() {
public void actionPerformed(ActionEvent e) {
a-=10;
// System.out.print(1+" ");
}
});
//键盘控制
public jump(){
setFocusable(true);
this.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e){
if (e.getKeyCode() == KeyEvent.VK_SPACE) {
timer.start();
timer5.start();
}
}
});
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
//定义坐标
int x = getWidth()/2-20;
int y = getHeight();
//画刺
int t1 = y-40;
int t2 = a-line;
int t3 = a+line;
int t4 = a;
g.drawLine(t4, t1,t2, y);
g.drawLine(t4, t1,t3, y);
g.drawString("i ",t4,getHeight()/2);
}
}
Timer已经好了,自己改下画图的,你那个做标就好像不行,,建议你对刺写个类封装下,那样应该可以实现刺的重复出现