java动画源代码 用java画一个动画的代码

求java小游戏源代码表1. CheckerDrag.java
// CheckerDrag.javaimport java.awt.*;import java.awt.event.*;public class CheckerDrag extends java.applet.Applet{// Dimension of checkerboard square.// 棋盘上每个小方格的尺寸final static int SQUAREDIM = 40;// Dimension of checkerboard -- includes black outline.// 棋盘的尺寸 – 包括黑色的轮廓线final static int BOARDDIM = 8 * SQUAREDIM2;// Dimension of checker -- 3/4 the dimension of a square.// 棋子的尺寸 – 方格尺寸的3/4final static int CHECKERDIM = 3 * SQUAREDIM / 4;// Square colors are dark green or white.// 方格的颜色为深绿色或者白色final static Color darkGreen = new Color (0, 128, 0);// Dragging flag -- set to true when user presses mouse button over checker// and cleared to false when user releases mouse button.// 拖动标记 --当用户在棋子上按下鼠标按键时设为truejava动画源代码,// 释放鼠标按键时设为falseboolean inDrag = false;// Left coordinate of checkerboard's upper-left corner.// 棋盘左上角的左方向坐标int boardx;// Top coordinate of checkerboard's upper-left corner.//棋盘左上角的上方向坐标int boardy;// Left coordinate of checker rectangle origin (upper-left corner).// 棋子矩形原点(左上角)的左方向坐标int ox;// Top coordinate of checker rectangle origin (upper-left corner).// 棋子矩形原点(左上角)的上方向坐标int oy;// Left displacement between mouse coordinates at time of press and checker// rectangle origin.// 在按键时的鼠标坐标与棋子矩形原点之间的左方向位移int relx;// Top displacement between mouse coordinates at time of press and checker// rectangle origin.// 在按键时的鼠标坐标与棋子矩形原点之间的上方向位移int rely;// Width of applet drawing area.// applet绘图区域的宽度int width;// Height of applet drawing area.// applet绘图区域的高度int height;// Image buffer.// 图像缓冲Image imBuffer;// Graphics context associated with image buffer.// 图像缓冲相关联的图形背景Graphics imG;public void init (){// Obtain the size of the applet's drawing area.// 获取applet绘图区域的尺寸width = getSize ().width;height = getSize ().height;// Create image buffer.// 创建图像缓冲imBuffer = createImage (width, height);// Retrieve graphics context associated with image buffer.// 取出图像缓冲相关联的图形背景imG = imBuffer.getGraphics ();// Initialize checkerboard's origin, so that board is centered.// 初始化棋盘的原点java动画源代码,使棋盘在屏幕上居中boardx = (width - BOARDDIM) / 21;boardy = (height - BOARDDIM) / 21;// Initialize checker's rectangle's starting origin so that checker is// centered in the square located in the top row and second column from// the left.// 初始化棋子矩形的起始原点java动画源代码,使得棋子在第一行左数第二列的方格里居中ox = boardxSQUAREDIM(SQUAREDIM - CHECKERDIM) / 21;oy = boardy(SQUAREDIM - CHECKERDIM) / 21;// Attach a mouse listener to the applet. That listener listens for// mouse-button press and mouse-button release events.// 向applet添加一个用来监听鼠标按键的按下和释放事件的鼠标监听器addMouseListener (new MouseAdapter (){public void mousePressed (MouseEvent e){// Obtain mouse coordinates at time of press.// 获取按键时的鼠标坐标int x = e.getX ();int y = e.getY ();// If mouse is over draggable checker at time// of press (i.e., contains (x, y) returns// true), save distance between current mouse// coordinates and draggable checker origin// (which will always be positive) and set drag// flag to true (to indicate drag in progress).// 在按键时如果鼠标位于可拖动的棋子上方// (也就是contains (x, y)返回true),则保存当前// 鼠标坐标与棋子的原点之间的距离(始终为正值)并且// 将拖动标志设为true(用来表明正处在拖动过程中)if (contains (x, y)){relx = x - ox;rely = y - oy;inDrag = true;}}boolean contains (int x, int y){// Calculate center of draggable checker.// 计算棋子的中心位置int cox = oxCHECKERDIM / 2;int coy = oyCHECKERDIM / 2;// Return true if (x, y) locates with bounds// of draggable checker. CHECKERDIM / 2 is the// radius.// 如果(x, y)仍处于棋子范围内则返回true// CHECKERDIM / 2为半径return (cox - x) * (cox - x)(coy - y) * (coy - y)CHECKERDIM / 2 * CHECKERDIM / 2;}public void mouseReleased (MouseEvent e){// When mouse is released, clear inDrag (to// indicate no drag in progress) if inDrag is// already set.// 当鼠标按键被释放时,如果inDrag已经为true,// 则将其置为false(用来表明不在拖动过程中)if (inDrag)inDrag = false;}});// Attach a mouse motion listener to the applet. That listener listens// for mouse drag events.//向applet添加一个用来监听鼠标拖动事件的鼠标运动监听器addMouseMotionListener (new MouseMotionAdapter (){public void mouseDragged (MouseEvent e){if (inDrag){// Calculate draggable checker's new// origin (the upper-left corner of// the checker rectangle).// 计算棋子新的原点(棋子矩形的左上角)int tmpox = e.getX () - relx;int tmpoy = e.getY () - rely;// If the checker is not being moved// (at least partly) off board,// assign the previously calculated// origin (tmpox, tmpoy) as the// permanent origin (ox, oy), and// redraw the display area (with the// draggable checker at the new// coordinates).// 如果棋子(至少是棋子的一部分)没有被// 移出棋盘,则将之前计算的原点// (tmpox, tmpoy)赋值给永久性的原点(ox, oy),// 并且刷新显示区域(此时的棋子已经位于新坐标上)if (tmpoxboardxtmpoyboardytmpoxCHECKERDIMboardxBOARDDIMtmpoyCHECKERDIMboardyBOARDDIM){ox = tmpox;oy = tmpoy;repaint ();}}}});}public void paint (Graphics g){// Paint the checkerboard over which the checker will be dragged.// 在棋子将要被拖动的位置上绘制棋盘paintCheckerBoard (imG, boardx, boardy);// Paint the checker that will be dragged.// 绘制即将被拖动的棋子paintChecker (imG, ox, oy);// Draw contents of image buffer.// 绘制图像缓冲的内容g.drawImage (imBuffer, 0, 0, this);}void paintChecker (Graphics g, int x, int y){// Set checker shadow color.// 设置棋子阴影的颜色g.setColor (Color.black);// Paint checker shadow.// 绘制棋子的阴影g.fillOval (x, y, CHECKERDIM, CHECKERDIM);// Set checker color.// 设置棋子颜色g.setColor (Color.red);// Paint checker.// 绘制棋子g.fillOval (x, y, CHECKERDIM - CHECKERDIM / 13, CHECKERDIM - CHECKERDIM / 13);}void paintCheckerBoard (Graphics g, int x, int y){// Paint checkerboard outline.// 绘制棋盘轮廓线g.setColor (Color.black);g.drawRect (x, y, 8 * SQUAREDIM1, 8 * SQUAREDIM1);// Paint checkerboard.// 绘制棋盘for (int row = 0; row8; row){g.setColor (((row1) != 0) ? darkGreen : Color.white);for (int col = 0; col8; col){g.fillRect (x1col * SQUAREDIM, y1row * SQUAREDIM,SQUAREDIM, SQUAREDIM);g.setColor ((g.getColor () == darkGreen) ? Color.white :darkGreen);}}}// The AWT invokes the update() method in response to the repaint() method// calls that are made as a checker is dragged. The default implementation// of this method, which is inherited from the Container class, clears the// applet's drawing area to the background color prior to calling paint().// This clearing followed by drawing causes flicker. CheckerDrag overrides// update() to prevent the background from being cleared, which eliminates// the flicker.// AWT调用java动画源代码了update()方法来响应拖动棋子时所调用的repaint()方法 。该方法从// Container类继承的默认实现会在调用paint()之前,将applet的绘图区域清除// 为背景色,这种绘制之后的清除就导致java动画源代码了闪烁 。CheckerDrag重写了update()来// 防止背景被清除,从而消除了闪烁 。public void update (Graphics g){paint (g);}}
怎么在电脑上运行Java源程序代码首先java动画源代码你要在你的电脑上安装jdk 。你可以在后面链接地址下载适合你自己的版本()java动画源代码,如果这个链接过期了 , 请在这个首先找一找 。
在你的电脑上配置java环境变量,主要是配置path和classpath 。你可以百度java环境变量配置,可以找到很多java环境变量配置方法 。配置完毕,可以在cmd窗口下用java -version来查看是否配置成功 。如果显示出java版本相关的信息表示配置成功 , 可以进行下一步了 。
编译你的源代码,cmd窗口下把路径改变(cd)到你源代码文件所在的路径,然后用javac 源文件名编译,例如javac Hello.java(需要注意的是源文件名需要是你文件public类的类名,如果你的文件有public类的话) 。当然你也可以不改变(cd)到源文件所在的路径 , 你的文件就需要加上绝对路径就可以了 。例如java动画源代码:javac e:\src\Hello.java.
运行你编译好的文件,java Hello(需要注意运行的时候没有后缀.java或者.class),同样你可以不改变路径用绝对路径运行 , 例如:java e:\src\Hello.如果你的代码中有窗口这样的类似的图形化界面,你就需要用javaw来运行 。
另外,你可以使用eclipse,NetBeans这样的集成开发环境(IDE)来写代码 , 这样方便很多 。
求JAVA源代码我用了半个小时 帮你写了一个简单的验证用户名和密码登陆问题 别辜负我的好意下面是代码!(建好包和类 代码粘过去就能用)
实体类 包entity
-------------------------------------------------------------
package entity;
/**
* 用户实体类
* @author new
*
*/
public class Users {
private String name;//用户名
private String pass;//用户密码
/**
* 空的构造函数 用户实力化 此类对象
*/
public Users(){
}
/**
* 构造函数 接收用户名和密码
* @param name
* @param pass
*/
public Users(String name, String pass) {
【java动画源代码 用java画一个动画的代码】this.name = name;
this.pass = pass;
}
/**
* 下面set和get方法就不用解释了吧
* @return
*/
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPass() {
return pass;
}
public void setPass(String pass) {
this.pass = pass;
}
}
数据库类包dao(我是模拟一下数据库 没有用到数据库)
--------------------------------------------------------------
package dao;
import java.util.*;
import entity.Users;//导入实体类
/**
* 模拟数据库 用户DAO
* @author new
*
*/
public class UsersDAO {
private static Users users=new Users();
static
{
users.setName("tom");
users.setPass("jerry");
}
/**
* 根据姓名查找这个用户 (模拟一下数据库)
* @param name
* @return
*/
public Users findUserByName(String name)
{
if(name.equals(this.users.getName()))
{
return this.users;
}
return null;
}
}
业务类 包service (验证用户名和密码)
------------------------------------------------------------
package service;
import dao.UsersDAO;
import entity.Users;
/**
* 验证密码 业务类
* @author new
*
*/
public class validatePass {
//实力化DAO对象
private UsersDAO us=new UsersDAO();
/**
* 验证输入的密码是否正确
* @param name
* @param pass
* @return
*/
public Users validate(String name,String pass)
{
Users user=null;
user=us.findUserByName(name);
//如果不为空 说明查到了
if(user!=null)
{
//用查询出来对象的密码和传过来的密码比较
if(user.getPass().equals(pass))
{
return user;
}
}
return null;
}
}
最后是测试test类 包test
----------------------------------------------------------
package test;
import entity.Users;
import service.validatePass;
/**
* 测试类
* @author new
*
*/
public class test {
/**
* main方法 用于测试
* @param args
*/
public static void main(String[] args)
{
//实例化业务类对象
validatePass v=new validatePass();
//用户名和密码
String name="tom";
String pass="jerry";
//开始验证
Users user=v.validate(name, pass);
if(user==null)
{
System.out.println("你输入的用户名或密码错误!");
}else
{
System.out.println("你已经通过验证 , 成功登陆!");
}
}
}
什么是java源代码 怎么查看你说的java源代码是指编译成的class文件前的java文件 。
当我们运行.java文件时,它会被系统编译成.class文件 , 例如Test.java编译之后就是Test.class,
源文件就是指Test.java文件,
一般部署项目时 , 有.class文件就可以发布运行了,但是如果想修改这个系统,.class是不能修改的,要有.java文件才能修改
也可以上网去下反编译软件,就是能把.class文件大部分还原成.java文件的工具,但不是100%还原 , 而且如果不是正版的,小心有毒?。?什么的 。
关于java动画源代码和用java画一个动画的代码的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站 。

    推荐阅读