martes, 23 de octubre de 2012

ALGUNOS EJEMPLOS SOBRE CLIPPING, FIGURAS Y TEXTO

 
TRABAJANDO CON CLIPPING

public void paint (Graphics g) {
        Graphics2D g2 = (Graphics2D)g;   
        // Primera parte del ejemplo
        int ancho = this.getSize().width;
        int largo = this.getSize().height;
        Ellipse2D e = new Ellipse2D.Float ( ancho/4.0f, largo/4.0f,
                              ancho/2.0f, largo/2.0f);
        g2.setClip(e);
        g2.setColor(Color.cyan);
        g2.fill(new Rectangle2D.Float(0.0f,0.0f,ancho,largo));
       
//Segunda parte del ejemplo
        Rectangle2D r2 = new Rectangle2D.Float( ancho/4.0f+10.0f, largo/4.0f+10.0f,
ancho/2.0f-20.0f, largo/2.0f-20.0f);
        g2.clip(r2);   
        g2.setColor(Color.white);
        g2.fill(new Rectangle2D.Float(0.0f,0.0f,ancho,largo));
}

COMPOSICIONES

public void paint (Graphics g) {
    Graphics2D g2 = (Graphics2D)g;
    Dimension d = getSize();
    int w = d.width;
    int h = d.height;
    // Crea una BufferedImage.
    BufferedImage buffImg = new BufferedImage(w, h,
BufferedImage.TYPE_INT_ARGB);
    Graphics2D gbi = buffImg.createGraphics();
      // Dibujo de un cuadrado rojo
        gbi.setColor(Color.red);
Rectangle2D r1 = new Rectangle2D.Float(150.0f, 50.0f, 100.0f, 100.0f);
        gbi.fill(r1);
    AlphaComposite ac;
    // Dibujo de un rectángulo verde encima
        ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER);
        gbi.setColor(Color.green);
        gbi.setComposite(ac);
        Rectangle2D r2 = new Rectangle2D.Float(150.0f, 100.0f, 150.0f, 100.0f);
        gbi.fill(r2);
      // Dibujo de un cuadrado magenta con preferencia de lo que hay debajo
        ac = AlphaComposite.getInstance(AlphaComposite.DST_OVER);
        gbi.setColor(Color.magenta);
        gbi.setComposite(ac);
        Rectangle2D r3 = new Rectangle2D.Float(100.0f, 100.0f, 100.0f, 100.0f);
        gbi.fill(r3);
      // Dibujo de un cuadrado amarillo que modifica sólo sobre lo que se superpone
        ac = AlphaComposite.getInstance(AlphaComposite.DST_IN, 0.15f);
        gbi.setColor(Color.yellow);
        gbi.setComposite(ac);
        Rectangle2D r4 = new Rectangle2D.Float(150.0f, 150.0f, 100.0f, 100.0f);
        gbi.fill(r4);
    g2.drawImage(buffImg, null, 0, 0);
}


DIBUJANDO CON GENERALPATH

public void paint (Graphics g) {
Graphics2D g2 = (Graphics2D)g;
// Creación del GeneralPath
g2.setColor(Color.green);
GeneralPath gp = new GeneralPath();
gp.moveTo(50.0f,50.0f);
gp.lineTo(100.0f,50.0f);
gp.curveTo(120.0f,30.0f,120.0f,100.0f,180.0f,125.0f);
gp.lineTo(50.0f,150.0f);
gp.closePath();
// Relleno y contorno
g2.fill(gp);
g2.setColor(Color.blue);
g2.draw(gp);
}

DIBUJAR TEXTO

public void paint (Graphics g) {
        super.paint(g);
        Graphics2D g2 = (Graphics2D)g;
        g2.setColor(Color.blue);
        g2.setFont(new Font("Arial", Font.ITALIC, 24));
g2.drawString("Lex Flavia Malacitana", 20, 60);
}

  
TEXTO CON TEXTLAYOUT

public void paint (Graphics g) {
        super.paint(g);
        Graphics2D g2 = (Graphics2D)g;      
FontRenderContext frc = g2.getFontRenderContext();
Font f = new Font("Arial", Font.ITALIC, 24);
TextLayout tl = new TextLayout("Lex Flavia Malacitana", f, frc);
g2.setColor(Color.red);
tl.draw(g2, 20, 60);
}

MAS COMPLETO

public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D)g;
FontRenderContext frc = g2.getFontRenderContext();
Dimension tamaño = getSize();
TextLayout tl;
Font f;
f = new Font("Arial", Font.ITALIC, 24);
tl = new TextLayout("Tamaño 24 Arial Cursiva", f, frc);
g2.setColor(Color.pink);
tl.draw(g2, (float)(tamaño.width - tl.getBounds().getWidth())/2,
tamaño.height/2.0f);
f = f.deriveFont(Font.BOLD, 16);
tl = new TextLayout("Tamaño 16 Arial Negrita", f, frc);
g2.setColor(Color.red);
tl.draw(g2, (float)(tamaño.width - tl.getBounds().getWidth())/2,
tamaño.height/3.5f);
f = new Font("Times New Roman", Font.PLAIN, 20);
tl = new TextLayout("Tamaño 20 Times New Roman Normal", f, frc);
g2.setColor(Color.orange);
tl.draw(g2, (float)(tamaño.width - tl.getBounds().getWidth())/2,
tamaño.height/1.5f);
}




martes, 2 de octubre de 2012

CURVAS


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

public class CubicCurve2D1 extends JFrame {
  private CubicCurve2D Shape1;
  private CubicCurve2D Shape2;
  private float X1;
  private float Y1;
  private float X2;
  private float Y2;
  private float CX1;
  private float CY1;
  private float CX2;
  private float CY2;
  private double DX1;
  private double DY1;
  private double DX2;
  private double DY2;
  private double DCX1;
  private double DCY1;
  private double DCX2;
  private double DCY2;

  public CubicCurve2D1 () {
    X1 = 10.F;
    Y1 = 20.F;
    X2 = 150.F;
    Y2 = 130.F;
    CX1 = 20.F;
    CY1 = 90.F;
    CX2 = 140.F;
    CY2 = 100.F;
    DX1 = 160.;
    DY1 = 20.;
    DX2 = 230.;
    DY2 = 40.;
    DCX1 = 160.;
    DCY1 = 120.;
    DCX2 = 240.;
    DCY2 = 90.;
    Shape1 = new CubicCurve2D.Float (X1, Y1, CX1, CY1, CX2, CY2, X2, Y2);
    Shape2 = new CubicCurve2D.Double ();
    Shape2.setCurve (DX1, DY1, DCX1, DCY1, DCX2, DCY2, DX2, DY2);
  }

  public void paint (Graphics g) {
    Graphics2D g2D;

    g2D = (Graphics2D) g;

    g2D.setColor (Color.cyan);
    g2D.drawLine ((int)X1, (int)Y1, (int)CX1, (int)CY1);
    g2D.drawLine ((int)X2, (int)Y2, (int)CX2, (int)CY2);
    g2D.drawLine ((int)DX1, (int)DY1, (int)DCX1, (int)DCY1);
    g2D.drawLine ((int)DX2, (int)DY2, (int)DCX2, (int)DCY2);

    g2D.setColor (Color.black);
    g2D.draw (Shape1);
    g2D.draw (Shape2);
  }

}