📄 lienzo.java
字号:
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
import javax.xml.bind.annotation.adapters.CollapsedStringAdapter;
import java.awt.geom.*;
class Lienzo extends Canvas
{
//Atributos
private int radio;
private int centro_x, centro_y;
//Circunferencias
private Ellipse2D e[] = new Ellipse2D[21];
private int indice;
public Lienzo()
{
//Constructor sin par�metros
}
public Lienzo(int x, int y, int ancho, int alto)
{
//Constructor con par�metros
this.setBounds(x,y,ancho,alto);
//this.setBackground(Color.WHITE);
}
public void dibujarPlantilla(int radio)
{
this.radio = radio;
this.centro_x = this.getWidth() / 2;
this.centro_y = this.getHeight() / 2;
this.iniciarPlantilla();
}
public void paint(Graphics g)
{
Graphics2D g2_0 = (Graphics2D)g.create();
Graphics2D g2_1 = (Graphics2D)g.create();
Graphics2D g2_2 = (Graphics2D)g.create();
Graphics2D g2_3 = (Graphics2D)g.create();
//this.fig5(g2_0);
//this.fig4(g2_3);
//this.fig1(g2_0);
//this.fig2(g2_1);
//this.fig3(g2_2);
}
private void iniciarPlantilla()
{
int i;
//Tam�o de pincel
//Stroke pincel = new BasicStroke(2.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_MITER);
//g2.setStroke(pincel);
//Dibujamos el circulo principal
e[indice] = new Ellipse2D.Float(this.centro_x - this.radio / 2, this.centro_y - this.radio / 2, radio, radio);
indice++;
//Dibujamos los dos circulos de radio w = r/2
for(i = -1; i < 1; i++, indice++)
e[indice] = new Ellipse2D.Float(this.centro_x - this.radio / 4, this.centro_y + i * this.radio / 2, radio / 2, radio / 2);
//Dibujamos los cuatro circulos de radio x = r/4
for(i = -2; i < 2; i++, indice++)
e[indice] = new Ellipse2D.Float(this.centro_x - this.radio / 8, this.centro_y + i * this.radio / 4, radio / 4, radio / 4);
//Dibujamos los dos circulos de radio y = r/3
e[indice] = new Ellipse2D.Float(this.centro_x - this.radio / 2, this.centro_y - this.radio / 6, radio / 3, radio / 3);
indice++;
e[indice] = new Ellipse2D.Float(this.centro_x + this.radio / 6, this.centro_y - this.radio / 6, radio / 3, radio / 3);
indice++;
//Dibujamos los doce circulos de radio z = r/6
for(i = -1; i < 1; i++, indice++)
{
e[indice] = new Ellipse2D.Float(this.centro_x - this.radio / 4 + i * this.radio / 6, this.centro_y - this.radio / 4 - this.radio / 12, radio / 6, radio / 6);
e[indice + 2] = new Ellipse2D.Float(this.centro_x - this.radio / 4 - this.radio / 6, this.centro_y + i * this.radio / 6, radio / 6, radio / 6);
e[indice + 4] = new Ellipse2D.Float(this.centro_x - this.radio / 4 + i * this.radio / 6, this.centro_y + this.radio / 4 - this.radio / 12, radio / 6, radio / 6);
}
for(indice += 4, i = -1; i < 1; i++, indice++)
{
e[indice] = new Ellipse2D.Float(this.centro_x + this.radio / 4 + i * this.radio / 6, this.centro_y - this.radio / 4 - this.radio / 12, radio / 6, radio / 6);
e[indice + 2] = new Ellipse2D.Float(this.centro_x + this.radio / 4, this.centro_y + i * this.radio / 6, radio / 6, radio / 6);
e[indice + 4] = new Ellipse2D.Float(this.centro_x + this.radio / 4 + i * this.radio / 6, this.centro_y + this.radio / 4 - this.radio / 12, radio / 6, radio / 6);
}
indice += 4;
}
private void fig1(Graphics2D g2)
{
int i;
//Estabelcemos el color de la figura
g2.setColor(Color.BLUE);
//Creamos un area con la circunferencia principal
Area a = new Area(e[0]);
//Quitamos las cincunferencias circunscritas
for(i = 1; i < indice; i++)
a.subtract(new Area(e[i]));
//Creamos un rectangulo de ancho igual a radio / 2
Rectangle2D r = new Rectangle2D.Float(this.centro_x,0.0f,this.getWidth() - this.centro_x,this.getHeight());
//Intersectamos para obtener la mitad del area
a.intersect(new Area(r));
//Agregamos las circunferencias que hacen falta
a.add(new Area(e[1]));
a.add(new Area(e[8]));
a.add(new Area(e[16]));
a.add(new Area(e[20]));
//AffineTransform at = g2.getTransform();
//at.setToRotation(Math.toRadians(90),this.centro_x,this.centro_y);
//a.transform(at);
//Dibujamos la circunferencia obtenida
g2.fill(a);
g2.dispose();
g2 = null;
}
private void fig2(Graphics2D g2)
{
//Establecemos el color de la figura
g2.setColor(Color.RED);
//Creamos el area con la circunferencia principal
Area a = new Area(e[0]);
//Variables para el poligono a formar
int coordx [] = new int[4];
int coordy [] = new int[4];
coordx[0] = (int)e[9].getCenterX();
coordy[0] = (int)e[9].getCenterY();
coordx[1] = coordx[0] - radio/2;
coordy[1] = coordy[0] - radio/2;
coordx[3] = (int)e[16].getCenterX();
coordy[3] = (int)e[16].getCenterY();
coordx[2] = coordx[3] + radio/2;
coordy[2] = coordy[3] - radio/2;
//Formamos el poligono
Polygon pol = new Polygon(coordx,coordy,4);
//Intersectamos el area con el poligono
a.intersect(new Area(pol));
//Quitamos las circunferencias que no deben formar parte
a.subtract(new Area(e[9]));
a.subtract(new Area(e[16]));
//Agregamos las circunferencias faltantes
a.add(new Area(e[10]));
a.add(new Area(e[15]));
coordx[0] = (int)e[4].getCenterX();
coordy[0] = (int)e[4].getCenterY();
coordx[1] = coordx[0] - radio/6;
coordy[1] = coordy[0] - radio/6;
coordx[3] = coordx[0];
coordy[3] = coordy[0];
coordx[2] = coordx[3] + radio/6;
coordy[2] = coordy[3] - radio/6;
pol = new Polygon(coordx,coordy,4);
a.add(new Area(pol));
a.subtract(new Area(e[4]));
//Creamos una nueva area con el circulo principal
Area a2 = new Area(e[0]);
//Restamos de esta area el resultado del area a
a2.subtract(a);
//Dibujamos la figura resultante
g2.fill(a2);
g2.dispose();
g2 = null;
}
private void fig3(Graphics2D g2)
{
g2.setColor(Color.ORANGE);
Area a = new Area(e[0]);
Rectangle2D rec = new Rectangle2D.Float((int)e[9].getCenterX(),(int)e[9].getCenterY(),(int)(e[20].getCenterX() - e[9].getCenterX()),(int)(e[20].getCenterY() - e[9].getCenterY()));
a.intersect(new Area(rec));
a.add(new Area(e[9]));
a.subtract(new Area(e[11]));
a.add(new Area(e[12]));
a.subtract(new Area(e[13]));
a.add(new Area(e[14]));
a.add(new Area(e[16]));
a.subtract(new Area(e[17]));
a.add(new Area(e[18]));
a.add(new Area(e[19]));
a.subtract(new Area(e[20]));
Area a2 = new Area(e[0]);
int coordx [] = new int[4];
int coordy [] = new int[4];
coordx[0] = (int)e[9].getCenterX();
coordy[0] = (int)e[9].getCenterY();
coordx[1] = coordx[0] - radio/2;
coordy[1] = coordy[0] - radio/2;
coordx[3] = (int)e[16].getCenterX();
coordy[3] = (int)e[16].getCenterY();
coordx[2] = coordx[3] + radio/2;
coordy[2] = coordy[3] - radio/2;
Polygon pol = new Polygon(coordx,coordy,4);
a2.intersect(new Area(pol));
a2.subtract(new Area(e[9]));
a2.subtract(new Area(e[16]));
a2.add(new Area(e[10]));
a2.add(new Area(e[15]));
coordx[0] = (int)e[4].getCenterX();
coordy[0] = (int)e[4].getCenterY();
coordx[1] = coordx[0] - radio/6;
coordy[1] = coordy[0] - radio/6;
coordx[3] = coordx[0];
coordy[3] = coordy[0];
coordx[2] = coordx[3] + radio/6;
coordy[2] = coordy[3] - radio/6;
pol = new Polygon(coordx,coordy,4);
a2.add(new Area(pol));
a2.subtract(new Area(e[4]));
a.subtract(a2);
coordx[0] = (int)e[6].getCenterX();
coordy[0] = (int)e[6].getCenterY();
coordx[1] = coordx[0] - radio/6;
coordy[1] = coordy[0] - radio/6;
coordx[3] = coordx[0];
coordy[3] = coordy[0];
coordx[2] = coordx[3] + radio/6;
coordy[2] = coordy[3] - radio/6;
pol = new Polygon(coordx,coordy,4);
a2 = new Area(pol);
a.add(a2);
a.subtract(new Area(e[6]));
g2.fill(a);
g2.dispose();
g2 = null;
}
private void fig4(Graphics2D g2)
{
g2.setColor(Color.MAGENTA);
Area a = new Area(e[0]);
Rectangle2D rec = new Rectangle2D.Float((int)e[9].getCenterX(),(int)e[9].getCenterY(),(int)(e[20].getCenterX() - e[9].getCenterX()),(int)(e[20].getCenterY() - e[9].getCenterY()));
a.intersect(new Area(rec));
a.subtract(new Area(e[7]));
a.subtract(new Area(e[8]));
a.add(new Area(e[9]));
a.add(new Area(e[16]));
a.add(new Area(e[13]));
a.add(new Area(e[20]));
Area a2 = new Area(e[0]);
int coordx [] = new int[4];
int coordy [] = new int[4];
coordx[0] = (int)e[9].getCenterX();
coordy[0] = (int)e[9].getCenterY();
coordx[1] = coordx[0] - radio/2;
coordy[1] = coordy[0] - radio/2;
coordx[3] = (int)e[16].getCenterX();
coordy[3] = (int)e[16].getCenterY();
coordx[2] = coordx[3] + radio/2;
coordy[2] = coordy[3] - radio/2;
Polygon pol = new Polygon(coordx,coordy,4);
a2.intersect(new Area(pol));
a2.subtract(new Area(e[9]));
a2.subtract(new Area(e[16]));
a2.add(new Area(e[10]));
a2.add(new Area(e[15]));
a.add(a2);
a2 = new Area(e[0]);
coordx[0] = (int)e[13].getCenterX();
coordy[0] = (int)e[13].getCenterY();
coordx[1] = coordx[0] - radio/2;
coordy[1] = coordy[0] + radio/2;
coordx[3] = (int)e[20].getCenterX();
coordy[3] = (int)e[20].getCenterY();
coordx[2] = coordx[3] + radio/2;
coordy[2] = coordy[3] + radio/2;
pol = new Polygon(coordx,coordy,4);
a2.intersect(new Area(pol));
a2.subtract(new Area(e[13]));
a2.subtract(new Area(e[20]));
a2.add(new Area(e[14]));
a2.add(new Area(e[19]));
a.add(a2);
g2.fill(a);
g2.dispose();
g2 = null;
}
private void fig5(Graphics2D g2)
{
g2.setColor(Color.PINK);
Area a = new Area(e[0]);
Rectangle2D rec = new Rectangle2D.Float(this.centro_x - this.radio / 2 ,this.centro_y - this.radio / 2, this.radio / 2, this.radio / 2);
a.subtract(new Area(rec));
int coordx [] = new int[4];
int coordy [] = new int[4];
coordx[0] = (int)e[1].getCenterX();
coordy[0] = (int)e[1].getCenterY();
coordx[1] = (int)e[7].getCenterX();
coordy[1] = (int)e[7].getCenterY();
coordx[2] = (int)e[8].getCenterX();
coordy[2] = (int)e[8].getCenterY();
coordx[3] = coordx[0];
coordy[3] = coordy[0];
Polygon pol = new Polygon(coordx,coordy,4);
a.add(new Area(pol));
a.add(new Area(e[7]));
a.subtract(new Area(e[1]));
g2.fill(a);
g2.dispose();
g2 = null;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -