「最美java代码」java最浪漫的编程代码
今天给各位分享最美java代码的知识,其中也会对java最浪漫的编程代码进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
求java做动画代码
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class TestImage extends Frame
{
private static final long serialVersionUID = 1L;
private static boolean PRESSED = false;
private static int pointX = 0;
private static int pointy = 200;
private static int RIGHT_GO = 0;
private static int LEFT_GO = 0;
private static int DIR = 0;
private static int ANGLE = 0;
private static int W = 50;
private static int H = 60;
private _Canvas canvas = null;
public TestImage ()
{
add (canvas = new _Canvas ());
setIgnoreRepaint (true);
requestFocus ();
}
public class _Canvas extends Canvas implements Runnable
{
private static final long serialVersionUID = 1L;
private BufferedImage bi = null;
private Image bufferedImage = null;
private Thread thread = null;
private long sleepTime = 10;
public _Canvas ()
{
try
{
bi = ImageIO.read (new File ("go.png"));
}
catch (IOException e)
{}
setBackground (Color.BLACK);
requestFocus ();
addKeyListener (new KeyListener ()
{
@Override
public void keyTyped ( KeyEvent e )
{}
@Override
public void keyReleased ( KeyEvent e )
{
RIGHT_GO = 0;
PRESSED = false;
}
@Override
public void keyPressed ( KeyEvent e )
{
// 38 40 37 39上下左右
DIR = e.getKeyCode ();
PRESSED = true;
}
});
}
@Override
public void paint ( Graphics g )
{
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint (RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2d.drawImage (rotateImage (bi.getSubimage (RIGHT_GO, LEFT_GO, W, H), ANGLE, true), pointX, pointy, W, H,
this);
g2d.dispose ();
}
@Override
public void update ( Graphics g )
{
if (null == bufferedImage)
{
bufferedImage = createImage (getWidth (), getHeight ());
}
Graphics bufferedG = bufferedImage.getGraphics ();
bufferedG.clearRect (0, 0, getWidth (), getHeight ());
paint (bufferedG);
bufferedG.dispose ();
g.drawImage (bufferedImage, 0, 0, this);
g.dispose ();
}
public void start ()
{
thread = new Thread (this);
thread.setName ("TestImage");
thread.setPriority (Thread.MIN_PRIORITY);
thread.start ();
}
public synchronized void stop ()
{
thread = null;
notify ();
}
@Override
public void run ()
{
Thread me = Thread.currentThread ();
while (thread == me !isShowing () || getSize ().width == 0)
{
try
{
Thread.sleep (555);
}
catch (InterruptedException e)
{
return;
}
}
while (thread == me isShowing ())
{
if (PRESSED)
{
try
{
if (DIR == 39)
{
RIGHT_GO = RIGHT_GO + 50;
LEFT_GO = 0;
pointX = pointX + 1;
if (pointX 420)
{
ANGLE = 90;
pointX--;
pointy--;
W = 60;
H = 50;
}
if (RIGHT_GO 50)
{
RIGHT_GO = 0;
}
}
else if (DIR == 37)
{
pointX = pointX - 1;
RIGHT_GO = RIGHT_GO + 50;
LEFT_GO = 60;
if (pointX 0)
{
ANGLE = -90;
pointX++;
pointy--;
W = 60;
H = 50;
}
if (RIGHT_GO 50)
{
RIGHT_GO = 0;
}
}
else if (DIR == 38)
{
W = 50;
H = 60;
pointy = 150;
ANGLE = 0;
RIGHT_GO = 100;
}
else if (DIR == 40)
{
W = 50;
H = 60;
ANGLE = 0;
pointy = 200;
RIGHT_GO = 0;
}
Thread.sleep (sleepTime);
repaint ();
}
catch (InterruptedException e)
{
break;
}
}
else
{
RIGHT_GO = RIGHT_GO + 50;
LEFT_GO = 0;
pointX = pointX + 1;
if (RIGHT_GO 50)
{
RIGHT_GO = 0;
}
if (pointX 500)
{
pointX = 0;
}
try
{
Thread.sleep (sleepTime);
repaint ();
}
catch (InterruptedException e)
{
break;
}
}
}
thread = null;
}
}
/**
* 旋转图像为指定角度
*
* @param degree
* @return
*/
public static BufferedImage rotateImage ( final BufferedImage image, final int angdeg, final boolean d )
{
int w = image.getWidth ();
int h = image.getHeight ();
int type = image.getColorModel ().getTransparency ();
BufferedImage img;
Graphics2D graphics2d;
( graphics2d = ( img = new BufferedImage (w, h, type) ).createGraphics () ).setRenderingHint (
RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
graphics2d.rotate (d ? -Math.toRadians (angdeg) : Math.toRadians (angdeg), w / 2, h / 2);
graphics2d.drawImage (image, 0, 0, null);
graphics2d.dispose ();
return img;
}
public static void main ( String[] args )
{
EventQueue.invokeLater (new Runnable ()
{
@Override
public void run ()
{
final TestImage ti = new TestImage ();
ti.setSize (new Dimension (500, 300));
ti.setLocationRelativeTo (null);
ti.addWindowListener (new WindowAdapter ()
{
@Override
public void windowClosing ( WindowEvent e )
{
System.exit (0);
}
@Override
public void windowDeiconified ( WindowEvent e )
{
ti.canvas.start ();
}
@Override
public void windowIconified ( WindowEvent e )
{
ti.canvas.stop ();
}
});
ti.setResizable (false);
ti.canvas.start ();
ti.setVisible (true);
}
});
}
}
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程序设计,多线程,求大神给一份可运行的代码
给你一个经典的例子。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 cn.com.storm.common.test;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class Test {
// 开始字符串
private final static String START_STRING = "startPosition";
// 结束字符串
private final static String END_STRING = "/startPosition";
public static void main(String[] args) {
File oldFile = new File("d:\\xxx.xrdml");
BufferedReader br = null;
BufferedWriter bw = null;
try {
// 获得原文件的输入流
br = new BufferedReader(new InputStreamReader(new FileInputStream(oldFile)));
// 获得新文件的输出流
bw =new BufferedWriter(new OutputStreamWriter(new FileOutputStream(oldFile.getParent()
+ oldFile.getName().replace(".", "_2."))));
// 读取一行记录
String line = null;
while ((line = br.readLine()) != null) {
// 判断当前行是否包含startPosition与/startPosition
if (line.contains(START_STRING) line.contains(END_STRING)) {
// 获取startPosition与/startPosition中间的字符串
String strNum = line.substring(line.indexOf(START_STRING) + START_STRING.length(),
line.indexOf(END_STRING));
// 转化成double类型
double num = Double.parseDouble(strNum);
num = num / 2;
// 把原数字替换成新数字
line.replace(strNum, String.valueOf(num));
}
// 写入新文件一行数据
bw.write(line);
// 写入新文件换行符
bw.newLine();
}
} catch (FileNotFoundException e) {
System.out.println("文件不存在!");
} catch (NumberFormatException e) {
System.out.println("文件中的数据错误!");
} catch (IOException e) {
System.out.println("文件读取或写入时发生错误!");
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
System.out.println("文件关闭失败");
}
}
if (bw != null) {
try {
bw.close();
} catch (IOException e) {
System.out.println("文件关闭失败");
}
}
}
}
}
JAVA编程
/*
* Question9.java
*
* Created on 2007年1月18日, 下午9:04
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package know;
/**
*
* @author Administrator
*/
public class Question9 {
public static void main(String args[]) {
StringBuffer str=new StringBuffer("ABCDEFG");
//【代码1】 // 向str尾加“123456789”。
str.append("123456789");
System.out.println(str);
//【代码2】 // 将str中的字符 ‘B’替换为‘b’。
str.replace(1,2,"b");
System.out.println(str);
//【代码3】 // 在str中的“123456789”前面插入“Game”。
str.insert(7,"Game");
System.out.println(str);
int index= str.indexOf("1"); // 获取str中首次出现“1”的位置。
//【代码5】 // 删除str中“1234”。
str.delete(11,15);
System.out.println(str);
int n = str.length();//【代码6】 // 获取str中字符个数。
//System.out.println(n);
//【代码7】 // 将str中“789”替换为“七八九”。
str.replace(13,16,"七八九");
System.out.println(str);
/*
StringBuffer otherStr = new StringBuffer("we love you");
int start = 0;
char c = '\0';
while(start!=-1) {
if(start!=0) {
start=start+1;
}
c=otherStr.charAt(start);
if(Character.isLowerCase(c)) {
c=Character.toUpperCase(c);
otherStr.setCharAt(start,c);
}
start=otherStr.indexOf(" ",start); //查找下一个空格。
}
System.out.println(otherStr);
*/
StringBuffer yourStr=new StringBuffer("i Love THIS GaME");
//【代码8】 // 将yourStr中的大写字符替换为对应的小写字符,
// 小写字符替换为对应的大写字符
char h = '\0';
for(int i = 0;i yourStr.length(); i++){
h = yourStr.charAt(i);
if(Character.isLowerCase(h))
h = Character.toUpperCase(h) ;
else
h = Character.toLowerCase(h);
yourStr.setCharAt(i,h);
}
System.out.println(yourStr);
}
}
最美java代码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java最浪漫的编程代码、最美java代码的信息别忘了在本站进行查找喔。
发布于:2022-11-28,除非注明,否则均为
原创文章,转载请注明出处。