
package ejemplo01;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.*;
public class Ejemplo01 extends JFrame {
   private JPanel panel;
   int xa=0,ya=0,xb=0,yb=0,xc=0,yc=0,xp=0,yp=0,xcr=0,ycr=0,xpr=0,ypr=0;
   Boolean dibujaLinea=false, dibujaOval=false,llenaOval=false;
   public Ejemplo01() {
      setTitle("Ejemplo uso de JFrame");
 
      setSize(500, 500);
      setLocationRelativeTo(null);
      //setUndecorated(true);
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setVisible(true);
      setBackground(Color.YELLOW);
      
      MouseAdapter ma=new MouseAdapter() {
          
          @Override
          public void mouseDragged(MouseEvent e) {
              super.mouseDragged(e); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/OverriddenMethodBody
              System.out.println("Dragged");
          }

          @Override
          public void mouseReleased(MouseEvent e) {
              super.mouseReleased(e); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/OverriddenMethodBody
              System.out.println("Released");
          }

          @Override
          public void mousePressed(MouseEvent e) {
              super.mousePressed(e); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/OverriddenMethodBody
              System.out.println("Pressed");
              
              if (e.isControlDown()&&!dibujaLinea){ 
                    xa=e.getX();
                    ya=e.getY(); 
                    dibujaLinea=!dibujaLinea;
              }
              else if (e.isControlDown()&&dibujaLinea){
                    xb=e.getX();
                    yb=e.getY();
                    System.out.println(""+xa+" "+ya+" "+xb+" "+yb); 
                    dibujaLinea=!dibujaLinea;
                    repaint();
              }
              
              if (e.isAltDown()&&!dibujaOval){ 
                    xc=e.getX();
                    yc=e.getY(); 
                    dibujaOval=!dibujaOval;
              }
              else if (e.isAltDown()&&dibujaOval){
                    xp=e.getX();
                    yp=e.getY();
                    System.out.println(""+xc+" "+yc+" "+xp+" "+yp); 
                    dibujaOval=!dibujaOval;
                    repaint();
              }
              
              if (e.isShiftDown()&&e.isAltDown()&&!llenaOval){ 
                    xcr=e.getX();
                    ycr=e.getY(); 
                    llenaOval=!llenaOval;
              }
              else if (e.isShiftDown()&&e.isAltDown()&&llenaOval){
                    xpr=e.getX();
                    ypr=e.getY();
                    System.out.println(""+xcr+" "+ycr+" "+xpr+" "+ypr); 
                    llenaOval=!llenaOval;
                    repaint();
              }

          }
      };
      addMouseListener(ma);
      addMouseMotionListener(ma);
      
      
   }
   
   public void paint(Graphics g){
      
      g.setColor(Color.BLUE); 
      lineaBres(50,50,400,499,g);
      g.setColor(Color.RED);
      lineaDDA(40,494,395,40,g);
       
      int x[]={100,100,200,200};
      int y[]={100,200,200,100};
      g.setColor(Color.MAGENTA);
      g.fillPolygon(x, y, 4);
      
      g.setColor(Color.BLACK);
      poligono(250,250,12,100,g);
      
      g.setColor(Color.RED);
      g.drawLine(xa,ya,xb,yb);
      
      circulo(xc,yc,(int)(Math.sqrt(Math.pow(xp-xc,2)+Math.pow(yp-yc,2))),g);
      
      llenaCirculo(xcr,ycr,(int)(Math.sqrt(Math.pow(xpr-xcr,2)+Math.pow(ypr-ycr,2))),g);
      
     
    }
   
   public void lineaDDA(int x0, int y0, int x1, int y1, Graphics g){
        int dx = x1 - x0;
        int dy = y1 - y0;
        int steps = Math.max(Math.abs(dx), Math.abs(dy));
        
        float xIncrement = dx / (float) steps;
        float yIncrement = dy / (float) steps;
        
        float x = x0;
        float y = y0;
        
        for (int i = 0; i <= steps; i++) {
            g.drawRect(Math.round(x),Math.round(y),1,1);
            x += xIncrement;
            y += yIncrement;
        }
   }
   
   public void lineaBres(int x0,int y0, int x1, int y1, Graphics g){
           
         
        int dx = Math.abs(x1 - x0);
        int dy = Math.abs(y1 - y0);
        int sx = x0 < x1 ? 1 : -1;
        int sy = y0 < y1 ? 1 : -1;
        int err = dx - dy;

        while (true) {
            g.drawRect(x0,y0,1,1);
            if (x0 == x1 && y0 == y1) break;
            int e2 = 2 * err;
            if (e2 > -dy) {
                err -= dy;
                x0 += sx;
            }
            if (e2 < dx) {
                err += dx;
                y0 += sy;
            }
        }
    }
   
   public void poligono(int xc, int yc,int n,int r,Graphics g){
       int x[]=new int[n];
       int y[]=new int[n];
       double ang=0;
       double incAng=360/n*Math.PI/180;
       for (int i=0;i<n;i++){
           x[i]=xc+(int)(r*Math.cos(ang));
           y[i]=yc-(int)(r*Math.sin(ang));
           System.out.println(String.format("%3d 5%d 5%d\n",i,x[i],y[i]));
           ang+=incAng;
       }
       g.drawPolygon(x, y, n);    
   }
   
   public void circulo(int xc, int yc, int r, Graphics g){
       g.drawOval(xc-r,yc-r,2*r,2*r);
   }
    
   public void llenaCirculo(int xc, int yc, int r, Graphics g){
       g.fillOval(xc-r,yc-r,2*r,2*r);
   }
    
   
   public static void main(String args[]) {
      Ejemplo01 p=new Ejemplo01();
   }
}
