Tatar gr. 22_1

Ćwiczenie A:

1)Kod programu:

package PackMT51;

import java.awt.*;

import java.applet.*;

import java.awt.event.*;

public class Zdarzenia_myszy extends Applet implements MouseListener, MouseMotionListener{

int x;

int y;

int wyborfigury;

Font a = new Font ("SERIF", Font.BOLD, 20);

Font b = new Font ("Monospaced", Font.BOLD, 30);

Font c = new Font ("Monospaced", Font.ITALIC, 20);

public void init(){

addMouseListener(this);

addMouseMotionListener(this);

setSize(1300,700);

}

public void paint (Graphics g){

Graphics2D g2= (Graphics2D) g;

g2.setStroke(new BasicStroke(4));

g.setColor(Color.green);

g.fillRect(0, 0, 1300, 100);

g.setColor(Color.black);

g.drawLine(0,100,1300,100);

g.drawLine(0,0,0,700);

g.drawLine(0,0,1300,0);

g.drawLine(100,100,100,700);

g.drawLine(1300,0,1300,700);

g.drawLine(0, 200, 100, 200);

g.drawLine(0, 300, 100, 300);

g.drawLine(0, 400, 100, 400);

g.drawLine(0, 500, 100, 500);

g.drawLine(0, 600, 100, 600);

g2.setStroke(new BasicStroke(2));

g.setColor(Color.pink);

g.drawLine(20, 180, 80, 120);

g.setColor(Color.magenta);

g.drawOval(20, 220, 60, 60);

x=50;

y=350;

rysujsniezynke();

x=50;

y=450;

rysujdzwonek();

x=50;

y=650;

rysujswiateczne();

g.setFont(a);

g.setColor(Color.red);

g.drawString("USUN", 20, 555);

g.setFont(b);

g.setColor(Color.yellow);

g.drawString("PROSTY PROGRAM GRAFICZNY", 200, 60);

}

public void rysujsniezynke(){

Graphics g=getGraphics();

Graphics2D g2= (Graphics2D) g;

g2.setStroke(new BasicStroke(3));

g.setColor(Color.cyan);

g.drawLine(x-10, y-10, x+10, y+10);

g.drawLine(x-10, y+10, x+10, y-10);

g.drawLine(x, y+14, x, y-14);

g.drawLine(x-14, y, x+14, y);

}

public void rysujdzwonek(){

Graphics g=getGraphics();

Graphics2D g2= (Graphics2D) g;

g2.setStroke(new BasicStroke(3));

g.setColor(Color.yellow);

g.drawLine(x-14, y+14, x-7, y+4);

g.drawLine(x-7, y+4, x-7, y-10);

g.drawLine(x-7, y-10, x-2, y-14);

g.drawLine(x-2, y-14, x+2, y-14);

g.drawLine(x+2, y-14, x+7, y-10);

g.drawLine(x+7, y-10, x+7, y+4);

g.drawLine(x+7, y+4, x+14, y+14);

g.drawLine(x-14, y+14, x+14, y+14);

g.drawOval(x, y+14, 7, 7);

g.drawOval(x-2, y-18, 4, 4);

}

public void rysujcos(){

Graphics g=getGraphics();

Graphics2D g2= (Graphics2D) g;

g2.setStroke(new BasicStroke(2));

g.setColor(Color.green);

g.drawLine(x-13, y, x+13, y);

g.drawLine(x, y-13, x, y+13);

g.drawOval(x-10, y-10, 20, 20);

}

public void rysujswiateczne(){

Graphics g=getGraphics();

Graphics2D g2= (Graphics2D) g;

g2.setStroke(new BasicStroke(2));

g.setColor(Color.green);

g.drawLine(x, y-20, x+6, y-10);

g.drawLine(x, y-20, x-6, y-10);

g.drawLine(x-2, y-10, x-6, y-10);

g.drawLine(x+2, y-10, x+6, y-10);

g.drawLine(x+2, y-10, x+12, y+5);

g.drawLine(x-2, y-10, x-12, y+5);

g.drawLine(x-5, y+5, x-12, y+5);

g.drawLine(x+5, y+5, x+12, y+5);

g.drawLine(x+5, y+5, x+20, y+27);

g.drawLine(x-5, y+5, x-20, y+27);

g.drawLine(x-20, y+27, x+20, y+27);

g.setColor(Color.black);

g.drawRect(x-7,y+27,14,8);

g.fillRect(x-7,y+27,14,8);

}

@Override

public void mouseClicked(MouseEvent arg0) {

// TODO Auto-generated method stub

}

@Override

public void mouseEntered(MouseEvent arg0) {

// TODO Auto-generated method stub

Graphics g=getGraphics();

g.setFont(c);

g.setColor(Color.yellow);

g.drawString("kursor w obrebie okna programu", 200, 80);

}

@Override

public void mouseExited(MouseEvent arg0) {

// TODO Auto-generated method stub

repaint(); // przywraca stan grafiki okreslony w met. paint

}

@Override

public void mousePressed(MouseEvent e) {

// TODO Auto-generated method stub

Graphics g=getGraphics();

x=e.getX();

y=e.getY();

if(x<100){

if(y>100){

if(y<200){

wyborfigury=1;

}

else{

if(y<300){

wyborfigury=2;

}

else{

if(y<400){

wyborfigury=3;

}

else{

if(y<500){

wyborfigury=4;

}

else{

if(y<600){

wyborfigury=5;

}

else{

if(y<700){

wyborfigury=6;

}

}

}

}

}

}

}

}

else{

if(wyborfigury==1){

g.setColor(Color.pink);

g.drawLine(x-10,y+10,x+10,y-10);

}

else{

if(wyborfigury==2){

g.setColor(Color.magenta);

g.drawOval(x,y,40,40);

}

else{

if(wyborfigury==3){

rysujsniezynke();

}

else{

if(wyborfigury==4){

rysujdzwonek();

}

else{

if(wyborfigury==5){

g.setColor(Color.white);

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

g.drawRect(x,y,30,30);

}

else{

if(wyborfigury==6){

rysujswiateczne();

}

}

}

}

}

}

}

}

@Override

public void mouseReleased(MouseEvent arg0) {

// TODO Auto-generated method stub

}

@Override

public void mouseDragged(MouseEvent arg0) {

// TODO Auto-generated method stub

}

@Override

public void mouseMoved(MouseEvent e) {

// TODO Auto-generated method stub

Graphics g=getGraphics();

x=e.getX();

y=e.getY();

if(x<100){

if(y>100){

if(y<200){

g.clearRect(5,5,42,42);

g.setColor(Color.pink);

g.drawLine(15,35,35,15);

}

else{

if(y<300){

g.clearRect(5, 5, 42, 42);

g.setColor(Color.magenta);

g.drawOval(15, 15, 20, 20);

}

else{

if(y<400){

g.clearRect(5,5,42,42);

x=25;

y=25;

rysujsniezynke();

}

else{

if(y<500){

g.clearRect(5,5,42,42);

x=25;

y=25;

rysujdzwonek();

}

else{

if(y<600){

g.clearRect(5,5,42,42);

x=25;

y=25;

rysujswiateczne();

}

}

}

}

}

}

}

}

}

0x08 graphic

2) Opis działania i użytych funkcji:

MouseListener - interfejs klasy Listener, implementowany przez klasę Zdarzenia_myszy, służacy do rejestrowania zachowań myszy, takich jak: press, release, click, enter, i exit.

MouseMotionListener - interfejs klasy Listener, implementowany przez klasę Zdarzenia_myszy, służacy do rejestrowania zachowań myszy, takich jak: mouse moves i mouse drags.

addMouseListener(this) - kontruktor określający w klasie init apletu, żeby rejestrować zdarzenia myszy występujące w jego własnym obszarze

Graphics2D g2= (Graphics2D) g

g2.setStroke(new BasicStroke(4));

@Override

public void mouseClicked(MouseEvent arg0)

public void mouseEntered(MouseEvent arg0)

mouseExited

mousePressed

mouseReleased

mouseDragged

mouseMoved

Ćwiczenie B:

1) Kod programu:

package PackgMT41;

public class ClassMT41 {

public static void main(String[] args) {

int i;

int n;

// n e(100,999)

for(i=145;i<=999;i++){

int a=i/100;

int b=(i%100)/10;

int c=(i%100)%10;

int d=a*a*a+b*b*b+c*c*c;

if(d==i){

System.out.println(d);

}

}

//int a=i/100;

//int b=(i%100)/10;

//int c=(i%100)%10;

//System.out.println(a);

//System.out.println(b);

//System.out.println(c);

}

}

0x08 graphic

  1. Opis działania i użytych funkcji:

% - operator modulo

/ - operator dzielenia całkowitego

Program wyszukuje liczby o zadanych parametrach opierając się na dwóch operatorach dzielenia.