import java.awt.*; import javax.swing.*; import java.awt.event.*; public class Proyecto_1 extends JFrame { JMenu MenuArchivo, MenuGrafico, MenuEdicion; JMenuItem ArchivoGuardar, ArchivoSalir, GraficoLinea, GraficoCirculo, EdicionLimpiar; Grafico G = new Grafico(); final int NADA = 0, LINEA = 1, CIRCULO = 2; int X[] = new int[2]; int Y[] = new int[2]; boolean OK = false; // Para diferenciar el primer punto del segundo int Primitiva = NADA; public static void main(String R[]) { new Proyecto_1(); } public Proyecto_1() { this.getContentPane().setLayout(null); // Contenedor nulo this.setTitle("Editor Grafico 1.0"); // Titulo de la ventana this.setResizable(true); // Si es redimensionable this.setLocation(100, 100); // Posicion this.setSize(new Dimension(600, 450)); // Dimensiones this.toFront(); this.add(G); G.setBounds(0, 0, 600, 450); JMenuBar BarraMenu = new JMenuBar(); // Barra de menU MenuArchivo = InsertaMenu("Archivo"); MenuArchivo.setMnemonic(KeyEvent.VK_A); // Se accesa con Alt-A InsertaItem(MenuArchivo, ArchivoGuardar, "guardaR", KeyEvent.VK_G); //Alt-R InsertaItem(MenuArchivo, ArchivoSalir, "Salir", KeyEvent.VK_S); //Alt-S MenuGrafico = InsertaMenu("Grafico"); MenuGrafico.setMnemonic(KeyEvent.VK_G); // Se accesa con Alt-G InsertaItem(MenuGrafico, GraficoLinea, "Linea", KeyEvent.VK_L); // Alt-L InsertaItem(MenuGrafico, GraficoCirculo, "Circulo", KeyEvent.VK_C); // Alt-C MenuEdicion = InsertaMenu("Edicion"); InsertaItem(MenuEdicion, EdicionLimpiar, "lImpiar", KeyEvent.VK_I); // Alt-I BarraMenu.add(MenuArchivo); BarraMenu.add(MenuGrafico); BarraMenu.add(MenuEdicion); setJMenuBar(BarraMenu); G.addMouseListener(new EventoRaton()); this.setVisible(true); } JMenu InsertaMenu(String CAD) { return new JMenu(CAD); } void InsertaItem(JMenu MENU, JMenuItem ITEM, String CAD, int KEY_EVENT) { ITEM = new JMenuItem(CAD); ITEM.setMnemonic(KEY_EVENT); MENU.add(ITEM); ITEM.addActionListener(new Evento_Opciones()); } protected void processWindowEvent(WindowEvent e) { if (e.getID() == WindowEvent.WINDOW_CLOSING) System.exit(0); } class Evento_Opciones implements ActionListener { public void actionPerformed(ActionEvent e) { if (e.getActionCommand().equals("Salir")) System.exit(0); if (e.getActionCommand().equals("Guardar")) { } if (e.getActionCommand().equals("Linea")) { Primitiva = LINEA; } if (e.getActionCommand().equals("Circulo")) { Primitiva = CIRCULO; } } } class EventoRaton implements MouseListener { // Estos mEtodos no nos interesan por ahora. Sin embargo hay que ponerlos // al tratarse de una interfaz public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} public void mousePressed(MouseEvent e) {} public void mouseReleased(MouseEvent e) {} // Se crea el mEtodo que si nos interesa trabajar public void mouseClicked(MouseEvent e) { String boton = ""; if ( !OK ) { X[0] = e.getX(); Y[0] = e.getY(); OK = true; } else { X[1] = e.getX(); Y[1] = e.getY(); OK = false; } switch (e.getButton()) { // boton = "izquierdo"; case MouseEvent.BUTTON1: if ( !OK) G.Dibuja(X, Y, Primitiva); break; case MouseEvent.BUTTON2: break; // boton = "central"; case MouseEvent.BUTTON3: break; // boton = "derecho"; } } } } class Grafico extends Canvas { final int NADA = 0, LINEA = 1, CIRCULO = 2; int X[] = new int[2], Y[] = new int[2], Primitiva = 1; final double POT = 2.0; double Ancho = 0.0, Alto = 0.0, Radio = 0.0; public Grafico() { setBackground(Color.red); // Color del fondo setForeground(Color.yellow); // Color de los trazos } void Dibuja(int X[], int Y[], int Primitiva) { this.X[0] = X[0]; this.Y[0] = Y[0]; this.X[1] = X[1]; this.Y[1] = Y[1]; this.Primitiva = Primitiva; repaint(); } public void paint(Graphics A) { switch ( Primitiva ) { case LINEA: A.drawLine( X[0], Y[0], X[1], Y[1] ); break; case CIRCULO: Radio = (int) Math.hypot ( (double)(X[1] - X[0]), (double)(Y[1] - Y[0]) ); Alto = Ancho = 2 * Radio; X[0] -= (int)Radio; Y[0] -= (int)Radio; A.drawOval( X[0], Y[0], (int)Ancho, (int)Alto ); break; } } }