我们专注攀枝花网站设计 攀枝花网站制作 攀枝花网站建设
成都网站建设公司服务热线:400-028-6601

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

用java画人代码 画人的代码

在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);

50分 java Graphics 画图代码补充

最大化图形消失的问题也解决了,注意类名首字母大写

import java.awt.*;

import java.awt.event.*;

import java.util.ArrayList;

import javax.swing.*;

public class Map {

public static void main(String[] args) {

new Draw();

}

}

class Draw extends JFrame{

ArrayListPoint points = new ArrayListPoint();

JPanel p=new JPanel();

Draw(){

add(p,BorderLayout.CENTER);

p.addMouseListener(new MyMouse());

this.addWindowStateListener(new MyWindow());

this.setResizable(true);

validate();

setBounds(50,50,500,400);

setVisible(true);

}

public void paint (Graphics g){

for(int i = 0;ipoints.size();i++){

g.fillOval(points.get(i).x, points.get(i).y, 30, 30);

}

}

private class MyWindow implements WindowStateListener{

public void windowStateChanged(WindowEvent arg0) {

repaint();

}

}

private class MyMouse extends MouseAdapter{

public void mouseClicked(MouseEvent e) {

points.add(e.getPoint());

repaint();

}

}

}

跪求大神荣光 回答下面的机器人的JAVA代码

import java.text.DecimalFormat;

import java.util.ArrayList;

import java.util.List;

import java.util.Scanner;

public class WalkDistance {

public static void main(String[] args) {

Scanner in = new Scanner(System.in);

int n = in.nextInt();

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

String order = in.next();

System.out.println(new DecimalFormat("0.00").format(calDistance(order)));

}

in.close();

}

private static float calDistance(String order) {

char[] orderChar = order.toCharArray();

ListString orderList = new ArrayListString();

int num = 0;

for (int i = 0, len = orderChar.length; i len; i++) {

if (orderChar[i] = '0' orderChar[i] = '9') {

num++;

} else {

if (num == 0) {

orderList.add(String.valueOf(orderChar[i]));

} else {

StringBuffer sb = new StringBuffer();

for (int j = num; j 0; j--) {

sb.append(String.valueOf(orderChar[i - j]));

}

orderList.add(sb.toString());

orderList.add(String.valueOf(orderChar[i]));

num = 0;

}

}

if (i == len - 1 num != 0) {

StringBuffer sb = new StringBuffer();

for (int j = num - 1; j = 0; j--) {

sb.append(String.valueOf(orderChar[i - j]));

}

orderList.add(sb.toString());

}

}

Point curPoint = new Point(0, 0, 90);

for (int i = 0, len = orderList.size(); i len; i++) {

if ("R".equals(orderList.get(i))) {

curPoint.angle = (curPoint.angle - 90 + 360) % 360;

} else if ("L".equals(orderList.get(i))) {

curPoint.angle = (curPoint.angle + 90) % 360;

} else {

curPoint.setLocation(curPoint, orderList.get(i));

}

}

return curPoint.getDis();

}

static class Point {

private int x;

private int y;

private int angle;

private float dis;

public Point(int x, int y, int angle) {

this.x = x;

this.y = y;

this.angle = angle;

this.dis = (float) 0;

}

public void setLocation(Point point, String distance) {

if (point.angle == 0) {

point.x += Integer.valueOf(distance);

} else if (point.angle == 90) {

point.y += Integer.valueOf(distance);

} else if (point.angle == 180) {

point.x -= Integer.valueOf(distance);

} else {

point.y -= Integer.valueOf(distance);

}

}

public float getDis() {

return (float) Math.sqrt(this.x * this.x + this.y * this.y);

}

}

}

用java代码写人生 int s = 人的生命的开始; int o = 人的生命的结束; fo

int 后面不能跟字符串。。并且for循环也不知道想要表达什么

public class Test {

public static void main(String[] args) {

double b = Math.random() * 50;

int a = (int) b + 50;

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

if (i == 0) {

System.out.println("出生!");

} else {

System.out.println(i + "岁了!");

}

}

}

}

如何用JAVA定义类-人(Human),根据Human类创建如图所示对象,并输入对象信息

public class Human{

public String _name;

public String _wugong;

Human(){_name="";_wugong="";}

Human(String name, String wugong){_name=name;_wugong=wugong;}

public static void main(String[] args) {

java.util.Scanner sc = new java.util.Scanner(System.in);

java.util.ArrayListHuman list = new java.util.ArrayListHuman();

String name, wugong;

System.out.println("输入人名和武功(输入人结束)");

while(true){

System.out.print("人名:");

name = sc.next();

boolean flag = false;

for(int i = 0; i list.size(); ++i) {

if(list.get(i)._name.equals(name)){

System.out.println("此人已经存在,请重新输入");

flag = true;

}

}

if(flag) continue;

if(name.equals("人")) break;

System.out.print("武功:");

wugong = sc.next();

list.add(new Human(name, wugong));

}

System.out.println("输入人查询,输入人名查询武功,输入exit退出");

while(true){

name = sc.next();

if(name.equals("exit")) break;

if(name.equals("人")) {

System.out.println("人名:");

for(int i = 0; i list.size(); ++i) {

System.out.println(list.get(i)._name);

}

} else {

boolean flag = false;

int k = 0;

for(int i = 0; i list.size(); ++i) {

if(list.get(i)._name.equals(name)) {flag = true; k = i;}

}

if(flag) System.out.println(name + "的武功为:" + list.get(k)._wugong);

else System.out.println("查无此人");

}

}

System.out.println("谢谢使用");

}

}

测试结果是:

输入人名和武功(输入人结束)

人名:黄蓉

武功:打狗棒法

人名:郭靖

武功:降龙十八掌

人名:小龙女

武功:玉女素心剑法

人名:杨过

武功:玉女素心剑法

人名:郭靖

此人已经存在,请重新输入

人名:人

输入人查询,输入人名查询武功,输入exit退出

人名:

黄蓉

郭靖

小龙女

杨过

黄蓉

黄蓉的武功为:打狗棒法

查无此人

exit

谢谢使用

求一个最简单的在JAVA上画图形的代码。

import java.awt.*;

class MyFrame extends Frame{

MyPanel p = new MyPanel();

public MyFrame(){

this.add(p);

this.setSize(300,300);

//省略了关闭窗口的代码

this.setVisible(true);

}

public static void main(String[] args){

new MyFrame();

}

class MyPanel extends Panel

{

public void paint(Graphics g){

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

}

}

}


网页名称:用java画人代码 画人的代码
标题来源:http://mswzjz.cn/article/docigss.html

其他资讯