代碼如下,可以直接運行。
創新互聯建站主要為客戶提供服務項目涵蓋了網頁視覺設計、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!");//第二個問題的解答,兩人沒機會跳舞
}
代碼主要為以下:
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);
我自己編了一個程序,有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小人跳舞動畫
網頁網址:http://vcdvsql.cn/article28/doiehcp.html
成都網站建設公司_創新互聯,為您提供網頁設計公司、商城網站、網站導航、網站排名、定制網站、企業網站制作
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯