1. 编写一个Java应用程序,设计一个汽车类Vehicle,包含的成员属性有:车轮个数wheels和车重weight。

Python039

1. 编写一个Java应用程序,设计一个汽车类Vehicle,包含的成员属性有:车轮个数wheels和车重weight。,第1张

classVehicle{privateintwheels;privatefloatweight;protectedVehicle(intwheels,floatweight){this。wheels=wheels;this。weight=weight。是普通的除号,即10/2=5。

编写java程序的注意事项:

大小写敏感:Java是大小写敏感的,这就意味着标识符Hello与hello是不同的。

类名:对于所有的类来说,类名的首字母应该大写。如果类名由若干单词组成,那么每个单词的首字母应该大写,例如 MyFirstJavaClass。

法名:所有的方法名都应该以小写字母开头。如果方法名含有若干单词,则后面的每个单词首字母大写,例如myFirstJavaClass。

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()

}

}