java画出一个小圆,并能用键盘控制方向的代码
2个回答
展开全部
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Ellipse2D;
import javax.swing.*;
public class DrawCircle extends JFrame{
Ellipse2D circle;
public DrawCircle(){
super("Moving Circle");
circle = new Ellipse2D.Double(200,200,100,100);
this.addKeyListener(new myKeyListener(this));
this.setSize(500,500);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public void paint(Graphics g){
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.white);
g2.fillRect(0,0,this.getWidth(),this.getHeight());
g2.setColor(Color.blue);
g2.fill(circle);
}
public static void main(String args[]){
DrawCircle d = new DrawCircle();
}
class myKeyListener extends KeyAdapter{
JFrame frame;
public myKeyListener(JFrame frame){
this.frame = frame;
}
public void keyPressed(KeyEvent e){
switch(e.getKeyCode()){
case KeyEvent.VK_UP:
circle.setFrame(circle.getX(),circle.getY()-10,circle.getHeight(),circle.getWidth());
break;
case KeyEvent.VK_DOWN:
circle.setFrame(circle.getX(),circle.getY()+10,circle.getHeight(),circle.getWidth());
break;
case KeyEvent.VK_LEFT:
circle.setFrame(circle.getX()-10,circle.getY(),circle.getHeight(),circle.getWidth());
break;
case KeyEvent.VK_RIGHT:
circle.setFrame(circle.getX()+10,circle.getY(),circle.getHeight(),circle.getWidth());
break;
}
frame.repaint();
}
}
}
辛辛苦苦给你做的... 不是复制的哈...
import java.awt.event.*;
import java.awt.geom.Ellipse2D;
import javax.swing.*;
public class DrawCircle extends JFrame{
Ellipse2D circle;
public DrawCircle(){
super("Moving Circle");
circle = new Ellipse2D.Double(200,200,100,100);
this.addKeyListener(new myKeyListener(this));
this.setSize(500,500);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public void paint(Graphics g){
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.white);
g2.fillRect(0,0,this.getWidth(),this.getHeight());
g2.setColor(Color.blue);
g2.fill(circle);
}
public static void main(String args[]){
DrawCircle d = new DrawCircle();
}
class myKeyListener extends KeyAdapter{
JFrame frame;
public myKeyListener(JFrame frame){
this.frame = frame;
}
public void keyPressed(KeyEvent e){
switch(e.getKeyCode()){
case KeyEvent.VK_UP:
circle.setFrame(circle.getX(),circle.getY()-10,circle.getHeight(),circle.getWidth());
break;
case KeyEvent.VK_DOWN:
circle.setFrame(circle.getX(),circle.getY()+10,circle.getHeight(),circle.getWidth());
break;
case KeyEvent.VK_LEFT:
circle.setFrame(circle.getX()-10,circle.getY(),circle.getHeight(),circle.getWidth());
break;
case KeyEvent.VK_RIGHT:
circle.setFrame(circle.getX()+10,circle.getY(),circle.getHeight(),circle.getWidth());
break;
}
frame.repaint();
}
}
}
辛辛苦苦给你做的... 不是复制的哈...
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
package Conte0;
//引入java包
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Move extends JFrame {
/**
* @param args
* @author lovefang
* @功能:控制小球移动
*/
MyPanel2 mp=null;
public static void main(String[] args) {
// TODO Auto-generated method stub
Move move=new Move();
}
//构造
public Move(){
mp=new MyPanel2();
//添加组件
this.add(mp);
this.addKeyListener(mp);
//定义窗体
this.setTitle("小球移动");
this.setSize(400, 300);
this.setResizable(false);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
//定义自己的面板
class MyPanel2 extends JPanel implements KeyListener{
int x=10;
int y=10;
public void paint(Graphics g){
super.paint(g);
g.fillOval(x, y, 10, 10);
}
//键被按下
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
if(e.getKeyCode()==KeyEvent.VK_DOWN){
y++;
}
else if(e.getKeyCode()==KeyEvent.VK_UP){
y--;
}
else if(e.getKeyCode()==KeyEvent.VK_LEFT){
x--;
}
else if(e.getKeyCode()==KeyEvent.VK_RIGHT){
x++;
}
//用WASD控制
else if(e.getKeyCode()==KeyEvent.VK_S){
y++;
}
else if(e.getKeyCode()==KeyEvent.VK_W){
y--;
}
else if(e.getKeyCode()==KeyEvent.VK_A){
x--;
}
else if(e.getKeyCode()==KeyEvent.VK_D){
x++;
}
this.repaint();
}
//键被松开
public void keyReleased(KeyEvent arg0) {
// TODO Auto-generated method stub
}
//键的一个值被输入
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub
}
}
//引入java包
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Move extends JFrame {
/**
* @param args
* @author lovefang
* @功能:控制小球移动
*/
MyPanel2 mp=null;
public static void main(String[] args) {
// TODO Auto-generated method stub
Move move=new Move();
}
//构造
public Move(){
mp=new MyPanel2();
//添加组件
this.add(mp);
this.addKeyListener(mp);
//定义窗体
this.setTitle("小球移动");
this.setSize(400, 300);
this.setResizable(false);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
//定义自己的面板
class MyPanel2 extends JPanel implements KeyListener{
int x=10;
int y=10;
public void paint(Graphics g){
super.paint(g);
g.fillOval(x, y, 10, 10);
}
//键被按下
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
if(e.getKeyCode()==KeyEvent.VK_DOWN){
y++;
}
else if(e.getKeyCode()==KeyEvent.VK_UP){
y--;
}
else if(e.getKeyCode()==KeyEvent.VK_LEFT){
x--;
}
else if(e.getKeyCode()==KeyEvent.VK_RIGHT){
x++;
}
//用WASD控制
else if(e.getKeyCode()==KeyEvent.VK_S){
y++;
}
else if(e.getKeyCode()==KeyEvent.VK_W){
y--;
}
else if(e.getKeyCode()==KeyEvent.VK_A){
x--;
}
else if(e.getKeyCode()==KeyEvent.VK_D){
x++;
}
this.repaint();
}
//键被松开
public void keyReleased(KeyEvent arg0) {
// TODO Auto-generated method stub
}
//键的一个值被输入
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询