java代码实现小人跳舞 java小人跳舞动画

Java数据结构跳舞配对问题(队列的应用)

代码如下,可以直接运行。

创新互联建站主要为客户提供服务项目涵盖了网页视觉设计、VI标志设计、营销型网站建设、网站程序开发、HTML5响应式重庆网站建设公司手机网站制作设计、微商城、网站托管及成都网站维护、WEB系统开发、域名注册、国内外服务器租用、视频、平面设计、SEO优化排名。设计、前端、后端三个建站步骤的完善服务体系。一人跟踪测试的建站服务标准。已经为凿毛机行业客户提供了网站推广服务。

public static void main(String[] args) {

final int M = 6; // number of girls,可改动

final int N = 7; // number of boys,可改动

int x = 3;// some boy,可改动

int y = 5;// some girl,可改动

String result = "";// 记录结果,即第二个问题

// 初始化,假设队列存放男女生编号,从1开始

QueueInteger boys = new LinkedListInteger();

for (int i = 1; i = N; i++) {

boys.add(i);

}

QueueInteger girls = new LinkedListInteger();

for (int i = 1; i = M; i++) {

girls.add(i);

}

// 跳舞开始

int min = boys.size() girls.size() ? girls.size() : boys.size();

int k = 1;// songs

int count = 2; // 求出两个值,可改动

while (k 1000) {//为了不死循环,这里假设最多有999支舞蹈

System.out.println("***This is the " + k + "st dance:");

for (int i = 0; i min; i++) {

// 跳舞,第一个问题:输出每曲配对情况

System.out.println("Boy " + boys.peek() + " = Girl "

+ girls.peek());

// 跳过的排到对尾

int boy = boys.remove();

boys.add(boy);

int girl = girls.remove();

girls.add(girl);

// 判断 x和y跳舞了没有

if (boy == x girl == y) {

result += k + ",";

count--;

}

}

if (count == 0)

break;

// next dance

k++;

}

// 结果

if (count == 0)

System.out.println("\n***Boy " + x + " and Girl " + y

+ " dance together in : " + result);//第二个问题的解答,跳了哪几支舞

else

System.out.println("\n***Boy " + x + " and Girl " + y

+ " have no chance to dance!");//第二个问题的解答,两人没机会跳舞

}

在Java游戏中让一个人物走动的代码是什么?

代码主要为以下:

package com.lovo.game.frame;

import java.awt.Graphics;

import java.awt.Image;

import java.awt.MediaTracker;

import javax.swing.JFrame;

import com.lovo.game.role.Fire;

import com.lovo.game.role.GameMap;

import com.lovo.game.role.ZhaoYun;

import com.lovo.game.util.TrackerInit;

public class GameStartFrame extends JFrame implements Runnable{

private Image memoryImage;

private Graphics memoryGraphics;

public static boolean isRun = true;

private static GameMap gameMap = new GameMap();

private ZhaoYun zy = new ZhaoYun();

private Fire fire = new Fire();

public GameStartFrame(){

this.setSize(900,700);

this.setVisible(true);

this.setDefaultCloseOperation(3);

//设置居中

this.setLocationRelativeTo(null);

//初始化双缓冲画布、画笔

this.memoryImage = this.createImage(900,700);

this.memoryGraphics = this.memoryImage.getGraphics();

//媒体追踪器

MediaTracker tracker = new MediaTracker(this);

TrackerInit.initImage(tracker);

//启动线程

Thread th = new Thread(this);

th.start();

}

public static void main(String[] args) {

GameStartFrame game = new GameStartFrame();

}

public void run() {

while(isRun){

this.flushFrame();

try {

Thread.sleep(20);

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}

private void flushFrame(){

gameMap.drawMap(memoryGraphics);

zy.drawImage(memoryGraphics);

fire.drawImage(memoryGraphics);

//将双缓冲画布中的图片显示在窗体

this.getGraphics().drawImage(this.memoryImage,0,0,this);

}

}

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

package com.lovo.game.role;

import java.awt.Graphics;

import java.awt.Image;

public class GameMap {

public static Image mapImg;

public static int mapx;

public void drawMap(Graphics memoryGraphics){

mapx -=5;

if(mapx =-17100){

mapx = -17100;

}

memoryGraphics.drawImage(mapImg,mapx,0,null);

}

}

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

package com.lovo.game.role;

import java.awt.Graphics;

import java.awt.Image;

import com.lovo.game.util.MoveImageChange;

public class Fire {

public static Image[]fireImage;

private int x =100;

private int y =300;

private int width = 200;

private int height = 130;

private MoveImageChange moveChange = new MoveImageChange(3);

public void drawImage(Graphics memoryGraphics){

Image img = moveChange.imageChange(fireImage);

memoryGraphics.drawImage(img,this.x,this.y,this.width,this.height,null);

}

}

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

package com.lovo.game.util;

import java.awt.MediaTracker;

import com.lovo.game.role.Fire;

import com.lovo.game.role.GameMap;

import com.lovo.game.role.ZhaoYun;

public class TrackerInit {

public static void initImage(MediaTracker tracker){

//初始化地图图片

GameMap.mapImg = CutImage.getSingleImage("image/map.jpg", tracker);

//赵云

ZhaoYun.zyImage = CutImage.cutOneImage("image/boss/playSpear.png",18, 236, 134,tracker);

//火

Fire.fireImage = CutImage.cutOneImage("image/fireImg.png", 6, 283, 161,tracker);

try {

//0组的图片全部等待加载完毕后,在显示

tracker.waitForID(0);

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

package com.lovo.game.util;

import java.awt.Image;

import java.awt.MediaTracker;

import java.awt.image.CropImageFilter;

import java.awt.image.FilteredImageSource;

import java.awt.image.ImageProducer;

import javax.swing.ImageIcon;

import javax.swing.JFrame;

public class CutImage {

public static Image[][] cutManyImage(String filePath, int row, int col,

int imageWidth, int imageHight,MediaTracker tracker) {

Image[][] img = new Image[row][col];

ImageIcon imIcon = new ImageIcon(filePath);// 创建图像数组对象

Image imgTemp = imIcon.getImage();// 创建源图像

// 为源 图象获取ImageProducer源

ImageProducer sourceProducer = imgTemp.getSource();

for (int i = 0; i row; i++) {

for (int j = 0; j col; j++) {

// 创建图片分割图像对象,第一、二个参数为分割图像起始坐标。后两个参数为图像大小

CropImageFilter cropImg = new CropImageFilter(j * imageWidth, i * imageHight,imageWidth, imageHight);

ImageProducer imgProducer = new FilteredImageSource(sourceProducer, cropImg);

img[i][j] = new JFrame().createImage(imgProducer);

tracker.addImage(img[i][j], 0);

}

}

return img;

}

public static Image[] cutOneImage(String filePath,int col,

int imageWidth, int imageHight,MediaTracker tracker) {

Image[] img = new Image[col];

ImageIcon imIcon = new ImageIcon(filePath);// 创建图像数组对象

Image imgTemp = imIcon.getImage();// 创建源图像

// 为源 图象获取ImageProducer源

ImageProducer sourceProducer = imgTemp.getSource();

for (int j = 0; j col; j++) {

// 创建图片分割图像对象,第一、二个参数为分割图像起始坐标。后两个参数为图像大小

CropImageFilter cropImg = new CropImageFilter(j * imageWidth, 0,imageWidth, imageHight);

ImageProducer imgProducer = new FilteredImageSource(sourceProducer, cropImg);

img[j] = new JFrame().createImage(imgProducer);

tracker.addImage(img[j], 0);

}

return img;

}

public static Image getSingleImage(String imgPath,MediaTracker tracker){

Image img = new ImageIcon(imgPath).getImage();

tracker.addImage(img, 0);

return img;

}

}

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

package com.lovo.game.util;

import java.awt.Image;

public class MoveImageChange {

private int count;

private Image img;

private int frequency;

private int loopCount;

public MoveImageChange(int frequency){

this.frequency=frequency;

}

public MoveImageChange(int frequency,int count){

this.frequency=frequency;

this.count = count;

}

public Image imageChange(Image[] images){

if(img==null){//初始图片为第一张图片

img=images[0];

}

count++;

if(countfrequency){//当记数器大于频率时

count=0;

loopCount++;

if(loopCount = images.length){//图片下标越界时

loopCount=0;

}

img=images[loopCount];

}

return img;

}

public void setLoopCount(int loopCount){

this.loopCount = loopCount;

}

}

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

package com.lovo.game.role;

import java.awt.Graphics;

import java.awt.Image;

import com.lovo.game.util.MoveImageChange;

public class ZhaoYun {

public static Image[] zyImage;

private int x =600;

private int y =300;

private int width =200;

private int height =130;

private MoveImageChange moveChange = new MoveImageChange(3);

public void drawImage(Graphics memoryGraphics){

Image img = moveChange.imageChange(zyImage);

memoryGraphics.drawImage(img,this.x,this.y,this.width,this.height,null);

java中如何在窗体上插入一个随鼠标动眼睛的小人

我自己编了一个程序,有2个文件,我先说一下思路

用线程编程

监听鼠标移动事件,用线程不断获取鼠标坐标

根据鼠标坐标和眼球坐标来画出眼球的位置

package s0124随鼠标动眼睛;

//最初编写出问题的原因是没有明白把要做的事情放到run方法里面

import java.awt.BorderLayout;

import java.awt.Graphics;

import java.awt.event.MouseEvent;

import java.awt.event.MouseListener;

import java.awt.event.MouseMotionListener;

import javax.swing.ImageIcon;

import javax.swing.JFrame;

import javax.swing.JPanel;

public class Main extends JFrame{

   static int xx;

static int yy;

JPanel drawpanel;

Graphics g;

public static void main(String[] args) {

Main a=new Main();

}

public Main()

{

this.setTitle("随着鼠标动眼睛");

this.setSize(500,500);

this.setLocationRelativeTo(null);       //设置窗体出现位置居中  

this.setDefaultCloseOperation(3);      //设置窗体的关闭操作

this.setResizable(false);                      //设置禁止调整窗体的大小                              

this.setLayout(new BorderLayout());            //设置为边框布局

drawpanel=new JPanel();                        //新建一个画图面板

this.add(drawpanel,BorderLayout.CENTER);       //将画图面板添加到窗体的中间位置     

this.setVisible(true);                         //设置窗体可见

g=drawpanel.getGraphics();                     //得到画图面板的画笔,为了传递给监听器listener

MyThread mt=new MyThread(g,drawpanel);         //新建一个监听器,并把参数传递进去

drawpanel.addMouseMotionListener(mt);          //为画图面板添加监听对象

Thread r=new Thread(mt);                     

r.start();                                //启动线程

}

}

package s0124随鼠标动眼睛;

import java.awt.Graphics;

import java.awt.event.MouseEvent;

import java.awt.event.MouseMotionListener;

import javax.swing.JPanel;

import javax.swing.plaf.SliderUI;

public class MyThread implements Runnable,MouseMotionListener{

int x;

int y;

Graphics g;

JPanel drawpanel;

public MyThread(Graphics g,JPanel drawpanel)

{

this.g=g;

this.drawpanel=drawpanel;

}

public void run()

{    

while(true)

{

try {

Thread.sleep(10);

} catch (InterruptedException e) {

e.printStackTrace();

}

g.clearRect(0, 0, 600, 600);

g.drawOval(100, 100, 100, 100); 

g.drawOval(300, 100, 100, 100); 

//画左眼球

int x1=150+(x-150)/5;

int y1=150+(y-150)/5;

g.fillOval(x1-15,y1-15, 30,30);

//画右眼球

int x2=350+(x-350)/5;

int y2=150+(y-150)/5;

g.fillOval(x2-15,y2-15, 30,30);

g.drawLine(200, 300, 250, 250);

g.drawLine(250, 250,300,300);

g.drawLine(150, 350, 350, 350);

}

}

public void mouseDragged(MouseEvent e) {

}

public void mouseMoved(MouseEvent e) {

x=e.getX();

y=e.getY();

}

}

网站标题:java代码实现小人跳舞 java小人跳舞动画
网页网址:https://www.cdcxhl.com/article28/doiehcp.html

成都网站建设公司_创新互联,为您提供网页设计公司商城网站网站导航网站排名定制网站企业网站制作

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

成都网页设计公司