Results 1 to 2 of 2
  1. #1
    AlexGraal is offline Señor Member
    Join Date
    Jan 2014
    Posts
    184
    Rep Power
    0

    Default Displaying sets of points - issues

    Here is my code: (taken from a certain place and modified to do what I need it to do)
    Java Code:
    //Points Display
    import java.applet.Applet;
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.Graphics;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.util.Random;
    
    public class PointsDisplay extends Applet implements ActionListener
    {
        // variables
        Random rnd;
        Point point[];
        int totcount = 0;
        int pNum = 4;
        int xPoints[];
        int yPoints[];
        int num;
        int xPoints2[];
        int yPoints2[];
        int w, h;
        int hold = 0;
        int[] gridx = {0 * 100 + 100, 0 * 100 + 100, 2 *  100 + 100, 2 * 100 + 100};
        int[] gridy = {0 * 100 + 50, 2 * 100 + 50, 0 * 100 + 50, 2 * 100 + 50};
        int[][] displaypoints = {{1,1,1,0,0,1,0,0},{1,1,1,0,0,2,0,0},{1,1,1,0,0,2,0,1},{1,2,1,0,0,1,0,0},{1,2,1,0,0,2,0,0},{1,2,1,0,0,2,0,1},{1,2,1,1,0,1,0,0},{1,2,1,1,0,2,0,0},{1,2,1,1,0,2,0,1},{2,0,1,0,0,2,0,1},{2,0,1,1,0,1,0,0},{2,0,1,1,1,0,0,1},{2,0,1,2,0,1,0,0},{2,0,1,2,0,2,0,0},{2,0,1,2,0,2,0,1},{2,0,1,2,1,0,0,1},{2,0,1,2,1,0,0,2},{2,0,1,2,1,1,0,0},{2,0,1,2,1,1,0,1},{2,1,1,0,0,1,0,0},{2,1,1,0,0,2,0,0},{2,1,1,0,0,2,0,1},{2,1,1,1,0,2,0,0},{2,1,1,1,1,0,0,0},{2,1,1,1,1,0,0,2},{2,1,1,2,0,1,0,0},{2,1,1,2,0,2,0,0},{2,1,1,2,0,2,0,1},{2,1,1,2,1,0,0,0},{2,1,1,2,1,0,0,1},{2,1,1,2,1,0,0,2},{2,1,1,2,1,1,0,0},{2,1,1,2,1,1,0,2},{2,1,2,0,0,1,0,0},{2,1,2,0,0,2,0,0},{2,1,2,0,0,2,0,1},{2,1,2,0,1,0,0,1},{2,1,2,0,1,0,0,2},{2,1,2,0,1,1,0,0},{2,1,2,0,1,1,1,0},{2,1,2,0,1,2,0,0},{2,1,2,0,1,2,0,1},{2,1,2,0,1,2,0,2},{2,1,2,0,1,2,1,0},{2,1,2,0,1,2,1,1},{2,2,1,0,0,1,0,0},{2,2,1,0,0,2,0,0},{2,2,1,0,0,2,0,1},{2,2,1,1,0,2,0,1},{2,2,1,1,1,0,0,1},{2,2,1,1,1,0,0,2},{2,2,1,2,0,1,0,0},{2,2,1,2,1,0,0,0},{2,2,1,2,1,0,0,1},{2,2,1,2,1,1,0,1},{2,2,2,0,0,1,0,0},{2,2,2,0,0,2,0,0},{2,2,2,0,0,2,0,1},{2,2,2,0,1,0,0,1},{2,2,2,0,1,0,0,2},{2,2,2,0,1,1,0,1},{2,2,2,0,1,1,1,0},{2,2,2,0,1,2,0,0},{2,2,2,0,1,2,0,1},{2,2,2,0,1,2,1,0},{2,2,2,0,1,2,1,1},{2,2,2,1,0,1,0,0},{2,2,2,1,0,2,0,0},{2,2,2,1,0,2,0,1},{2,2,2,1,1,0,0,0},{2,2,2,1,1,0,0,1},{2,2,2,1,1,0,0,2},{2,2,2,1,1,1,0,2},{2,2,2,1,1,1,1,0},{2,2,2,1,1,2,0,0},{2,2,2,1,1,2,0,1},{2,2,2,1,1,2,1,0},{2,2,2,1,1,2,1,1}};
        //
        Button bt;
    
        public void init()
        {
     Dimension size = getSize();
     w = size.width;
     h = size.height;
    
     rnd = new Random();
    
     xPoints = new int[pNum];
     yPoints = new int[pNum];
     xPoints2 = new int[pNum];
     yPoints2 = new int[pNum];
    
     giftWrapping();
    
     // button
     bt = new Button("New");
     bt.addActionListener(this);
     add(bt);
        }
    
        public void actionPerformed(ActionEvent ev)
        {
     if ( ev.getSource() == bt ) {
       if(hold < displaypoints.length - 1) {
         hold++;
       }
       //System.out.println("Hold: " + hold);
         giftWrapping();
     }
     repaint();
        }
    
        public boolean small(int current, int smallest, int i)
        {
     int xa, ya, xb, yb, val;
     xa = xPoints[smallest] - xPoints[current];
     xb = xPoints[i] - xPoints[current];
     ya = yPoints[smallest] - yPoints[current];
     yb = yPoints[i] - yPoints[current];
     
     val = xa * yb - xb * ya;
     if ( val > 0 )
         return true;
     else if ( val < 0 )
         return false;
     else {
         if ( xa * xb + ya * yb < 0 )
      return false;
         else {
      if ( xa * xa + ya * ya > xb * xb + yb * yb )
          return true;
      else
          return false;
         }
     }
        }
    
        public void giftWrapping()
        {
     // random
          
     int x, y;
     /*
     for ( int i = 0; i < pNum; i++ ) {
         xPoints[i] = 25 + rnd.nextInt(w-50);
         yPoints[i] = 25 + rnd.nextInt(h-50);
     }
     */
     xPoints[0] = displaypoints[hold][0] * 100 + 100;
     xPoints[1] = displaypoints[hold][2] * 100 + 100;
     xPoints[2] = displaypoints[hold][4] * 100 + 100;
     xPoints[3] = displaypoints[hold][6] * 100 + 100;
     yPoints[0] = displaypoints[hold][1] * 100 + 50;
     yPoints[1] = displaypoints[hold][3] * 100 + 50;
     yPoints[2] = displaypoints[hold][5] * 100 + 50;
     yPoints[3] = displaypoints[hold][7] * 100 + 50;
     // convex hull
     int min = 0;
     for ( int i = 1; i < pNum; i++ ) {
         if ( yPoints[i] == yPoints[min] ) {
      if ( xPoints[i] < xPoints[min] )
          min = i;
         }
         else if ( yPoints[i] < yPoints[min] )
      min = i;
     }
     //System.out.println("min: " + min);
    
     num = 0;
     int smallest;
     int current = min;
     do {
         xPoints2[num] = xPoints[current];
         yPoints2[num] = yPoints[current];
         num++;
         //System.out.println("num: " + num + ", current: " + current + "(" + xPoints[current] + ", " + yPoints[current] + ")");
         smallest = 0;
         if ( smallest == current )
      smallest = 1;
         for ( int i = 0; i < pNum; i++ ) {
      if ( ( current == i ) || ( smallest == i ) )
          continue;
      if ( small(current, smallest, i))
          smallest = i;
         }
         current = smallest;
     } while ( current != min );
        }
    
        public void paint(Graphics g)
        {
     // background
     g.setColor(Color.lightGray);
     g.fillRect(0,0,w,h);
     //System.out.println("w:"+w+", h:"+h);
     
     g.setColor(Color.yellow);
     g.drawPolygon(gridx, gridy, num);
     
     // set of points
     g.setColor(Color.blue);
     for ( int i = 0; i < pNum; i++ ) {
         g.fillOval(xPoints[i]-2,yPoints[i]-2, 4,4);
     }
     
     // border edge
     g.setColor(Color.black);
     g.drawPolygon(xPoints2, yPoints2, num);
     
    
     // border points
     g.setColor(Color.red);
     for ( int i = 0; i < num; i++ ) {
         g.drawOval(xPoints2[i]-5,yPoints2[i]-5, 10,10);
     }
        
        //display total
     if(hold < displaypoints.length - 1) {
          totcount++;
     }
        String dis = "Total shapes: " + String.valueOf(totcount);
        g.setColor(Color.white);
       g.drawString (dis, 25, 25);
        }
    }
    A couple of questions.

    1. Why, when I press "New" rapidly, will it skip painting shapes sometimes/adding to totcount.
    2. How can I make it so that The box in the background(which is currently connected in the wrong way as I was getting an error when I connected it correctly) can be modifiably large. AKA, I want to draw a grid in the background that can be different sizes, depending on the values of a variable xsize and ysize.
    3. What can I do to clean this up? All I want it to do is display the convex hull of the four points given in the int[][] displaypoints.

    Thanks,
    Alex

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,804
    Rep Power
    5

    Default Re: Displaying sets of points - issues

    Regarding number 1). I haven't run your code but all events are processed on the Event Dispatch Thread. So it is possible that the events are getting stacked up and while you are painting. So when the button events are finally processed, all the subsequent painting events may be reduced to a single event. I can't imagine you are hitting the button that fast. But that is just a guess.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Replies: 1
    Last Post: 11-06-2013, 11:14 AM
  2. Replies: 1
    Last Post: 08-11-2011, 11:46 AM
  3. Issues in Displaying JSP Page.
    By raju.i in forum Advanced Java
    Replies: 0
    Last Post: 05-07-2010, 12:31 AM
  4. Issues In Displaying the JSP page.
    By raju.i in forum New To Java
    Replies: 0
    Last Post: 05-06-2010, 09:58 PM
  5. Replies: 2
    Last Post: 08-24-2008, 01:30 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •