Documente Academic
Documente Profesional
Documente Cultură
FCIM
Catedra Calculatoare
RAPORT
la lucrarea de laborator nr. 1
la Grafica pe Calculator
Varianta 13
Chiinu 2017
0
Scopul lucrrii: Obinerea cunotinelor practice n sinteza figurilor grafice.
Sarcina:
1. De a elabora un program pentru sintezarea i afiarea figurilor grafice conform variantei 13;
2. De a afia figuri sintezate n diferite pori de vizualizare;
3. De a utiliza operaiuni de salvare i restaurare a imaginilor n/din fiiere;
Codul Programului:
package com.graphics;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Arc2D;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Path2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.awt.image.*;
class MyPanel extends JPanel {
private static boolean check,checkl,checkr,checkt,checkp,checkc,checke,checksc,checkse,img;
public void paintComponent(Graphics g) {
super.paintComponent(g);
removeAll();
g.drawImage(setBackground(), 0, 0,null);
JButton b = new JButton("Prism");
b.addActionListener(e -> {
check=true;
this.repaint();
});
b.setBounds(10,170,80,30);
this.add(b);
Prism(g);
JButton b1 = new JButton("Line");
b1.addActionListener(e -> {
checkl=true;
this.repaint();
});
b1.setBounds(10,10,80,30);
this.add(b1);
Line(g);
JButton b2 = new JButton("Rect");
b2.addActionListener(e -> {
checkr=true;
this.repaint();
});
b2.setBounds(10,90,80,30);
this.add(b2);
Rect(g);
1
JButton b3 = new JButton("Triangle");
b3.addActionListener(e -> {
checkt=true;
this.repaint();
});
b3.setBounds(10,50,80,30);
this.add(b3);
Triangle(g);
JButton b4 = new JButton("Polygon");
b4.addActionListener(e -> {
checkp=true;
this.repaint();
});
b4.setBounds(10,130,80,30);
this.add(b4);
Polygon(g);
JButton b5 = new JButton("Circle");
b5.addActionListener(e -> {
checkc=true;
this.repaint();
});
b5.setBounds(10,210,80,30);
this.add(b5);
Circle(g);
JButton b6 = new JButton("Elipse");
b6.addActionListener(e -> {
checke=true;
this.repaint();
});
b6.setBounds(10,250,80,30);
this.add(b6);
Elipse(g);
Graphics2D g2 = (Graphics2D) g;
JButton b7 = new JButton("H-Circle");
b7.addActionListener(e -> {
checksc=true;
this.repaint();
});
b7.setBounds(10,290,80,30);
this.add(b7);
SecCircle(g2);
JButton b8 = new JButton("H-Elipse");
b8.addActionListener(e -> {
checkse=true;
this.repaint();
});
b8.setBounds(10,330,80,30);
this.add(b8);
SecElipse(g2);
JButton b9 = new JButton("Erase");
b9.addActionListener(e -> {
this.repaint();
});
b9.setBounds(710,10,80,30);
this.add(b9);
JButton b10 = new JButton("Save");
b10.addActionListener(e -> {
try {
2
ScreenShot();
} catch (AWTException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
});
b10.setBounds(710,50,80,30);
this.add(b10);
JButton b11 = new JButton("Open");
b11.addActionListener(e -> {
img=true;
this.repaint();
});
OpenImage(g);
b11.setBounds(710,90,80,30);
this.add(b11);
}
private void Prism (Graphics g){
int xPolyP[] = {350,550,550,530,530,330,350,330,330,530,530,550};
int yPolyP[] = {150,150,200,240,190,190,150,190,240,240,190,150};
int xPolyPf[] = {350,330,330,530,550,550};
int yPolyPf[] = {150,190,240,240,200,150};
if(check) {
g.setColor(Color.YELLOW);
g.fillPolygon(xPolyPf, yPolyPf,6);
g.setColor(Color.BLUE);
g.drawPolygon(xPolyP, yPolyP,12);
check=false;
g.setColor(Color.BLACK);
}
}
private void Line (Graphics g){
if(checkl) {
g.setColor(Color.RED);
g.drawLine(150, 350, 700, 50);
checkl=false;
g.setColor(Color.BLACK);
}
}
private void Rect (Graphics g){
if(checkr) {
g.setColor(Color.CYAN);
g.fillRect(250, 80, 400, 200);
g.setColor(Color.RED);
g.drawRect (250, 80, 400, 200);
checkr=false;
g.setColor(Color.BLACK);
}
}
private void Triangle (Graphics g){
int xPolyT[] = {450,620,280};
int yPolyT[] = {80,300,300};
if(checkt) {
g.setColor(Color.GREEN);
g.fillPolygon(xPolyT, yPolyT,3);
g.setColor(Color.BLACK);
g.drawPolygon(xPolyT, yPolyT,3);
3
checkt=false;
g.setColor(Color.BLACK);
}
}
private void Polygon (Graphics g){
if(checkp) {
Polygon p = new Polygon();
for (int i = 0; i < 17; i++)
p.addPoint((int)(440 + 180 * Math.cos(i * 2 * Math.PI / 17)),
(int) (190 + 180 * Math.sin(i * 2 * Math.PI / 17)));
g.drawPolygon(p);
checkp=false;
}
}
private void Circle (Graphics g){
if(checkc) {
g.setColor(Color.CYAN);
g.fillRoundRect(350,80,200,200,360,360);
g.setColor(Color.ORANGE);
g.drawOval(350,80,200,200);
checkc=false;
g.setColor(Color.BLACK);
}
}
private void Elipse (Graphics g){
if(checke) {
g.setColor(Color.PINK);
g.fillRoundRect(350,120,200,130,300,300);
g.setColor(Color.BLACK);
g.drawOval(350,120,200,130);
checke=false;
g.setColor(Color.BLACK);
}
}
private void SecCircle (Graphics2D g2){
if(checksc) {
g2.draw(new Arc2D.Double(350, 80, 200, 200, 0, 180, Arc2D.PIE));
checksc=false;
}
}
private void SecElipse (Graphics2D g2){
if(checkse) {
g2.draw(new Arc2D.Double(350, 120, 200, 130, 0, 180, Arc2D.PIE));
checkse=false;
}
}
private void ScreenShot() throws AWTException, IOException {
Rectangle r = new Rectangle(200, 30, 512, 360);
BufferedImage bi = ScreenImage.createImage(r);
ScreenImage.writeImage(bi, "ScreenShot.jpg");
}
private void OpenImage(Graphics g){
if(img) {
g.drawImage(getImage(), 200, 0, null);
img = false;
}
}
private Image getImage(){
4
Image i1=null;
try {
i1 = ImageIO.read(new File("ScreenShot.jpg"));
} catch (IOException ex) {
Logger.getLogger(MyPanel.class.getName()).log(Level.SEVERE, null, ex);
}
return i1;
}
private Image setBackground(){
Image i1=null;
try {
i1 = ImageIO.read(new File("Background.jpg"));
} catch (IOException ex) {
Logger.getLogger(MyPanel.class.getName()).log(Level.SEVERE, null, ex);
}
return i1;
}
}
class MyFrame extends JFrame {
MyFrame() {
this.setTitle("Graphics");
this.setResizable(false);
this.setSize(800, 400);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.add(new MyPanel());
}}
public class Lab1GC {
public static void main(String[] args) {
MyFrame f = new MyFrame();
f.setVisible(true);
}
}
Rezultatele Testrii:
Concluzia: