Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
02__ПСП проги отсортированы.doc
Скачиваний:
0
Добавлен:
01.07.2025
Размер:
118.78 Кб
Скачать

Import java.Awt.*;

import java.awt.event.*;

import javax.swing.*;

public class Main extends JApplet implements Runnable, MouseListener {

Color c;

Thread t;

int x1=30,y1=15,h1=150,w1=60,l=500,m=500;

Rectangle a = new Rectangle(x1, y1, h1, w1);

int i=0, x=350, y=175, q=0;

public void mousePressed(MouseEvent e) {

i++;

if(i%2==0){

int a=(int)(Math.random()*8);

if(a==1){q=1;}

if(a==2){q=2;}

if(a==3){q=3;}

if(a==4){q=4;}

if(a==5){q=5;}

if(a==6){q=6;}

if(a==7){q=7;}

if(a==8){q=8;}

int k=(int)(Math.random()*255);

int k1=(int)(Math.random()*255);

int k2=(int)(Math.random()*255);

c = new Color(k, k1, k2);}

if(i%2!=0){

int a=(int)(Math.random()*8);

if(a==1){q=1;}

if(a==2){q=2;}

if(a==3){q=3;}

if(a==4){q=4;}

if(a==5){q=5;}

if(a==6){q=6;}

if(a==7){q=7;}

if(a==8){q=8;}

}}

public void run() {

while (true) {

repaint();

try {

t.sleep(50); }

catch (InterruptedException e) {

}}}

public void init() {

setSize(l, m);

t = new Thread(this);

t.start();

addMouseListener(this);

}

public void mouseClicked(MouseEvent e) {

}

public void mouseEntered(MouseEvent e) {}

public void mouseExited(MouseEvent e) {}

public void mouseReleased(MouseEvent e) {}

public void paint(Graphics g) {

g.setColor(c);

g.fillRect(0, 0, l, m);

g.setColor(Color.MAGENTA);

g.fillRect(x1, y1, h1, w1);

if(q==1){

x1++;

y1++;}

if(q==2){

x1--;

y1--;}

if(q==3){

y1--;

x1++;}

if(q==4){

y1++;

x1--;}

if(q==5){

y1--; }

if(q==6){

y1++;}

if(q==7){

x1++;}

if(q==8){

x1--;}

if(x1==500){

x1=0; }

else if(x1==0){

x1=500;

}

else if(y1==0){

y1=500;}

else if (y1==500)

y1=0;

}}

Длина дуги окружности изменяется нажатием клавиш от 1 до 9.

Import java.Applet.Applet;

Import java.Awt.*;

import java.awt.event.*;

public class Main extends Applet{

char n;

int x = 50, y = 50, w = 150, h = 150;

boolean c1,c2,c3,c4,c5,c6,c7,c8,c9;

public Main(){addKeyListener(new KeyAdapter(){

public void keyTyped(KeyEvent e) {

if(e.getKeyChar()=='1'){

c1=true;}

else if (e.getKeyChar()=='2')

c2=true;

else if (e.getKeyChar()=='3')

c3=true;

else if (e.getKeyChar()=='4')

c4=true;

else if (e.getKeyChar()=='5')

c5=true;

else if (e.getKeyChar()=='6')

c6=true;

else if (e.getKeyChar()=='7')

c7=true;

else if (e.getKeyChar()=='8')

c8=true;

repaint();

}

});}

public void paint(Graphics g) {

if(c1){

g.drawArc(x, y, w, h, 0, 40);

c1=false;

}

else if(c2){

g.drawArc(x, y, w, h, 0, 80);

c2=false;}

else if(c3){

g.drawArc(x, y, w, h, 0, 120);

c3=false;}

else if(c4){

g.drawArc(x, y, w, h, 0, 160);

c4=false;}

else if(c5){

g.drawArc(x, y, w, h, 0, 200);

c5=false;}

else if(c6){

g.drawArc(x, y, w, h, 0, 240);

c6=false;}

else if(c7){

g.drawArc(x, y, w, h, 0, 280);

c7=false;}

else if(c8){

g.drawArc(x, y, w, h, 0, 320);

c8=false;} else if(c9){

g.drawArc(x, y, w, h, 0, 360);

c9=false;}

else g.drawArc(x, y, w, h, 0, 360);

} }

Клавиши “вверх”, “вниз”, “вправо”, “влево” двигают в соответствующем направлении линию. При достижении границ апплета линия появляется с противоположной стороны.