酷炫的java代码,优雅代码java

JAVA程序设计,多线程,求大神给一份可运行的代码

给你一个经典的例子。run里面放空循环来观察多线程是不合理的,空循环消耗时序极小,用sleep来间隔时间才是合理的。

创新互联-成都网站建设公司,专注成都网站建设、成都网站制作、网站营销推广,申请域名网站空间网站托管运营有关企业网站制作方案、改版、费用等问题,请联系创新互联

class RunnableDemo implements Runnable {

private Thread t;

private String threadName;

RunnableDemo( String name) {

threadName = name;

System.out.println("Creating " +  threadName );

}

public void run() {

System.out.println("Running " +  threadName );

try {

for(int i = 4; i  0; i--) {

System.out.println("Thread: " + threadName + ", " + i);

// Let the thread sleep for a while.

Thread.sleep(50);

}

}catch (InterruptedException e) {

System.out.println("Thread " +  threadName + " interrupted.");

}

System.out.println("Thread " +  threadName + " exiting.");

}

public void start () {

System.out.println("Starting " +  threadName );

if (t == null) {

t = new Thread (this, threadName);

t.start ();

}

}

}

public class TestThread {

public static void main(String args[]) {

RunnableDemo R1 = new RunnableDemo( "Thread-1");

R1.start();

RunnableDemo R2 = new RunnableDemo( "Thread-2");

R2.start();

}   

}

求一个简单又有趣的JAVA小游戏代码

具体如下:

连连看的小源码

package Lianliankan;

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

public class lianliankan implements ActionListener

{

JFrame mainFrame; //主面板

Container thisContainer;

JPanel centerPanel,southPanel,northPanel; //子面板

JButton diamondsButton[][] = new JButton[6][5];//游戏按钮数组

JButton exitButton,resetButton,newlyButton; //退出,重列,重新开始按钮

JLabel fractionLable=new JLabel("0"); //分数标签

JButton firstButton,secondButton; //

分别记录两次62616964757a686964616fe59b9ee7ad9431333335326239被选中的按钮

int grid[][] = new int[8][7];//储存游戏按钮位置

static boolean pressInformation=false; //判断是否有按钮被选中

int x0=0,y0=0,x=0,y=0,fristMsg=0,secondMsg=0,validateLV; //游戏按钮的位置坐标

int i,j,k,n;//消除方法控制

代码(code)是程序员用开发工具所支持的语言写出来的源文件,是一组由字符、符号或信号码元以离散形式表示信息的明确的规则体系。

对于字符和Unicode数据的位模式的定义,此模式代表特定字母、数字或符号(例如 0x20 代表一个空格,而 0x74 代表字符“t”)。一些数据类型每个字符使用一个字节;每个字节可以具有 256 个不同的位模式中的一个模式。

在计算机中,字符由不同的位模式(ON 或 OFF)表示。每个字节有 8 位,这 8 位可以有 256 种不同的 ON 和 OFF 组合模式。对于使用 1 个字节存储每个字符的程序,通过给每个位模式指派字符可表示最多 256 个不同的字符。2 个字节有 16 位,这 16 位可以有 65,536 种唯一的 ON 和 OFF 组合模式。使用 2 个字节表示每个字符的程序可表示最多 65,536 个字符。

单字节代码页是字符定义,这些字符映射到每个字节可能有的 256 种位模式中的每一种。代码页定义大小写字符、数字、符号以及 !、@、#、% 等特殊字符的位模式。每种欧洲语言(如德语和西班牙语)都有各自的单字节代码页。

虽然用于表示 A 到 Z 拉丁字母表字符的位模式在所有的代码页中都相同,但用于表示重音字符(如"é"和"á")的位模式在不同的代码页中却不同。如果在运行不同代码页的计算机间交换数据,必须将所有字符数据由发送计算机的代码页转换为接收计算机的代码页。如果源数据中的扩展字符在接收计算机的代码页中未定义,那么数据将丢失。

如果某个数据库为来自许多不同国家的客户端提供服务,则很难为该数据库选择这样一种代码页,使其包括所有客户端计算机所需的全部扩展字符。而且,在代码页间不停地转换需要花费大量的处理时间。

谁有java写的俄罗斯方块代码,要自己写的。跪求。急。

俄罗斯方块——java源代码提供importjava.awt.*;importjava.awt.event.*;//俄罗斯方块类publicclassERS_BlockextendsFrame{publicstaticbooleanisPlay=false;publicstaticintlevel=1,score=0;publicstaticTextFieldscoreField,levelField;publicstaticMyTimertimer;GameCanvasgameScr;publicstaticvoidmain(String[]argus){ERS_Blockers=newERS_Block("俄罗斯方块游戏V1.0Author:Vincent");WindowListenerwin_listener=newWinListener();ers.addWindowListener(win_listener);}//俄罗斯方块类的构造方法ERS_Block(Stringtitle){super(title);setSize(600,480);setLayout(newGridLayout(1,2));gameScr=newGameCanvas();gameScr.addKeyListener(gameScr);timer=newMyTimer(gameScr);timer.setDaemon(true);timer.start();timer.suspend();add(gameScr);PanelrightScr=newPanel();rightScr.setLayout(newGridLayout(2,1,0,30));rightScr.setSize(120,500);add(rightScr);//右边信息窗体的布局MyPanelinfoScr=newMyPanel();infoScr.setLayout(newGridLayout(4,1,0,5));infoScr.setSize(120,300);rightScr.add(infoScr);//定义标签和初始值Labelscorep=newLabel("分数:",Label.LEFT);Labellevelp=newLabel("级数:",Label.LEFT);scoreField=newTextField(8);levelField=newTextField(8);scoreField.setEditable(false);levelField.setEditable(false);infoScr.add(scorep);infoScr.add(scoreField);infoScr.add(levelp);infoScr.add(levelField);scorep.setSize(newDimension(20,60));scoreField.setSize(newDimension(20,60));levelp.setSize(newDimension(20,60));levelField.setSize(newDimension(20,60));scoreField.setText("0");levelField.setText("1");//右边控制按钮窗体的布局MyPanelcontrolScr=newMyPanel();controlScr.setLayout(newGridLayout(5,1,0,5));rightScr.add(controlScr);//定义按钮playButtonplay_b=newButton("开始游戏");play_b.setSize(newDimension(50,200));play_b.addActionListener(newCommand(Command.button_play,gameScr));//定义按钮LevelUPButtonlevel_up_b=newButton("提高级数");level_up_b.setSize(newDimension(50,200));level_up_b.addActionListener(newCommand(Command.button_levelup,gameScr));//定义按钮LevelDownButtonlevel_down_b=newButton("降低级数");level_down_b.setSize(newDimension(50,200));level_down_b.addActionListener(newCommand(Command.button_leveldown,gameScr));//定义按钮LevelPauseButtonpause_b=newButton("游戏暂停");pause_b.setSize(newDimension(50,200));pause_b.addActionListener(newCommand(Command.button_pause,gameScr));//定义按钮QuitButtonquit_b=newButton("退出游戏");quit_b.setSize(newDimension(50,200));quit_b.addActionListener(newCommand(Command.button_quit,gameScr));controlScr.add(play_b);controlScr.add(level_up_b);controlScr.add(level_down_b);controlScr.add(pause_b);controlScr.add(quit_b);setVisible(true);gameScr.requestFocus();}}//重写MyPanel类,使Panel的四周留空间classMyPanelextendsPanel{publicInsetsgetInsets(){returnnewInsets(30,50,30,50);}}//游戏画布类classGameCanvasextendsCanvasimplementsKeyListener{finalintunitSize=30;//小方块边长introwNum;//正方格的行数intcolumnNum;//正方格的列数intmaxAllowRowNum;//允许有多少行未削intblockInitRow;//新出现块的起始行坐标intblockInitCol;//新出现块的起始列坐标int[][]scrArr;//屏幕数组Blockb;//对方快的引用//画布类的构造方法GameCanvas(){rowNum=15;columnNum=10;maxAllowRowNum=rowNum-2;b=newBlock(this);blockInitRow=rowNum-1;blockInitCol=columnNum/2-2;scrArr=newint[32][32];}//初始化屏幕,并将屏幕数组清零的方法voidinitScr(){for(inti=0;i=rowNum||col=columnNum)return(-1);elsereturn(scrArr[row][col]);}//返回新块的初始行坐标方法publicintgetInitRow(){return(blockInitRow);//返回新块的初始行坐标}//返回新块的初始列坐标方法publicintgetInitCol(){return(blockInitCol);//返回新块的初始列坐标}//满行删除方法voiddeleteFullLine(){intfull_line_num=0;intk=0;for(inti=0;i1){ERS_Block.level--;ERS_Block.levelField.setText(""+ERS_Block.level);ERS_Block.score=0;ERS_Block.scoreField.setText(""+ERS_Block.score);}scr.requestFocus();break;casebutton_pause:if(pause_resume){ERS_Block.timer.suspend();pause_resume=false;}else{ERS_Block.timer.resume();pause_resume=true;}scr.requestFocus();break;casebutton_quit:System.exit(0);}}}//方块类classBlock{staticint[][]pattern={{0x0f00,0x4444,0x0f00,0x4444},//用十六进至表示,本行表示长条四种状态{0x04e0,0x0464,0x00e4,0x04c4},{0x4620,0x6c00,0x4620,0x6c00},{0x2640,0xc600,0x2640,0xc600},{0x6220,0x1700,0x2230,0x0740},{0x6440,0x0e20,0x44c0,0x8e00},{0x0660,0x0660,0x0660,0x0660}};intblockType;//块的模式号(0-6)intturnState;//块的翻转状态(0-3)intblockState;//快的下落状态introw,col;//块在画布上的坐标GameCanvasscr;//块类的构造方法Block(GameCanvasscr){this.scr=scr;blockType=(int)(Math.random()*1000)%7;turnState=(int)(Math.random()*1000)%4;blockState=1;row=scr.getInitRow();col=scr.getInitCol();}//重新初始化块,并显示新块publicvoidreset(){blockType=(int)(Math.random()*1000)%7;turnState=(int)(Math.random()*1000)%4;blockState=1;row=scr.getInitRow();col=scr.getInitCol();dispBlock(1);}//实现“块”翻转的方法publicvoidleftTurn(){if(assertValid(blockType,(turnState+1)%4,row,col)){dispBlock(0);turnState=(turnState+1)%4;dispBlock(1);}}//实现“块”的左移的方法publicvoidleftMove(){if(assertValid(blockType,turnState,row,col-1)){dispBlock(0);col--;dispBlock(1);}}//实现块的右移publicvoidrightMove(){if(assertValid(blockType,turnState,row,col+1)){dispBlock(0);col++;dispBlock(1);}}//实现块落下的操作的方法publicbooleanfallDown(){if(blockState==2)return(false);if(assertValid(blockType,turnState,row-1,col)){dispBlock(0);row--;dispBlock(1);return(true);}else{blockState=2;dispBlock(2);return(false);}}//判断是否正确的方法booleanassertValid(intt,ints,introw,intcol){intk=0x8000;for(inti=0;i1;}}returntrue;}//同步显示的方法publicsynchronizedvoiddispBlock(ints){intk=0x8000;for(inti=0;i1;}}}}//定时线程classMyTimerextendsThread{GameCanvasscr;publicMyTimer(GameCanvasscr){this.scr=scr;}publicvoidrun(){while(true){try{sleep((10-ERS_Block.level+1)*100);}catch(InterruptedExceptione){}if(!scr.getBlock().fallDown()){scr.deleteFullLine();if(scr.isGameEnd()){ERS_Block.isPlay=false;suspend();}elsescr.getBlock().reset();}}}}classWinListenerextendsWindowAdapter{publicvoidwindowClosing(WindowEventl){System.exit(0);}}------------------有机会一起学习~

怎么快速简单的打造一个炫酷的ListView

View动画的特殊使用场景,比如:

在ViewGroup中可以控制子元素的出场效果

在Activity中可以实现不同Activity之间的切换效果,

关于Activity切换这点,这篇帖子就不细说了无非就是overridePendingTransition的使用,本文主要要说的是LayoutAnimation

LayoutAnimatioon

LayoutAnimation作用于ViewGroup,为ViewGroup指定一个动画,这样他的子元素出场时都会具有这种动画

LayoutAnimatioon中的属性:

android:delay="0.5"

表示子元素开始动画的延迟时间,比如子元素入场动画的时间周期为300ms,那么0.5表示每个子元素都需要延迟150ms才能播放入场动画,也就是说:第一个子元素延迟150ms,第二个子元素延迟300ms。

android:animationOrder="normal"

表示元素动画的顺序,有三种选项分别是:

A:nromal--表示顺序显示

B:reverse--表示逆向显示

C:random--表示随机显示

android:animation="@anim/anim_item"

为子元素指定具体的入场动画

LayoutAnimatioon的使用遵循以下几个步骤:

定义LayoutAnimation

layoutAnimation xmlns:android=""

android:animation="@anim/anim_item"

android:animationOrder="normal"

android:delay="0.5"

/layoutAnimation

为子元素指定具体的入场动画

?xml version="1.0" encoding="utf-8"?

set xmlns:android=""

android:duration="600"

android:interpolator="@android:anim/accelerate_interpolator"

android:shareInterpolator="true"

alpha

android:fromAlpha="0"

android:toAlpha="1.0" /

translate

android:fromXDelta="500"

android:toXDelta="0" /

/set

为ViewGroup指定android:layoutAnimation属性:

在xml布局文件中指定android:layoutAnimation属性:

ListView

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:background="#fff4f7f9"

android:cacheColorHint="#00000000"

android:divider="#dddbdb"

android:dividerHeight="1.0px"

android:layoutAnimation="@anim/layout_animation"

android:listSelector="@android:color/transparent"

/

或者,可以在java代码中通过LayoutAnimationController来指定:

ListView listview = (ListView) findViewById(R.id.listview);

Animation animation = AnimationUtils.loadAnimation(TestAnimActivity.this, R.anim.anim_item);

LayoutAnimationController controller = new LayoutAnimationController(animation);

controller.setDelay(0.5f);

controller.setOrder(LayoutAnimationController.ORDER_NORMAL);

listview.setLayoutAnimation(controller);

然后正常使用ViewGroup(比如ListView)即可

怎么样,如此简单就能做出一个炫酷的ListView特效,so easy!

Java编程,填写下面的代码

class NoLowerLetterException extends Exception {

public NoLowerLetterException(String msg) {

super(msg);

}

}

class NoDigitException extends Exception {

public NoDigitException(String msg) {

super(msg);

}

}

class People {

void printLetter(char c) {

if (c = 'a' c = 'z') {

System.out.println(c);

} else {

try {

throw new NoLowerLetterException(String.valueOf(c));

} catch (NoLowerLetterException e) {

e.printStackTrace();

}

}

}

void printDigit(char c) {

if (c = '0' c = '9') {

System.out.println(c);

} else {

try {

throw new NoDigitException(String.valueOf(c));

} catch (NoDigitException e) {

e.printStackTrace();

}

}

}

}

public class ExceptionExample {

public static void main(String args[]) {

People people = new People();

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

// 【代码5】

// //将i转换为char类型,执行people.printLetter()方法,如果出现异常则捕获,并输出异常的错误信息!

people.printLetter((char) i);

}

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

// 【代码6】 //将i转换为char类型,执行people. printDigit

// ()方法,如果出现异常则捕获,并输出异常的错误信息!

people.printDigit((char) i);

}

}

}

求一个简单的JAVA游戏代码,100行左右,谢谢!

import java.awt.Dimension;

import java.awt.Graphics;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JPanel;

public class Painter extends JFrame{

/**

*

*/

private static final long serialVersionUID = 8160427604782702376L;

CanvasPanel canvas = new CanvasPanel();;

public Painter() {

super("Star");

this.add(canvas);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

this.pack();

this.setResizable(false);

this.setLocationRelativeTo(null);

this.setVisible(true);

}

public static void main(String[] args) {

new Painter();

}

}

class CanvasPanel extends JPanel implements ActionListener{

/**

*

*/

private static final long serialVersionUID = -4642528854538741028L;

private JButton[] btn = new JButton[4];

private String[] btn_name = {"+", "-", "R", "L"};

private int center_x = 200, center_y = 200, radius = 100, degree = 0;

public CanvasPanel() {

this.setPreferredSize(new Dimension(400, 500));

this.setLayout(null);

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

btn[i] = new JButton(btn_name[i]);

btn[i].setBounds(160 + i * 60, 425, 50, 50);

btn[i].addActionListener(this);

this.add(btn[i]);

}

}

@Override

public void paintComponent(Graphics g) {

super.paintComponent(g);

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

g.drawLine( (int) (center_x + radius * Math.sin(Math.toRadians(degree + 72 * i))),

(int) (center_y - radius * Math.cos(Math.toRadians(degree + 72 * i))),

(int) (center_x + radius * Math.sin(Math.toRadians(degree + 72 * i + 144))),

(int) (center_y - radius * Math.cos(Math.toRadians(degree + 72 * i + 144))));

}

}

public void actionPerformed(ActionEvent e) {

// TODO Auto-generated method stub

if(e.getActionCommand() == "+") {

if(radius 200)

radius += 2;

repaint();

} else if(e.getActionCommand() == "-") {

if(radius 0)

radius -= 2;

repaint();

} else if(e.getActionCommand() == "R") {

degree = (degree + 2) % 360;

repaint();

} else if(e.getActionCommand() == "L") {

degree = (degree - 2) % 360;

repaint();

}

}

}

网页标题:酷炫的java代码,优雅代码java
新闻来源:https://www.cdcxhl.com/article16/hedsgg.html

成都网站建设公司_创新互联,为您提供网站排名外贸网站建设移动网站建设用户体验网站营销品牌网站建设

广告

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

绵阳服务器托管