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);
}
No hay comentarios:
Publicar un comentario