Sunteți pe pagina 1din 8

Ministerul Educaţiei, Tineretului şi Sportului al Republicii Moldova

Universitatea Tehnică a Moldovei

Facultatea Calculatoare, Informatică şi Microelectronică

Departamentul Informatica si Ingineria sistemelor

RAPORT
Lucrare de laborator nr.2
la Grafica pe Calculator
Tema: TRANSFORMĂRI GEOMETRICE A IMAGINILOR

A efectuat:

A verificat:
Chişinău 2020
1. Elaborarea programului de rotire, scalare şi deplasare a imaginilor în plan şi în spaţiu
a) Extrageţi la ecran un triunghi de culoarea R
b) Extrageţi la ecran un pătrat de culoarea S
c) Îndepliniţi transformarea tuturor punctelor de culoarea S în conformitate cu variantele (tabela
2). Punctul ce se deplasează se recolorează în culoarea R. Pentru determinarea culorii
punctului folosiţi procedura GetPixel.
Rotirea să fie efectuată în jurul unui punct oarecare din spaţiul coordonatelor ecran

2. Rotirea unui obiect în jurul unei axe


a) Desenarea unui cub specificat prin coordonatele vârfurilor sale
b) Rotirea cubului în jurul axelor orizontală şi verticală care trec prin centrul sau, precum şi în
jurul axei OZ

Codul:

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Lab2 {

public static void main(String[] args) {


DrawPanelLab2 panel = new DrawPanelLab2();
Deplasare deplasare = new Deplasare();
Scalare scalare = new Scalare();
Rotire rotire = new Rotire();
Cub cub = new Cub();

JButton b = new JButton("Rotire");


JButton b2 = new JButton("Scalare");
JButton b3 = new JButton("Deplasare");
JButton b4 = new JButton("Cub");
b.setBounds(100,700,140, 40);
b2.setBounds(300,700,140, 40);
b3.setBounds(500,700,140, 40);
b4.setBounds(300, 650, 140, 40);

JFrame application = new JFrame();


application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

application.add(b);
application.add(b2);
application.add(b3);
application.add(b4);
application.add(panel);
application.setSize(900, 900);
application.setVisible(true);
b.addActionListener(new ActionListener() {
//Rotire
@Override
public void actionPerformed(ActionEvent arg0) {
JFrame application1 = new JFrame("Rotire 300gr");
//application1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
application1.setSize(300, 300);
application1.add(rotire);
application1.setVisible(true);

}
});

b2.addActionListener(new ActionListener() {
//Scalare
@Override
public void actionPerformed(ActionEvent arg0) {
JFrame application2 = new JFrame("Scalare 1.5");
//application2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
application2.setSize(300, 300);
application2.add(scalare);
application2.setVisible(true);
}
});

b3.addActionListener(new ActionListener() {
//Deplasare
@Override
public void actionPerformed(ActionEvent arg0) {
JFrame application3 = new JFrame("Deplasare X : 200; Y : 20");
//application3.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
application3.setSize(400, 500);
application3.add(deplasare);
application3.setVisible(true);
}
});

b4.addActionListener(new ActionListener() {
//Cub
@Override
public void actionPerformed(ActionEvent arg0) {
JFrame application4 = new JFrame("Deplasare X : 2000; Y : 20");
//application3.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
application4.setSize(140, 180);
application4.add(cub);
application4.setVisible(true);
}
});
}
}

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Cub extends JPanel implements ActionListener {
Timer timer = new Timer(1, this);
double rotation = 0, grades = 1;

public void paint(Graphics g)


{
super.paint(g);
Graphics2D g2 = (Graphics2D) g;
Color S1 = new Color(216, 148, 140);
Color S2 = new Color(175, 114, 108);
Color S3 = new Color(155, 81, 114);

Polygon cub1 = new Polygon(new int [] {27, 27, 70, 70}, new int [] {95, 45, 20,
70}, 4);
Polygon cub2 = new Polygon(new int [] {70, 113, 113, 70}, new int [] {20, 45,
95, 70}, 4);
Polygon cub3 = new Polygon(new int [] {113, 70, 27, 70}, new int [] {95, 70, 95,
120}, 4);
g2.rotate(rotation/180, 70, 70);
g.setColor(S1);
g2.fill(cub1);
g.setColor(S2);
g2.fill(cub2);
g.setColor(S3);
g2.fill(cub3);
timer.start();
}
public void actionPerformed(ActionEvent ae) {
rotation += grades;
repaint();
}
}

import javax.swing.*;
import java.awt.*;

public class DrawPanelLab2 extends JPanel {


public void paintComponent(Graphics g) {
super.paintComponent(g);

int width = getWidth();


int height = getHeight();

Color R = new Color(216, 142, 156);


g.setColor(R);
g.fillPolygon(new int [] {200, 325, 450}, new int [] {550, 300, 550}, 3);

Color S = new Color(162, 216, 8);


g.setColor(S);
g.fillRect(230, 40,220, 220);

}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Rectangle2D;

public class Deplasare extends JPanel implements ActionListener {


Timer timer = new Timer(20, this);
double x = 100, y = 100, velX = 1, velY = 2;
int red = 216, green = 142, blue = 156, r1 = 216, g1 = 142, b1 = 156, r2 = 162, g2 =
216, b2 = 8;
int count = 0;

public void paintComponent(Graphics g) {


super.paintComponent(g);

Graphics2D g2 = (Graphics2D) g;
Color S = new Color(red, green, blue);
g.setColor(S);
Rectangle2D square = new Rectangle2D.Double(x,y, 100, 100);
g2.fill(square);
timer.start();
}

public void actionPerformed(ActionEvent e) {


if(x < 100 || x >= 110 ) {
velX = -velX;
count++;
if (count%2 != 0) {
red = r2;
green = g2;
blue = b2;
}
if (count%2 == 0) {
red = r1;
green = g1;
blue = b1;
}
}

if(y < 100 || y >= 120 ) {


velY = -velY;
}
x += velX;
y += velY;
repaint();
}
}

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Rectangle2D;
public class Rotire extends JPanel implements ActionListener {
Timer timer = new Timer(50, this);
double rotation = 0, grades = 1;
int red = 216, green = 142, blue = 156, r1 = 216, g1 = 142, b1 = 156, r2 = 162, g2 =
216, b2 = 8;
int count = 0;

public void paint(Graphics g)


{
super.paint(g);
Graphics2D g2 = (Graphics2D) g;
Color S = new Color(red, green, blue);
g.setColor(S);
Rectangle square = new Rectangle(100, 100, 100, 100);
g2.rotate(rotation/180, square.x + square.width/2, square.y + square.height/2);
g2.fill(square);
timer.start();
}
public void actionPerformed(ActionEvent ae) {
if(rotation < 0 || rotation >= 300 ) {
grades = -grades;
count++;
if (count%2 != 0) {
red = r2;
green = g2;
blue = b2;
}
if (count%2 == 0) {
red = r1;
green = g1;
blue = b1;
}

}
rotation += grades;
repaint();
}
}

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Rectangle2D;

public class Scalare extends JPanel implements ActionListener {


Timer timer = new Timer(50, this);
double height = 100, width = 100, scalX = 1, scalY = 1, x = 100, y = 100;
int red = 216, green = 142, blue = 156, r1 = 216, g1 = 142, b1 = 156, r2 = 162, g2 =
216, b2 = 8;
int count = 0;

public void paintComponent(Graphics g) {


super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
Color S = new Color(red, green, blue);
g.setColor(S);
Rectangle2D square = new Rectangle2D.Double(x,y, height, width);
g2.fill(square);
timer.start();
}

public void actionPerformed(ActionEvent e) {


if(height < x * 1.3|| height >= x ) {
scalX = -scalX;

if (count%2 != 0) {
red = r2;
green = g2;
blue = b2;
}
if (count%2 == 0) {
red = r1;
green = g1;
blue = b1;
}
count++;
}
if(width < y * 1.3 || width >= y ) {
scalY = -scalY;
}
height += scalX;
width += scalY;
repaint();
}
}

S-ar putea să vă placă și