【java】java实现运动的小车
2016-10-24 00:50
162 查看
实现如图所示的运动的小车其实还是非常简单的,还是先来看一下源代码:
import java.awt.Color; import java.awt.Graphics; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.Timer; public class Car extends JFrame{ public Car(){ add(new MyCar()); } public static void main(String[] args) { Car frame = new Car(); frame.setTitle("Car race"); frame.setSize(400,100); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } } class MyCar extends JPanel{ private int x = 10; Timer time = new Timer(100,new ActionListener() { public void actionPerformed(ActionEvent e) { repaint(); } }); protected void paintComponent(Graphics g){ time.start(); super.paintComponent(g); g.setColor(new Color(0, 0, 255)); g.fillOval(x, 50, 10, 10); g.fillOval(x+20, 50, 10, 10); g.fillRect(x-10, 40, 50, 10); int [] xPoints = {x,x+10,x+20,x+30}; int [] yPoints = {40,30,30,40}; g.fillPolygon(xPoints, yPoints, 4); x = x + 10; if(x >= 400) x = 10; } }其中的fillPolygon方法是java中用于绘制多边形的方法,其参数为两个数组,第一个数组为多边形所有点的横坐标,第二个数组为多边形所有点的纵坐标,第三个参数为多边形点的个数,也就是那两个数组的长度。
相关文章推荐
- Java简单的台球运动轨迹效果实现
- 机器人的运动范围(Java实现)
- Java_Swing实现小球沿正弦曲线运动的代码
- Java实现机器人的运动范围
- 智能小车37:异常在ARM、JAVA、硬件里的实现
- 智能小车37:异常在ARM、JAVA、硬件里的实现
- Java编程实现的模拟行星运动示例
- 剑指Offer面试题67:机器人的运动范围 Java实现
- java实现类的封装(物体实现椭圆运动)
- [置顶] java程序实现小球来回运动
- java编程加载窗口,实现物体变速运动(台球的运动)
- java实现椭圆运动(太阳系)
- arduino mega 2560 通过 L298N二路直流电机控制板实现小车简单运动
- 使用简单的URDF模型文件结合ros小车实现同步运动
- JAVA 多线程制作大球吃小球 一、实现球的自动生成及运动 生产消费模型
- 机器人的运动范围java实现
- dom规范和java中的实现(三)
- 如何在Java应用程序中实现copy图像功能。
- 用JAVA实现Undo、Redo,Copy、Paste、Cut
- 用Java实现的设计模式系列(2)-Prototype