[ Ostap Bender @ 29.05.2007. 17:12 ] @
Dobio sam zadatak, u kojem trebam odrediti povrsinu presjeka dva proizvoljno odredjena poligona pomocu klikanja na radnoj povrsini. Nazalost prilicno sam nevjest u programiranju i jedino sam uspio da izguglam kod za program koji crta beskonacno poligona.

Code:

/*
      This applet lets the user draw colored polygons.
      The user inputs a polygon by clicking a series of points.
      Clicking near the starting point (within 2 pixels) or
      right-clicking (or Command-clicking) will complete the
      polygon, so the user can begin a new one.  Shift-clicking
      will clear the screen.  Up to 500 points are allowed in a
      polygon.  This applet does not keep a copy of the image
      on the screen, so it will not reappear if the applet is
      covered and then uncovered.
    */
    
    
    import java.awt.*;
    import java.awt.event.*;
    import java.applet.*;
    
    
    public class SimplePolygons extends Applet implements MouseListener {
            
            
       /* Variables for implementing polygon input. */
       
       private int[] xCoord, yCoord;  // Arrays containing the points of 
                                      //   the polygon.  Up to 500 points 
                                      //   are allowed.
                                      
       private int pointCt;  // The number of points that have been input.
       
       private final static Color polygonColor = Color.red;  
                            // Color that is used to draw the polygons.  
    
       public void init() {
             // Initialize the applet.  The applet listens for mouse events.
             // Arrays are created to hold the points.
          setBackground(Color.white);
          addMouseListener(this);
          xCoord = new int[500];
          yCoord = new int[500];
          pointCt = 0;
       }
       
       
       public void paint(Graphics g) {
       
             // The paint() routine does nothing but draw a 1-pixel black 
             // border around the applet.  Polygons drawn on the applet
             // are not permanent.
       
          g.setColor(Color.black);
          g.drawRect(0, 0, getSize().width - 1, getSize().height - 1);
                
       }  // end paint()
       
       
       private void putLine(int x1, int y1, int x2, int y2) {
              // Draw a line from (x1,y1) to (x2,y2) directly onto the
              // applet, without going through the paint() method.
           Graphics g = getGraphics();
           g.drawLine(x1,y1,x2,y2);
           g.dispose();
       }
       
       
public void mousePressed(MouseEvent evt) { 
             // Process a user mouse-click.
       
           if (pointCt < 2)
              return;
           Graphics g = getGraphics();
           if (pointCt == 2) {
              g.drawLine(xCoord[0], yCoord[0], xCoord[1], yCoord[1]);
           }

if (evt.isMetaDown() || pointCt == 500)    {
              g.setColor(Color.red);
              g.fillPolygon(xCoord, yCoord, pointCt);
              g.setColor(Color.black);
              g.drawPolygon(xCoord, yCoord, pointCt);
           }
          else {
                 // Add the point where the user clicked to the list of
                 // points in the polygon, and draw a line between the
                 // previous point and the current point.
             xCoord[pointCt] = evt.getX();
             yCoord[pointCt] = evt.getY();
             pointCt++;
             if (pointCt >= 2) {
                putLine(xCoord[pointCt-2], yCoord[pointCt-2], 
                             xCoord[pointCt-1], yCoord[pointCt-1]); 
             }
          }
          
       } // end mousePressed()
       
       public void mouseReleased(MouseEvent evt) { }
       public void mouseClicked(MouseEvent evt) { }
       public void mouseEntered(MouseEvent evt) { }
       public void mouseExited(MouseEvent evt) { }
    
    }  // end class SimplePolygons


Zanima me prvo kako da napravim da program iscrta samo dva poligona.
[ Mister_rap @ 29.05.2007. 22:41 ] @
Opste nisam gledao kod koji si dao jer nisi rekao kako treba da crtas poligon !?

Posle kolika klika treba da se iscrta poligon ?
[ Ostap Bender @ 30.05.2007. 01:42 ] @
Pogledaj kod!
Broj stranica je proizvoljno odredjen, moze biti od 3 pa nadalje. U ovom programu se svaki poligon zatvara ako se klikne blizu pocetne tocke ili ako se klikne desnim klikom. Mogu izbaciti prvu opciju pa da zatvaram samo sa desnim klikom, al volio bih da nakon drugog desnog klika zavrsim s programom. Bilo bih dobro da mogu takav uvjet postaviti.
[ bgd2500 @ 30.05.2007. 14:49 ] @
Kod bas i ne radi to sto pise da bi trebalo,...
Evo ga malo izmenjenog pa ti dalje nastavi.
Code:

import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class SimplePolygons extends Applet implements MouseListener {


  private final static Color polygonBorder = Color.black;
  private final static Color polygonColor = Color.red;

  private final int MAX_POINTS = 500;
  private final int MAX_POLYGONS = 2;
  private Polygon[] polygons;
  private boolean[] polygonsDone;
  private int pointCt;
  private int polygonCt;

  public void init() {
    setBackground(Color.white);
    addMouseListener(this);
    polygons = new Polygon[MAX_POLYGONS];
    polygonsDone = new boolean[MAX_POLYGONS];
    pointCt = 0;
    polygonCt = 0;
  }

  public void paint(Graphics g) {
    g.setColor(Color.black);
    g.drawRect(0, 0, getSize().width - 1, getSize().height - 1);

    for (int i=0; i<MAX_POLYGONS; i++) {
      if (polygons[i] != null) {
        if (polygonsDone[i]) {
          g.setColor(polygonColor);
          g.fillPolygon(polygons[i]);
          g.setColor(polygonBorder);
          g.drawPolygon(polygons[i]);
        } else {
          g.setColor(polygonBorder);
          g.drawPolyline(polygons[i].xpoints, polygons[i].ypoints, polygons[i].npoints);
        }
      }
    }
  }

  public void mousePressed(MouseEvent evt) { 
    if (polygonCt < MAX_POLYGONS) {
      if (polygons[polygonCt] == null) {
        polygons[polygonCt] = new Polygon();
      }
      polygons[polygonCt].addPoint(evt.getX(), evt.getY());
      int pointCt = polygons[polygonCt].npoints;
      if ((evt.isMetaDown() && pointCt > 2) || pointCt == MAX_POINTS) {
        polygonsDone[polygonCt++] = true;
      }
      repaint();
    }
  }

  public void mouseReleased(MouseEvent evt) { }
  public void mouseClicked(MouseEvent evt) { }
  public void mouseEntered(MouseEvent evt) { }
  public void mouseExited(MouseEvent evt) { }

}
[ Ostap Bender @ 30.05.2007. 18:50 ] @
Citat:
Kod bas i ne radi to sto pise da bi trebalo,...
Evo ga malo izmenjenog pa ti dalje nastavi.


U pravu si, ja budala sam krivi poslao.
Inace puno ti hvala na ovom novom kodu, odlican je.