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

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

要求:

1.画出一条直线表示地面,在轮子上画出同心圆的扇形,是小车在行走时,轮子有转动的感觉.

2.通过键盘操作使小车向左右移动.


同心树和我 1年前 已收到1个回答 举报

后街小混混 幼苗

共回答了18个问题采纳率:100% 举报

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 {


x05private final int rect_Width = 80;
x05private final int rect_Height = 50;
x05private final int radius = 15;
x05private final int arcAngle = 30;
x05private Point p = new Point();


x05public SwingCar() {
x05x05setSize(500,300);
x05x05setVisible(true);
x05x05setResizable(false);
x05x05setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
x05x05JFrameUtil.toCenter(this);
x05x05p = new Point(getWidth() / 2,getHeight() / 2);
x05x05move();
x05}


x05@Override
x05public void paint(Graphics g) {
x05x05super.paint(g);
x05x05drawRect(p);
x05x05drawRolls(p);
x05x05g.drawLine(0,getHeight()/2+rect_Height/2+radius,getWidth(),getHeight()/2+rect_Height/2+radius);
x05}


x05public void drawRect(Point c) {
x05x05Graphics2D g = (Graphics2D) getGraphics();
x05x05g.setColor(Color.red);
x05x05g.drawRect((int) (c.getX() - rect_Width / 2),
x05x05x05x05(int) (c.getY() - rect_Height / 2),rect_Width,rect_Height);
x05x05g.setColor(Color.black);
x05x05g.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);
x05x05g.setColor(Color.green);
x05x05g.fillOval((int) (c.getX() - rect_Width / 2)-50-2,(int) (c.getY() - rect_Height / 2)-rect_Height/2-2,10,10);
x05}


x05public void drawRolls(Point c) {
x05x05Graphics2D g = (Graphics2D) getGraphics();
x05x05g.setColor(Color.blue);
x05x05// first roll
x05x05g.fillOval((int) (c.getX() - rect_Width / 4 - radius),(int) (c.getY()
x05x05x05x05+ rect_Height / 2 - radius),radius * 2,radius * 2);
x05x05g.setColor(Color.green);
x05x05g.fillArc((int) (c.getX() - rect_Width / 4 - radius),(int) (c.getY()
x05x05x05x05+ rect_Height / 2 - radius),radius * 2,radius * 2,
x05x05x05x05calcAngle(c),arcAngle);


x05x05// second roll
x05x05g.setColor(Color.blue);
x05x05g.fillOval((int) (c.getX() + rect_Width / 4 - radius),(int) (c.getY()
x05x05x05x05+ rect_Height / 2 - radius),radius * 2,radius * 2);
x05x05g.setColor(Color.green);
x05x05g.fillArc((int) (c.getX() + rect_Width / 4 - radius),(int) (c.getY()
x05x05x05x05+ rect_Height / 2 - radius),radius * 2,radius * 2,
x05x05x05x05calcAngle(c),arcAngle);


x05}


x05public int calcAngle(Point c) {
x05x05Point center = new Point(getWidth() / 2,getHeight() / 2);
x05x05double distance = c.getX() - center.getX();
x05x05int angle = (int) (180 * distance / (Math.PI * radius));
x05x05return -angle;
x05}


x05public void move() {
x05x05this.addKeyListener(new KeyAdapter() {
x05x05x05@Override
x05x05x05public void keyPressed(KeyEvent e) {
x05x05x05x05if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
x05x05x05x05x05p.translate(2,0);
x05x05x05x05} else if (e.getKeyCode() == KeyEvent.VK_LEFT) {
x05x05x05x05x05p.translate(-2,0);
x05x05x05x05}
x05x05x05x05repaint();
x05x05x05}
x05x05});


x05}


x05public static void main(String[] args) {
x05x05SwingCar sc = new SwingCar();
x05}


}

1年前 追问

2

同心树和我 举报

如果用jcreator软件做呢?

举报 后街小混混

这个和JCreator有什么关系吗?JCreator不就是一个开发环境,像Eclipse一样,java的语法还不是这样。。
可能相似的问题
Copyright © 2024 YULUCN.COM - 雨露学习互助 - 17 q. 0.029 s. - webmaster@yulucn.com