教育路上

全国站>Java>多线程>java编写一个程序在窗体中模拟赛车比赛
学员需求

java编写一个程序在窗体中模拟赛车比赛

摘要:java编写一个程序在窗体中模拟赛车比赛,任务介绍 编写一个程序在窗体中模拟赛车比赛,java 编写 一个 程序 在 窗体 中 模拟 赛车 比赛。以下是我们为大家整理的,相信大家阅读完后肯定有了自己的选择吧。

2022-05-19 13:27网络推荐

发布时间:
2022-05-19 13:27
信息来源:
网络推荐
浏览次数:
618
java编写一个程序在窗体中模拟赛车比赛

赛车模拟

任务介绍 编写一个程序在窗体中模拟赛车比赛

任务目标 掌握使用 System 类的 out 对象的 println 方法在控制台中输出字符串。

实现思路 通过打印三角形的每一行来实现整个三角形的打印。

from shengerguan

实现代码:

Car.java

import java.awt.Color;

import java.awt.Graphics;

import java.awt.Point;

import java.awt.Rectangle;

public class Car {

private Point basePoint;

private Point[] head;

private Rectangle body;

private Rectangle leftWheel;

private Rectangle rightWheel;

public Car(Point p)

{

basePoint=p;

head=new Point[4];

computerPoint();

}

public void computerPoint()

{

head[0]=new Point(basePoint.x+20,basePoint.y);

head[1]=new Point(basePoint.x+10,basePoint.y+10);

head[2]=new Point(basePoint.x+40,basePoint.y+10);

head[3]=new Point(basePoint.x+30,basePoint.y);

body=new Rectangle(basePoint.x, basePoint.y+10, 50, 10);

leftWheel=new Rectangle(basePoint.x+10,basePoint.y+20, 10, 10);

rightWheel=new Rectangle(basePoint.x+30,basePoint.y+20, 10, 10);

}

public Point getBasePoint() {

return basePoint;

}

public void setBasePoint(Point basePoint) {

this.basePoint = basePoint;

}

public Point[] getHead() {

return head;

}

public void setHead(Point[] head) {

this.head = head;

}

public Rectangle getBody() {

return body;

}

public void setBody(Rectangle body) {

this.body = body;

}

public Rectangle getLeftWheel() {

return leftWheel;

}

public void setLeftWheel(Rectangle leftWheel) {

this.leftWheel = leftWheel;

}

public Rectangle getRightWheel() {

return rightWheel;

}

public void setRightWheel(Rectangle rightWheel) {

this.rightWheel = rightWheel;

}

public void move(Point p)

{

basePoint=p;

computerPoint();

}

public void draw(Graphics g)

{

g.setColor(Color.RED);

g.fillPolygon(new int[] {head[0].x,head[1].x,head[2].x,head[3].x,head[0].x}, new int[]

{head[0].y,head[1].y,head[2].y,head[3].y,head[0].y}, 5);

g.setColor(Color.GREEN);

g.fillRect(body.x, body.y, body.width, body.height);

g.setColor(Color.BLACK);

g.fillOval(leftWheel.x, leftWheel.y, leftWheel.width, leftWheel.height);

g.fillOval(rightWheel.x, rightWheel.y, rightWheel.width, rightWheel.height);

}

public void clear(Graphics g,Color bgColor)

{

g.setColor(bgColor);

g.fillRect(basePoint.x, basePoint.y, 50, 30);

}

}

CarPanel.java

import java.awt.Color;

import java.awt.Dimension;

import java.awt.Graphics;

import java.awt.Point;

import javax.swing.BorderFactory;

import javax.swing.JPanel;

public class RacePanel extends JPanel implements Runnable{

Car car;

int timeSpan;

public RacePanel()

{

setPreferredSize(new Dimension(404, 36));

setBorder(BorderFactory.createLineBorder(Color.BLACK,2));

car=new Car(new Point(2, 4));

timeSpan=10;

}

@Override

protected void paintComponent(Graphics g) {

// TODO Auto-generated method stub

super.paintComponent(g);

car.draw(g);

}

@Override

public void run() {

// TODO Auto-generated method stub

while(true)

{

try

{

Thread.sleep(timeSpan);

car.clear(getGraphics(), getBackground());

car.move(new Point((car.getBasePoint().x+1)%450, car.getBasePoint().y));

car.draw(getGraphics());

}

catch (InterruptedException e) {

// TODO: handle exception

e.printStackTrace();

}

}

}

}

Example02.java

import java.awt.FlowLayout;

import java.awt.GridLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.JTextField;

public class Example02 extends JFrame {

RacePanel[] racePanels;

JPanel topPanel;

JLabel[] lbls;

JTextField[] jtfs;

public Example02 ()

{

setLayout(new GridLayout(5, 0));

topPanel=new JPanel();

topPanel.setLayout(new FlowLayout(FlowLayout.CENTER));

lbls=new JLabel[4];

jtfs=new JTextField[4];

racePanels=new RacePanel[4];

for(int i=0;i<4;i++)

{

lbls[i]=new JLabel("Car "+(i+1)+":");

jtfs[i]=new JTextField(6);

topPanel.add(lbls[i]);

topPanel.add(jtfs[i]);

racePanels[i]=new RacePanel();

}

add(topPanel);

add(racePanels[0]);

add(racePanels[1]);

add(racePanels[2]);

add(racePanels[3]);

pack();

setTitle("赛车");

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setLocation(300,260);

setVisible(true);

for(int i=0;i<4;i++)

{

final Integer j=new Integer(i);

jtfs[i].addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

// TODO Auto-generated method stub

racePanels[j.intValue()].timeSpan=Integer.parseInt(jtfs[j.intValue()].getText());

}

});

}

Thread tr1=new Thread(racePanels[0]);

tr1.start();

Thread tr2=new Thread(racePanels[1]);

tr2.start();

Thread tr3=new Thread(racePanels[2]);

tr3.start();

Thread tr4=new Thread(racePanels[3]);

tr4.start();

}

public static void main(String[] args)

{

Example02 frame=new Example02 ();

}

}


上一篇:
java银行现金业务模拟
下一篇:
java哲学家进餐模拟
标签:
网友评论
发布评论

访客的评论 2024/03/29 23:13

文中描述的是准确的吗,如何报名!

相关推荐
我也来发表评价关闭
我对该内容的评价:
0
评价500
验证码: 看不清 换一张
提交 (匿名发布,无须担心别人知道您的身份)
学校免费发布信息关闭
我们审核后会尽快展示,如有图片请发邮件到:edu63@foxmail.com

姓      名:

内      容:

手机号码:

验  证  码:  换一张

确认提交
填写需求信息关闭
我们会根据您的需求匹配并审核留言

姓      名:

意向城市:

留      言:

手机号码:

验  证  码:  换一张

确认提交
完善补充本文信息关闭
非常感谢您帮助完善补充本文信息


 换一张

确认提交