java小车图片代码 Java代码图
求大神解答,Java 画出如下图所示的小车
import java.awt.Color;
创新互联公司专注于企业营销型网站、网站重做改版、渭城网站定制设计、自适应品牌网站建设、html5、商城网站开发、集团公司官网建设、成都外贸网站建设、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为渭城等各大城市提供网站开发制作服务。
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程序代码??重赏!
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代码
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class WalkDistance {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
for (int i = 0; i n; i++) {
String order = in.next();
System.out.println(new DecimalFormat("0.00").format(calDistance(order)));
}
in.close();
}
private static float calDistance(String order) {
char[] orderChar = order.toCharArray();
ListString orderList = new ArrayListString();
int num = 0;
for (int i = 0, len = orderChar.length; i len; i++) {
if (orderChar[i] = '0' orderChar[i] = '9') {
num++;
} else {
if (num == 0) {
orderList.add(String.valueOf(orderChar[i]));
} else {
StringBuffer sb = new StringBuffer();
for (int j = num; j 0; j--) {
sb.append(String.valueOf(orderChar[i - j]));
}
orderList.add(sb.toString());
orderList.add(String.valueOf(orderChar[i]));
num = 0;
}
}
if (i == len - 1 num != 0) {
StringBuffer sb = new StringBuffer();
for (int j = num - 1; j = 0; j--) {
sb.append(String.valueOf(orderChar[i - j]));
}
orderList.add(sb.toString());
}
}
Point curPoint = new Point(0, 0, 90);
for (int i = 0, len = orderList.size(); i len; i++) {
if ("R".equals(orderList.get(i))) {
curPoint.angle = (curPoint.angle - 90 + 360) % 360;
} else if ("L".equals(orderList.get(i))) {
curPoint.angle = (curPoint.angle + 90) % 360;
} else {
curPoint.setLocation(curPoint, orderList.get(i));
}
}
return curPoint.getDis();
}
static class Point {
private int x;
private int y;
private int angle;
private float dis;
public Point(int x, int y, int angle) {
this.x = x;
this.y = y;
this.angle = angle;
this.dis = (float) 0;
}
public void setLocation(Point point, String distance) {
if (point.angle == 0) {
point.x += Integer.valueOf(distance);
} else if (point.angle == 90) {
point.y += Integer.valueOf(distance);
} else if (point.angle == 180) {
point.x -= Integer.valueOf(distance);
} else {
point.y -= Integer.valueOf(distance);
}
}
public float getDis() {
return (float) Math.sqrt(this.x * this.x + this.y * this.y);
}
}
}
新闻名称:java小车图片代码 Java代码图
分享URL:http://myzitong.com/article/docoooc.html