网站建设资讯

NEWS

网站建设资讯

用Java的画小车的代码 小车是怎么画的

Java编写汽车类car

public class Car {

公司主营业务:网站制作、网站设计、移动网站开发等业务。帮助企业客户真正实现互联网宣传,提高企业的竞争能力。创新互联是一支青春激扬、勤奋敬业、活力青春激扬、勤奋敬业、活力澎湃、和谐高效的团队。公司秉承以“开放、自由、严谨、自律”为核心的企业文化,感谢他们对我们的高要求,感谢他们从不同领域给我们带来的挑战,让我们激情的团队有机会用头脑与智慧不断的给客户带来惊喜。创新互联推出阿拉尔免费做网站回馈大家。

private int num;//编号

private String name;//型号

private double price;//单价

/**

 * 无参构造

 */

public Car(){

super();

}

/**

 * 有参构造

 * @param num

 * @param name

 * @param price

 */

public Car(int num, String name, double price) {

super();

this.num = num;

this.name = name;

this.price = price;

}

public int getNum() {

return num;

}

public void setNum(int num) {

this.num = num;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public double getPrice() {

return price;

}

public void setPrice(double price) {

this.price = price;

}

public String inforShow() {

return "Car [num=" + num + ", name=" + name + ", price=" + price + "]";

}

}

public class PriCar extends Car{

private int PersonNum;//最大载客量

public PriCar(int personNum) {

super();

PersonNum = personNum;

}

public PriCar() {

super();

}

public int getPersonNum() {

return PersonNum;

}

public void setPersonNum(int personNum) {

PersonNum = personNum;

}

@Override

public String inforShow() {

return "PriCar [PersonNum=" + PersonNum + "]";

}

}

public class VanCar extends Car {

private double weight;//最大载重

public VanCar(double weight) {

super();

this.weight = weight;

}

public VanCar() {

super();

}

@Override

public String inforShow() {

return "PriCar  [num=" + super.getNum() + ", name=" + super.getName() + ", price=" + super.getPrice() +",weight=" + weight + "]";

}

}

测试类不想写了  应该可以自己写出来了吧

求大神解答,Java 画出如下图所示的小车

import java.awt.Color;

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.Point;

import java.awt.event.KeyAdapter;

import java.awt.event.KeyEvent;

import javax.swing.JFrame;

import com.me.util.JFrameUtil;

public class SwingCar extends JFrame {

private final int rect_Width = 80;

private final int rect_Height = 50;

private final int radius = 15;

private final int arcAngle = 30;

private Point p = new Point();

public SwingCar() {

setSize(500, 300);

setVisible(true);

setResizable(false);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JFrameUtil.toCenter(this);

p = new Point(getWidth() / 2, getHeight() / 2);

move();

}

@Override

public void paint(Graphics g) {

super.paint(g);

drawRect(p);

drawRolls(p);

g.drawLine(0,getHeight()/2+rect_Height/2+radius, getWidth(), getHeight()/2+rect_Height/2+radius);

}

public void drawRect(Point c) {

Graphics2D g = (Graphics2D) getGraphics();

g.setColor(Color.red);

g.drawRect((int) (c.getX() - rect_Width / 2),

(int) (c.getY() - rect_Height / 2), rect_Width, rect_Height);

g.setColor(Color.black);

g.drawLine((int) (c.getX() - rect_Width / 2)-50, (int) (c.getY() - rect_Height / 2)-rect_Height/2, (int) (c.getX() - rect_Width / 2), (int) (c.getY() - rect_Height / 2)+rect_Height/2);

g.setColor(Color.green);

g.fillOval((int) (c.getX() - rect_Width / 2)-50-2, (int) (c.getY() - rect_Height / 2)-rect_Height/2-2, 10, 10);

}

public void drawRolls(Point c) {

Graphics2D g = (Graphics2D) getGraphics();

g.setColor(Color.blue);

// first roll

g.fillOval((int) (c.getX() - rect_Width / 4 - radius), (int) (c.getY()

+ rect_Height / 2 - radius), radius * 2, radius * 2);

g.setColor(Color.green);

g.fillArc((int) (c.getX() - rect_Width / 4 - radius), (int) (c.getY()

+ rect_Height / 2 - radius), radius * 2, radius * 2,

calcAngle(c), arcAngle);

// second roll

g.setColor(Color.blue);

g.fillOval((int) (c.getX() + rect_Width / 4 - radius), (int) (c.getY()

+ rect_Height / 2 - radius), radius * 2, radius * 2);

g.setColor(Color.green);

g.fillArc((int) (c.getX() + rect_Width / 4 - radius), (int) (c.getY()

+ rect_Height / 2 - radius), radius * 2, radius * 2,

calcAngle(c), arcAngle);

}

public int calcAngle(Point c) {

Point center = new Point(getWidth() / 2, getHeight() / 2);

double distance = c.getX() - center.getX();

int angle = (int) (180 * distance / (Math.PI * radius));

return -angle;

}

public void move() {

this.addKeyListener(new KeyAdapter() {

@Override

public void keyPressed(KeyEvent e) {

if (e.getKeyCode() == KeyEvent.VK_RIGHT) {

p.translate(2, 0);

} else if (e.getKeyCode() == KeyEvent.VK_LEFT) {

p.translate(-2, 0);

}

repaint();

}

});

}

public static void main(String[] args) {

SwingCar sc = new SwingCar();

}

}

请问如何用Java编写一个汽车类Car

public class Car {

private String color;//颜色

private int door;//车门数量

private float speed;//车速

public Car(){

this.color = "红色";

this.door = 3;

this.speed = 110;

}

public Car(String color, int door, float speed) {

this.color = color;

this.door = door;

this.speed = speed;

}

public void start(){

//汽车启动。输出汽车已启动,并输出汽车的各个属性

System.out.println("汽车已启动,汽车颜色为"+color+",车门数为"+door+",车速为"+speed);

}

public void speedUp(float speed){

//加速

System.out.println("汽车加速到"+speed+"km/h");

}

public void shutDown(float speed){

//减速

System.out.println("汽车减速到"+speed+"km/h");

}

public void brake(){

//刹车

System.out.println("已刹车");

}

}

public class Test {

public static void main(String[] args){

Car car = new Car();

car.start();

car.speedUp(100);

car.shutDown(60);

car.brake();

Car car1 = new Car("白色",4,20.2F);

car1.start();

car1.speedUp(100);

car1.shutDown(60);

car1.brake();

}

}

运行结果

急~~高手请告诉我如何编写小汽车的java程序代码??重赏!

import java.lang.*;

class Car{

public void safe(){

System.out.println("an quan xi shu gao !");

}

}

class Benz extends Car{

public void safe(){

System.out.println("che hao !");

}

}

class Santana extends Car{

public void safe(){

System.out.println("che hen bu hao hao !");

}

}

public class Test{

public static void main(String[]args){

Car s = new Car();

s.safe();

Benz s1 = new Benz();

s1.safe();

Santana s2 = new Santana();

s2.safe();

Car s3 = new Benz();

s3.safe();

Car s4 = new Santana();

s4.safe();

}

}

这是一个 命名为Test.java

public class Car{

double price;

String name;

int id;

//3个重载的构造方法(name)(id,name)(id,name,price)

public Car(String name){

this(789,name);

}

public Car(int id,String name){

this(id,name,100000);

}

protected Car(int id,String name,double price){

this.price = price;

this.name = name;

this.id = id;

}

//3个重载的普通方法drive(int) drive(String) drive(int ,String)

public void drive(int i){

System.out.println("i = " + i);

System.out.println(price + "\n" + name + "\n" + id);

}

protected String drive(String s){

return "s is: " + s;

}

int drive(int i,String s){

return 2007;

}

}

class Test{

public static void main(String[]args){

Car c1 = new Car(777,"Santana");

c1.drive(18);

}

}

这个命名为Car.java

希望有你要的!


本文名称:用Java的画小车的代码 小车是怎么画的
网页网址:http://cdweb.net/article/hpecho.html