Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Курсовая. Отчет. Богданов 4205..docx
Скачиваний:
0
Добавлен:
01.03.2025
Размер:
553.16 Кб
Скачать

11. Приложение 4. Текст программы на языке Java

import java.util.*;

import java.awt.*;

import java.awt.event.*;

import java.io.*;

public class Avto extends Observable implements Runnable

{

public int num;

public int x, y;

public double fi = 0.1;

public int dx1, dy1, dx2, dy2;

Thread thr;

public boolean dvig = true, run = true;

boolean life;

public int speed;

public int N() { return num; }

public int X() { return x; }

public int Y() { return y; }

public Avto(int N, int X, int Y, int Speed)

{

num = N;

x = X; y = Y;

speed = Speed;

life = true;

thr = new Thread(this);

thr.start();

}

public void report()

{

setChanged();

notifyObservers();

}

public void Finish()

{

Resume();

life = false;

}

public void run()

{

while (life)

{

if (run)

{

try

{

Thread.sleep(speed);

}

catch (InterruptedException e) { }

if (dvig)

{

x += dx1;

y += dy1;

dx1 = (int)(19 * Math.sin(fi));

dy1 = (int)(19 * Math.cos(fi));

}

else

{

y -= dx2;

x -= dy2;

dx2 = (int)(28 * Math.sin(fi));

dy2 = (int)(28 * Math.cos(fi));

}

fi += 0.1;

report();

}

}

}

public void Suspend()

{

if (run)

{

run = false;

}

}

public void Resume()

{

if (!run)

{

run = true;

}

}

}

public class Autos extends Frame implements Observer, ActionListener, ItemListener

{

public ArrayList al;

private boolean run;

private Frame f;

private Button ok, add;

private int num = 0;

private Random r;

private Choice n;

private Button del;

public Autos()

{

al = new ArrayList();

r = new Random();

this.addWindowListener(new WindowAdapter()

{

public void windowClosing(WindowEvent wE)

{

for (int n = 0; n < al.size(); n++)

{

Avto a = (Avto)al.get(n);

a.Suspend();

}

System.exit(0);

}

});

setTitle("Шоссе");

setLocation(new Point(600, 0));

setBackground(Color.green);

run = true;

f = new Frame();

f.setSize(300, 100);

f.setTitle("Control");

f.setLocation(new Point(150,150));

f.setLayout(new FlowLayout());

ok = new Button("Стоп");

ok.setActionCommand("stop");

ok.addActionListener(this);

f.add(ok);

add = new Button("Add");

add.setActionCommand("ADD");

add.addActionListener(this);

f.add(add);

f.setVisible(true);

del = new Button("Del");

del.setActionCommand("DEL");

del.addActionListener(this);

f.add(del);

n = new Choice();

n.addItemListener(this);

f.add(n);

f.setVisible(true);

}

//обработать уведомдение

public void update(Observable obj, Object arg)

{

repaint();

}

//перерисовать окно

public void paint(Graphics g)

{

g.setColor(Color.gray);

g.fillOval(100, 100, 604, 604);

g.fillOval(175, 175, 450, 450);

g.setColor(Color.white);

g.fillOval(173, 173, 454, 454);

g.setColor(Color.gray);

g.fillOval(175, 175, 450, 450);

g.fillOval(175, 175, 450, 450);

g.setColor(Color.green);

g.fillOval(245, 245, 300, 300);

for (int i = 0; i < al.size(); i++)

{

g.setColor(Color.red);

Avto a = (Avto)al.get(i);

g.fillOval(a.X(), a.Y(), 25, 25);

g.setColor(Color.white);

g.drawString(Integer.toString(a.num), a.X() + 5, a.Y() + 15);

}

boolean crash = false;

for (int i = 0; i < al.size(); i++)

{

Avto a = (Avto)al.get(i);

if (a.life)

for (int j = 0; j < al.size(); j++)

{

Avto a1 = (Avto)al.get(j);

if ((a.num != a1.num) && (Math.abs(a1.x - a.x) < 10) && (Math.abs(a1.y - a.y) < 10))

{

System.out.println("Avto " + a.num + " and " + a1.num + " vrezalis");

crash = true;

a.run = false;

a1.run = false;

try

{

Thread.sleep(400);

}

catch (InterruptedException e) { }

run = true;

a.run = true;

a1.run = true;

if (crash)

{

if ((a.x > a1.x) || (a.y > a1.y))

{

a.x += 2;

a1.x -= 2;

a.y += 2;

a1.y -= 2;

}

else

{

a1.x += 2;

a.x -= 2;

a1.y += 2;

a.y -= 2;

}

System.out.println("Avto " + a.num + " and " + a1.num + " raz'ehalis");

}

}

}

}

}

//выполнить команды для нажатия кнопок

public void actionPerformed(ActionEvent aE)

{

String str = aE.getActionCommand();

if (str.equals("stop"))

{

if (run)

{

run = false;

for (int n = 0; n < al.size(); n++)

{

Avto b = (Avto)al.get(n);

b.Suspend();

}

ok.setLabel("Старт");

}

else

if (!run)

{

run = true;

for (int n = 0; n < al.size(); n++)

{

Avto b = (Avto)al.get(n);

b.Resume();

}

ok.setLabel("Стоп");

}

}

if (str.equals("ADD"))

{

num++;

Avto b;

r = new Random();

if (num % 2 == 0)

{

Avto a = new Avto(num, 385, 666, r.nextInt(100)+50);

System.out.println("Dobavlen avto " + num);

a.dvig = false;

b = a;

}

else

{

Avto a = new Avto(num, 195, 400, r.nextInt(100)+50);

System.out.println("Dobavlen avto " + num);

a.dvig = true;

b = a;

}

b.addObserver(this);

n.addItem(Integer.toString(num));

al.add(b);

}

if (str.equals("DEL") && !al.isEmpty())

{

String st = n.getSelectedItem();

n.remove(st);

int sel = Integer.parseInt(st);

for (int n = 0; n < al.size(); n++)

{

Avto b = (Avto)al.get(n);

if (b.num == sel)

{

b.Finish();

al.remove(n);

}

}

}

repaint();

}

public void itemStateChanged(ItemEvent e)

{

repaint();

}

public static void main(String[] args)

{

Autos auto = new Autos();

auto.setSize(750, 750);

auto.setVisible(true);

}

}

62