要是 swing 的button 的話
創(chuàng)新互聯主營云溪網站建設的網絡公司,主營網站建設方案,重慶APP開發(fā),云溪h5微信小程序開發(fā)搭建,云溪網站營銷推廣歡迎云溪等地區(qū)企業(yè)咨詢
setBounds
public void setBounds(int x,
int y,
int width,
int height)移動組件并調整其大小。由 x 和 y 指定左上角的新位置,由 width 和 height 指定新的大小。
參數:
x - 組件的新 x 坐標
y - 組件的新 y 坐標
width - 組件的新 width
height - 組件的新 height
button.setBonds(button.getBonds().getX(),button.getBonds().getY()-50,button.getBonds().getWidth
(),button.getBonds().getHeight() )
button.getBonds().getY()-50, 就是向上移動坐標的關鍵
代碼主要為以下:
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);// 創(chuàng)建圖像數組對象
Image imgTemp = imIcon.getImage();// 創(chuàng)建源圖像
// 為源 圖象獲取ImageProducer源
ImageProducer sourceProducer = imgTemp.getSource();
for (int i = 0; i row; i++) {
for (int j = 0; j col; j++) {
// 創(chuàng)建圖片分割圖像對象,第一、二個參數為分割圖像起始坐標。后兩個參數為圖像大小
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);// 創(chuàng)建圖像數組對象
Image imgTemp = imIcon.getImage();// 創(chuàng)建源圖像
// 為源 圖象獲取ImageProducer源
ImageProducer sourceProducer = imgTemp.getSource();
for (int j = 0; j col; j++) {
// 創(chuàng)建圖片分割圖像對象,第一、二個參數為分割圖像起始坐標。后兩個參數為圖像大小
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);
字符跳動好像不的行額...我只知道什么顏色啊!字體啊什么的或者加動態(tài)加音樂什么的!對了你可以這樣嘛!判斷文本的長度!如果長度在某個段內就調用Substring()截取字符串來設定大小顏色什么的!再創(chuàng)建幾個Icon ir= new ImageIcon(); 插入些制定找好的圖片 你看怎么樣~???加音樂如下! 可以這樣! 也是取值到某段就放MUSIC 通過Java提供的AudioClip類就可以播放了:
mid = java.applet.Applet.newAudioClip(this.getClass().getResource(midUrl[0]));mid.loop();
//循環(huán)mid.play();
//播放mid.stop();
//停止檢舉midUrl[0]是文件的路徑 不知道兄弟幫到你沒!哎!我也就這點功力了。嘿嘿
去學習A星尋路,可以得到最短路徑,其中也包括了繞開障礙物的代碼,然后就是動畫的圖片切換了,如果說要地圖跟隨主角移動,那個應該是滾屏操作,也就是說你主角往右走,超過窗口或者屏幕(全屏)坐標一半的時候,地圖整個往左移動,速度和主角的一樣就出來效果了。如果你看不懂A星的話,那咂就給你一段BFS的C++語言代碼,自己轉換成JAVA代碼寫法(就是改些關鍵字,有不少經典的游戲算法都來自C/C++)就可以了,這個是簡化版的A星尋路,一樣可以找到最近的路徑,你把path 這個路徑記錄下來再換算成像素位置就可以得到行走的具體步伐了...
#include?"stdafx.h"
#include?iostream
using?namespace?std;
const?int?rows?=?10;//行數
const?int?cols?=?10;//列數
const?int?nummax?=?4;//每一步,下一步可以走的方向:4個
//四種移動方向(左、右、上、下)對x、y坐標的影響
//x坐標:豎直方向,y坐標:水平方向
const?char?dx[nummax]?=?{0,0,-1,1};
const?char?dy[nummax]?=?{-1,1,0,0};
//障礙表
char?block[rows][cols]?=?{
0,1,0,0,0,0,0,0,0,0,
0,1,1,0,1,1,1,0,0,0,
0,0,0,0,0,0,0,0,0,0,
1,0,1,0,0,0,0,0,0,0,
0,0,0,0,0,0,1,1,1,0,
0,1,0,0,0,0,1,0,0,0,
0,0,0,0,0,0,1,1,0,1,
0,1,0,0,0,1,0,1,0,1,
0,1,1,1,0,0,0,1,0,1,
0,0,0,0,0,0,0,0,0,0,
};
char?block2[rows][cols]?=?{
0,1,0,0,0,0,0,0,0,0,
0,1,1,0,1,1,1,0,0,0,
0,0,0,0,0,0,0,0,0,0,
1,0,1,0,0,0,0,0,0,0,
0,0,0,0,0,0,1,1,1,0,
0,1,0,0,0,0,1,0,0,0,
0,0,0,0,0,0,1,1,0,1,
0,1,0,0,0,1,0,1,0,1,
0,1,1,1,0,0,0,1,0,1,
0,0,0,0,0,0,0,0,0,0,
};
char?path[rows][cols]?=?{0};//記錄路徑
int?startX?=?0,startY?=?0;//起始點坐標
int?endX?=?rows?-?1,endY?=?cols?-?1;//目標點坐標
//保存節(jié)點位置坐標的數據結構
typedef?struct?tagQNode{
char?x,y;
int?parentNode;//父節(jié)點索引
}QNode;
//打印路徑
void?printPath()
{
cout??""??endl;
for?(int?i?=?0;i??rows;++i)
{
for?(int?j?=?0;j??cols;++j)
{
if?(1?==?path[i][j])
{
cout??"♀";
}
else?if(block2[i][j]==0)
cout??"∷";
else?if(block2[i][j]==1)
cout??"■";
}
cout??endl;
}
cout??endl;
cout??endl;
}
void?BFS()
{
int?num?=?rows?*?cols;
//利用數組來模擬隊列
QNode?*queue?=?(QNode?*)malloc(num?*?sizeof(QNode));
//起始點入隊列
queue[0].x?=?queue[0].y?=?0;
queue[0].parentNode?=?-1;//起始點沒有父節(jié)點
int?front?=?0,rear?=?1;//隊列的頭和尾
while(front?!=?rear)//隊列不為空
{
for?(int?i?=?0;i??nummax;++i)
{
char?nextX,nextY;//下一步的坐標
nextX?=?queue[front].x?+?dx[i];
nextY?=?queue[front].y?+?dy[i];
//下一個節(jié)點可行
if?(nextX?=?0??nextX??rows???nextY?=?0??nextY??cols???0?==?block[nextX][nextY])
{
//尋找到目標點
if?(nextX?==?endX??nextY?==?endY)
{
//生成路徑
path[nextX][nextY]?=?1;
int?ParIn?=?front;
while(ParIn?!=?-1)
{
path[queue[ParIn].x][queue[ParIn].y]?=?1;
ParIn?=?queue[ParIn].parentNode;
}
//printPath();
}
//入棧
queue[rear].x?=?nextX;
queue[rear].y?=?nextY;
queue[rear].parentNode?=?front;
++rear;
//標記此點已被訪問
block[nextX][nextY]?=?1;
}
}
++front;
}
free(queue);
}
int?_tmain(int?argc,?_TCHAR*?argv[])
{
BFS();
printPath();
system("pause");
return?0;
}