有人可以帮助解释这个java代码的问题 [英] Can someone help explain the problem with this java code
本文介绍了有人可以帮助解释这个java代码的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.geom.*;
public class Ball extends JPanel implements ActionListener,KeyListener {
private static final long serialVersionUID = -3622302774061996117L;
//created an obj for the class Timer
Timer t=new Timer(5,this);
// created the x y coordinates and made the velocity variables
int x=0,y=0,velX=0,velY=0;
//created the method and started the timer and added the KeyListener
public Ball() {
t.start();
addKeyListener(this);
setFocusable(true);
setFocusTraversalKeysEnabled(false);
}
//over rode the paint component method and created the obj that is gonna be controled by the arrow keys
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
this.setBackground(Color.WHITE);
g.setColor(new Color(66, 134, 244));
Graphics2D g2= (Graphics2D) g;
Ellipse2D circle=new Ellipse2D.Double(x,y,40,40);
g2.fill(circle);
}
//Over rode the action performed method and gave the obj the ability to move
@Override
public synchronized void actionPerformed(ActionEvent e) {
if(x<-40) {
velX=0;
x=600;
}
if(y<-40) {
velY=0;
y=600;
}
if(x>600) {
velX=0;
x=-40;
}
if(y>600) {
velY=0;
y=-40;
}
x+=velX;
y+=velY;
repaint();
}
//Over rode the key pressed method so as the user can control the obj by the arrow keys
@Override
public synchronized void keyPressed(KeyEvent e) {
int c=e.getKeyCode();
if(c==KeyEvent.VK_UP) {
velY=-3;
velX=0;
}
if(c==KeyEvent.VK_DOWN) {
velY=3;
velX=0;
}
if(c==KeyEvent.VK_RIGHT) {
velX=3;
velY=0;
}
if(c==KeyEvent.VK_LEFT) {
velX=-3;
velY=0;
}
}
//Over rode the key released method so as the obj would stop moving when the arrow keys are released
@Override
public void keyReleased(KeyEvent e) {
velY=0;
velX=0;
}
@Override
public void keyTyped(KeyEvent e) {}
//created the main window
public static void main(String [] args) {
JFrame frame=new JFrame("moving ball");
Ball b=new Ball();
frame.setSize(600,600);
frame.setVisible(true);
frame.setResizable(false);
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.add(b);
JOptionPane.showMessageDialog(null, "let's start\nif it doesn't work then re-run the program please", "message", JOptionPane.DEFAULT_OPTION);
}
}
现在这段代码应该可以在一个可以通过箭头键控制的窗口上查看cirlcle
它运作良好
但问题是当我运行它有时它不起作用球没有移动
当我重新运行它它完美的工作
有人可以给我一个解释和一些可能的修复
谢谢。
我试过的:
我不知道这个问题是什么,所以我希望你们帮助
now this code is supposed to view a cirlcle on a window that can be controled by the arrow keys
it works well
but the problem is when i run it sometimes it doesnt work where the ball doesn't move
when i rerun it it works perfectly
can someone give me an explation and some possible fixes
Thank you.
What I have tried:
I don't know whats the problem so i wish from you guys to help
推荐答案
下面的代码似乎有效。请参阅我已删除或添加了一些代码的注释行:
The code below seems to work. See my commented lines where I have removed or added some code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.geom.*;
public class Ball extends JPanel implements ActionListener,KeyListener {
private static final long serialVersionUID = -3622302774061996117L;
//created an obj for the class Timer
Timer t=new Timer(10,this);
// created the x y coordinates and made the velocity variables
int x=0,y=0,velX=0,velY=0;
//created the method and started the timer and added the KeyListener
public Ball() {
t.start();
addKeyListener(this);
// remove the following two lines here
// setFocusable(true);
// setFocusTraversalKeysEnabled(false);
}
//over rode the paint component method and created the obj that is gonna be controled by the arrow keys
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
this.setBackground(Color.WHITE);
g.setColor(new Color(66, 134, 244));
Graphics2D g2= (Graphics2D) g;
Ellipse2D circle=new Ellipse2D.Double(x,y,40,40);
g2.fill(circle);
}
//Over rode the action performed method and gave the obj the ability to move
@Override
public synchronized void actionPerformed(ActionEvent e) {
if(x<-40) {
velX=0;
x=600;
}
if(y<-40) {
velY=0;
y=600;
}
if(x>600) {
velX=0;
x=-40;
}
if(y>600) {
velY=0;
y=-40;
}
x+=velX;
y+=velY;
repaint();
}
//Over rode the key pressed method so as the user can control the obj by the arrow keys
@Override
public synchronized void keyPressed(KeyEvent e) {
int c=e.getKeyCode();
if(c==KeyEvent.VK_UP) {
velY=-3;
velX=0;
}
if(c==KeyEvent.VK_DOWN) {
velY=3;
velX=0;
}
if(c==KeyEvent.VK_RIGHT) {
velX=3;
velY=0;
}
if(c==KeyEvent.VK_LEFT) {
velX=-3;
velY=0;
}
}
//Over rode the key released method so as the obj would stop moving when the arrow keys are released
@Override
public synchronized void keyReleased(KeyEvent e) {
velY=0;
velX=0;
}
@Override
public void keyTyped(KeyEvent e) {}
//created the main window
public static void main(String [] args) {
JFrame frame=new JFrame("moving ball");
Ball b=new Ball();
frame.setSize(600,600);
frame.setVisible(true);
frame.setResizable(false);
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
// added the following three lines here
frame.setFocusable(true);
frame.setFocusTraversalKeysEnabled(false);
frame.addKeyListener(b);
frame.add(b);
}
}
这篇关于有人可以帮助解释这个java代码的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文