UNIDAD II.- TRANSFORMACIONES GEOMÉTRICAS

Anuncio
Graficación
UNIDAD II.- TRANSFORMACIONES GEOMÉTRICAS
___________________________________________________________________
LECCIÓN 2.4 Transformación ventana-área de vista
___________________________________________________________________
2.4.1.- Ruta de recorte
Una ruta de recorte define una región en la cual los objetos serán visibles.
Graphics2D mantiene dicha regíon.
Las porciones del objeto fuera del área de recorte no se mostrarán
Graphics2D g2 = (Graphics2D)g;
Shape ellipse = new Ellipse2D.Double(0, 0, 300,200);
g2.setClip(ellipse);
g2.drawImage(image, 0, 0, this);
package ejemplosgraphics2d;
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
public class VentanaDeVision extends JPanel{
public void paintComponent(Graphics g){
Graphics2D g2 = (Graphics2D)g;
GeneralPath path = new GeneralPath(GeneralPath.WIND_EVEN_ODD);
path.moveTo(100,200);
path.quadTo(250, 50, 400, 200);
path.lineTo(400,400);
path.quadTo(250,250,100,400);
path.closePath();
g2.clip(path);
g2.setColor(new Color(200,200,200));
Rafael Rivera López
1
Graficación
g2.fill(path);
g2.setColor(Color.black);
g2.setFont(new Font("Serif", Font.BOLD, 60));
g2.drawString("Clip Path Demo",80,200);
g2.drawOval(50, 250, 400, 100);
}
}
2.4.2.- Objetos Gráficos definidos por el usuario
Adicionalmente a los objetos gráficos que implementan Shape, se pueden diseñar nuevos
objetos.
Los métodos que se deben implementar al utilizar la interfaz Shape son:
public boolean contains(Rectangle2D rect)
public boolean contains(Point2D point)
public boolean contains(double x, double y)
public boolean contains(double x, double y, double w, double h)
public Rectangle getBounds()
public Rectangle2D getBounds2D()
public PathIterator getPathIterator(AffineTransform at)
public PathIterator getPathIterator(AffineTransform at, double flatness)
public boolean intersects(Rectangle2D rect)
public boolean intersects(double x, double y, double w, double h)
package ejemplosgraphics2d;
import java.awt.*;
import java.awt.geom.*;
public class Corazon implements Shape{
private GeneralPath path;
Rafael Rivera López
2
Graficación
public Corazon(float x, float y, float w, float h) {
path = new GeneralPath();
float x0 = x + 0.5f*w;
float y0 = y + 0.3f*h;
float x1 = x + 0.1f*w;
float y1 = y + 0f * h;
float x2 = x + 0f * w;
float y2 = y + 0.6f * h;
float x3 = x + 0.5f * w;
float y3 = y + 0.9f * h;
float x4 = x + 1f * w;
float y4 = y + 0.6f * h;
float x5 = x + 0.9f * w;
float y5 = y + 0f * h;
path.moveTo(x0, y0);
path.curveTo(x1, y1, x2, y2, x3, y3);
path.curveTo(x4, y4, x5, y5, x0, y0);
}
public Rectangle getBounds() {
return path.getBounds();
}
public Rectangle2D getBounds2D() {
return path.getBounds2D();
}
public boolean contains(double x, double y) {
return path.contains(x,y);
}
public boolean contains(Point2D p) {
return path.contains(p);
}
public boolean intersects(double x, double y, double w, double h) {
return path.intersects(x, y, w, h);
}
public boolean intersects(Rectangle2D r) {
return path.intersects(r);
}
public boolean contains(double x, double y, double w, double h) {
return path.contains(x,y);
}
Rafael Rivera López
3
Graficación
public boolean contains(Rectangle2D r) {
return path.contains(r);
}
public PathIterator getPathIterator(AffineTransform at) {
return path.getPathIterator(at);
}
public PathIterator getPathIterator(AffineTransform at,
double flatness) {
return path.getPathIterator(at, flatness);
}
}
package ejemplosgraphics2d;
import javax.swing.*;
import java.awt.*;
public class GraficoDefinidoPorUsuario extends JPanel{
public void paintComponent(Graphics g){
Graphics2D g2 = (Graphics2D)g;
Shape corazon = new Corazon(0,0,500,500);
g2.setColor(Color.red);
g2.fill(corazon);
}
}
Rafael Rivera López
4
Descargar