// VISUALIZA GRAFICOS POR BLOQUES...
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
class Grafico extends JFrame
{
Menu M;
Lienzo G;
public static void main(String R[]) { new Grafico(); }
public Grafico()
{
setLayout(null);
setBounds(100, 100, 800, 800); setVisible(true); setResizable(false);
M = new Menu(); add(M); M.setVisible(true); M.setBounds(0,0,800,100);
G=new Lienzo(M);add(G);G.setVisible(true);G.setBounds(0,110,800,650);
}
protected void processWindowEvent(WindowEvent e)
{ if (e.getID() == WindowEvent.WINDOW_CLOSING) System.exit(0); }
}
class Menu extends JPanel
{
JButton BLinea, BCuadro, BCirculo, BTodo;
JLabel LIndica;
int Tipo=1; // 1=LINEA, 2=CIRCULO, 3=CUADRO
public Menu()
{
setLayout(null); setBackground(Color.green);
BLinea = new JButton("LINEA"); BLinea.setBounds(10, 10, 100, 30);
BCirculo = new JButton("CIRCULO"); BCirculo.setBounds(130,10,100,30);
BCuadro = new JButton("CUADRO"); BCuadro.setBounds(250, 10, 100, 30);
BTodo = new JButton("TODO"); BTodo.setBounds(370, 10, 100, 30);
LIndica = new JLabel("Indicador"); LIndica.setBounds(600, 10, 90, 30);
add(BLinea); add(BCirculo); add(BCuadro); add(BTodo); add(LIndica);
BLinea.addActionListener( new RATON() );
BCirculo.addActionListener(new RATON() );
BCuadro.addActionListener(new RATON() );
BTodo.addActionListener(new RATON() );
}
class RATON implements ActionListener
{
String CAD = "";
public void actionPerformed(ActionEvent R)
{
CAD = R.getActionCommand();
LIndica.setText(CAD);
// 1=LINEA, 2=CIRCULO, 3=CUADRO
if (CAD.equals("LINEA")) Tipo=1;
if (CAD.equals("CIRCULO")) Tipo=2;
if (CAD.equals("CUADRO")) Tipo=3;
}
}
}
class Lienzo extends JPanel
{
Menu M;
Point Pi, Pf;
Point VLIi[] = new Point[10]; Point VLIf[] = new Point[10];
Point VCIi[] = new Point[10]; Point VCIf[] = new Point[10];
// 1=LINEA, 2=CIRCULO, 3=CUADRO
int NLI=0, NCI=0, NCU=0; // cantidades iniciales
boolean Bandera = false; // primer click
class RATONAZO implements MouseListener
{
public void mouseClicked(MouseEvent R)
{
if ( Bandera ) // segundo click
{
if (M.Tipo==1)
{
if (NLI != 10)
{
VLIf[NLI] = new Point();
VLIf[NLI].x = R.getX();
VLIf[NLI].y = R.getY();
NLI++;
}
}
if (M.Tipo==2)
{
if (NCI != 10)
{
VCIf[NCI] = new Point();
VCIf[NCI].x = R.getX();
VCIf[NCI].y = R.getY();
NCI++;
}
}
Bandera = false; repaint();
}
else
{
if (M.Tipo==1)
{
if (NLI != 10)
{
VLIi[NLI] = new Point();
VLIi[NLI].x = R.getX();
VLIi[NLI].y = R.getY();
}
}
if (M.Tipo==2)
{
if (NCI != 10)
{
VCIi[NCI] = new Point();
VCIi[NCI].x = R.getX();
VCIi[NCI].y = R.getY();
}
}
Bandera = true;
}
}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
}
public Lienzo(Menu M)
{
this.M = M;
setBackground(Color.blue);
addMouseListener(new RATONAZO());
}
public void paint(Graphics G)
{
int n=0; // contador auxiliar
G.setColor(Color.blue);
G.fillRect(0,0,800, 650) ;
G.setColor(Color.yellow);
switch (M.Tipo)
{
case 1:
for (n=0; n < NLI; n++)
G.drawLine(VLIi[n].x, VLIi[n].y, VLIf[n].x, VLIf[n].y);
break;
case 2:
for (n=0; n < NCI; n++)
G.drawOval(VCIi[n].x, VCIi[n].y, VCIf[n].x, VCIf[n].y);
break;
}
}
}