java中物体自转的代码 java自动转换类型的机制
java图形界面中键盘控制物体移动
去掉TextField后的程序,这个程序是要用到repaint()的,具体请参考程序中的注释位置:
成都创新互联主营贵定网站建设的网络公司,主营网站建设方案,app开发定制,贵定h5成都小程序开发搭建,贵定网站营销推广欢迎贵定等地区企业咨询
import java.awt.*;
import java.awt.event.*;
class mCar extends Frame{
Color redColor;
int xl=80,yl=80,speed=10,step=5;/*********注意这里***********/
public mCar(){
addKeyListener(new KeyAdapter(){ /*********注意这里***********/
public void keyPressed(KeyEvent e){
if(e.getKeyCode()== KeyEvent.VK_UP){
System.out.println("\n Go Up");
yl-=speed;/*********注意这里***********/
}
else if(e.getKeyCode()== KeyEvent.VK_DOWN){
System.out.println("\n Go Down");
yl+=speed;/*********注意这里***********/
}
else if(e.getKeyCode()== KeyEvent.VK_LEFT){
System.out.println("\n Go Left");
xl-=speed;/*********注意这里***********/
}
else if(e.getKeyCode()== KeyEvent.VK_RIGHT){
System.out.println("\n Go Right");
xl+=speed;/*********注意这里***********/
}
else if(e.getKeyCode()== KeyEvent.VK_F1){
speed+=step;/*********注意这里***********/
System.out.println("\n Speed Up");
}
else if(e.getKeyCode()== KeyEvent.VK_F2){
System.out.println("\n Speed Down");
speed-=step;/*********注意这里***********/
}
else
System.out.println(e.getKeyChar());
repaint();/*********注意这里***********/
}
}
);
setSize(400,300);
setVisible(true);
setLocation(400,200);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
dispose();
System.exit(0);
}
}
);
}
public void paint(Graphics g){
g.setColor(Color.GREEN);
g.fillRect(xl, yl, 40, 40);/*********注意这里***********/
}
}
public class miniCar {
public static void main(String[] args){
new mCar();
}
}
Java的Graphics2D的rotate这函数来旋转图片,转动后导致锯齿
用原始图旋转各个角度,而不用旋转后的已损图再转..
可以避免。。通常就够了。
实在效果不好可以放大八倍再转再缩小....
用java写一个物体移动的轨迹
lz 你好
我做了一个比较简单的雏形
绿色正方形水平随机运动
蓝色正方形垂直随机运动
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.math.*;
public class MoveRect extends Frame implements Runnable{
private Rectangle hRect, vRect;
private Image buffer;
private Object o = new Object();
public MoveRect(){
super("MoveRect");
hRect = new Rectangle(50,0,50,50);//水平随机运动的正方形
vRect = new Rectangle(0,50,50,50);//垂直随机运动的正方形
setUndecorated(true);
setLocation(500,200);
setSize(400,400);
setVisible(true);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
//启动循环绘制(使正方形一直循环的运动)
public void circleDraw(){
}
//绘制正方形的状态
public void drawRect(Graphics g,Rectangle r){
g.drawRect(r.x, r.y, r.width, r.height);
}
//双缓冲机制(主要解决闪屏)
public void update(Graphics g){
buffer = createImage(getSize().width, getSize().height);
if(buffer != null){
Graphics g2 = buffer.getGraphics();
paint(g2);
g2.dispose();
g.drawImage(buffer, 0, 0, this);
}
else{
paint(g);
}
}
public void paint(Graphics g){
g.setColor(Color.GREEN);
drawRect(g,hRect);
g.setColor(Color.BLUE);
drawRect(g,vRect);
hRect.x = (int)(Math.random()*351);
vRect.y = (int)(Math.random()*351);
}
public void run(){
while(true){
stop();
rePaint();
}
}
//同步方法rePaint
public synchronized void rePaint(){
repaint();
}
//同步方法stop
public synchronized void stop(){
if(hRect.x = 50 vRect.y = 50){
try {
Thread.sleep(500);
}
catch (Exception ex) {
ex.printStackTrace();
}
}
}
public static void main(String[] args){
new Thread(new MoveRect()).start();
}
}
分享标题:java中物体自转的代码 java自动转换类型的机制
当前地址:http://ybzwz.com/article/dojehdc.html