Polygon Applet?

Please Help with this the applet is not working i tested the html file and it works fine, but there is something wrong with the applet. I am new to this stuff so I need your help. Much appreciated
The requirements: Write an applet that lets the user click on six points. After the sixth point is clicked, the applet should draw a polygon with a vertex at each point the user clicked.
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.applet.Applet;
public class assignment10 extends JApplet
     int i;
     int [] xCoords = new int[6];
     int [] yCoords = new int[6];
          Init Method
     public void init()
          getContentPane().setBackground(Color.white); // set the background color to white
          addMouseListener(new MyMouseListener()); // add the Mouse listener
     public void paint(Graphics g, int xCoords,int yCoords)
          super.paint(g);
          g.drawPolygon(xCoords, yCoords, 6);
     private class MyMouseListener extends MouseAdapter
          public void mousePressed(MouseEvent e)
               for(i=0; i<5; i++) // fill the array
                    xCoords[i] = e.getX();
                    yCoords[i] = e.getY();
if(i==5) // when the subscript i = 5, then reset to 0
                    i=0;

You've got a lot to fix here. Some suggestions:
1) For our benefit and yours, please use code tags whenever you post code. This will help your code retain its formatting, make it easier to read, and thus more likely that someone here will want to read it and help you. You simply highlight your pasted code and press the "Code" button before you post your message.
2) Don't paint directly in the applet with a paint method. Paint instead in a JPanel that is somehow placed in the applet's contentPane, either directly or inside of another panel that is directly added. Then paint by overriding the JPanel's paintComponent method.
3) Note that any time you override a method, be it paint, paintComponent, or any other method, the signature of the override must precisely match the signature of the method that it's overriding. Thus paint must have this signature: public void paint(Graphics g), and the same for paintComponent. public void paint(Graphics g, int x, int y) just won't work.
4) If your paint method DID work in fact work, your drawPolygon wouldn't. Look at the API and you'll see that its method signature is not Graphics#drawPolygon(int, int, int). You need to use arrays with this method. Why not use the arrays that are held by the class? You will probably need two sets of arrays, one to hold the data to be used to paint with and one to build up this data as the mouse is being pressed, then swapped with the first array, the display array, when 6 points have been pressed.
5) You have a for loop inside of your mousePressed method of the MouseAdapter. This will never work here as this logic is broken. You must think through what must happen here to fix this, and I think it best if you figure this out yourself.
6) Let's continue on in this thread here, but in the future if you have Swing problems, you'll be best served to post the question in the Swing forum which is where the Swing gurus live. (Although this would probably be appropriate for the Java 2D forum also -- in the future you need to choose the one most appropriate of these and post only there).
Much luck, and hope this helps!

Similar Messages

  • Animation problem ( badly needs) ?

    Hello,
    Sorry i 'm posting again and again. because nobody couldn't respond . I badly need this application to be run. Here is what am doing. First applet should start and display '+'( by painting) after keypressed(eg spacebar) then sleep for a while and paint rectangles and next arrow then sleep and etc . this goes on until i keypressed again. Here is that i wrote code , could u please check and modify it. Where i stucked is that KeyPressed code is not working. Please see the code modify it wherever is necessary.
    Thanks in advance,
    - Balaji
    import java.awt.*;
    import java.awt.event.*;
    import java.applet.Applet;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Color;
    import java.awt.Rectangle;
    import java.awt.GradientPaint;
    import java.awt.geom.GeneralPath;
    import java.awt.Polygon;
    //<applet code="ExptA4.class" width=300 height=300></applet>
    public class ExptA4 extends Applet implements Runnable {
         Thread animator;
         Dimension appSize;
         boolean frozen = false;
         int appWidth = 1000, appHeight = 800;
         int delay = 500;
         java.awt.geom.Ellipse2D.Double circle;
         public void init(){
              appSize = new Dimension(appWidth,appHeight);
              this.setSize(appSize);
             addKeyListener(new KeyAdapter(){
                  public void keyPressed(KeyEvent e){
                  ExptA4 exptA4 = (ExptA4) e.getSource();
                  if (e.getKeyCode() == KeyEvent.VK_SPACE)
                       System.out.println("in the keypressed method ");
                  }// end of if      
                  } // end of keyPressed method
         } // end of init
         public void start(){
              if(frozen)
               { // do nothing
              else
                 if(animator == null) {
                           animator = new Thread(this);
                animator.start();  
               } // end of else
               requestFocus(); 
         } // end of start
         public void stop(){
              // stop the animation thread
              animator = null ;
         } // end of stop
         public void run(){
              Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
              while(Thread.currentThread() == animator)
                      repaint();
                      try {
                     Thread.sleep(5400);
                       } catch (InterruptedException e) {
                             break;
         } // end of run
         public void paint(Graphics g){
              Graphics2D g2 = (Graphics2D)g;
            Rectangle leftRect,rightRect,plus_horz,plus_vert;
            Polygon downArrow = new Polygon();    
           // drawing the plus               
           int xHPoint = 440,yHPoint = 350, widthP = 30, heightP = 30;
           plus_horz = new Rectangle(xHPoint,yHPoint,widthP,heightP-27);
           plus_vert = new Rectangle(xHPoint+10,yHPoint-10,widthP-17,heightP-10);
           // black interior
            g2.setColor(Color.black);
            g2.fill(plus_horz);
            // black border
            g2.setColor(Color.black);
            g2.draw(plus_horz);
           // black interior
            g2.setColor(Color.black);
            g2.fill(plus_vert);
            // black border
            g2.setColor(Color.black);
            g2.draw(plus_vert);
               try{
                     Thread.sleep(4000);
                 } catch (InterruptedException ie)
           // drawing the Rectangle
                  leftRect = new Rectangle(300,300,100,100);
             rightRect = new Rectangle(500,300,100,100);
             // white interior
            g2.setColor(Color.white);
            g2.fill(leftRect);
            // black border
            g2.setColor(Color.black);
            g2.draw(leftRect);
            // white interior
            g2.setColor(Color.white);
            g2.fill(rightRect);
            // black border
            g2.setColor(Color.black);
            g2.draw(rightRect);
           try{
                     Thread.sleep(4000);
                 } catch (InterruptedException ie)
           // drawing downArrow
            int xPointA = 340,yPointA = 340, widthA = 30, heightA = 30;
             downArrow.addPoint(xPointA,yPointA);
            downArrow.addPoint(xPointA+25,yPointA-25);
            downArrow.addPoint(xPointA+13,yPointA-25);
            downArrow.addPoint(xPointA+13,yPointA-75);
            downArrow.addPoint(xPointA-13,yPointA-75);
            downArrow.addPoint(xPointA-13,yPointA-25);
            downArrow.addPoint(xPointA-25,yPointA-25);
            g2.setColor(Color.white);
            g2.fill(downArrow);
            // black border
            g2.setColor(Color.black);
            g2.draw(downArrow);
           try{
                     Thread.sleep(4000);
                 } catch (InterruptedException ie)
       // drawing circle             
           int xPoint = 340,yPoint = 340, width = 30, height = 30;
           circle = new java.awt.geom.Ellipse2D.Double(xPoint,yPoint,width,height);
            // black interior
            g2.setColor(Color.black);
            g2.fill(circle);
            // black border
            g2.setColor(Color.black);
            g2.draw(circle);
           try{
                     Thread.sleep(4000);
                 } catch (InterruptedException ie)
         } // end of paint method
    }

    hi ,
    here is modified code . I kept all sleep methods in the run method.
    But the problem is i couldn't get any response from the keyboard.
    What i need is that whenever key presses then animation should starts.
    please help me in this regard.i 'm putting my code here, please modify
    it according to my requirements. Once again i badly need this one to be run. i really appreciate if anyone help in this one.
    Thanks,
    -balaji
    import java.awt.*;
    import java.awt.event.*;
    import java.applet.Applet;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Color;
    import java.awt.Rectangle;
    import java.awt.GradientPaint;
    import java.awt.geom.GeneralPath;
    import java.awt.Polygon;
    //<applet code="ExptA5.class" width=300 height=300></applet>
    public class ExptA5 extends Applet implements Runnable,KeyListener {
         Thread animator;
         Dimension appSize;
         boolean frozen = false;
         int appWidth = 1000, appHeight = 800;
         int delay = 500;
      // drawing circle and arrow.
         DrawCircle circle = new DrawCircle();
        DrawArrow downArrow = new DrawArrow();
         public void init(){
            // setting dimension for applet
              appSize = new Dimension(appWidth,appHeight);
              this.setSize(appSize);
              // adding key listener
             addKeyListener(this);
         } // end of init
    // to activate keys
         public void keyPressed(KeyEvent e ){
              System.out.println("in the keypressed method ");
              ExptA5 exptA5 = (ExptA5) e.getSource();
        // when space bar presses then start the thread and animation starts
              if( e.getKeyCode() == KeyEvent.VK_SPACE)
               if ( Thread.currentThread() == animator)
                     exptA5.animator.start();
              } // end of if
         } // end of keyPressed
    // other key methods
         public void keyTyped(KeyEvent e){}
         public void keyReleased(KeyEvent e){}
         // applet starts and animation starts .
         public void start(){
              if(frozen)
               { // do nothing
              else
                 if(animator == null) {
                           animator = new Thread(this);
                animator.start();
               } // end of else
         } // end of start
         public void stop(){
              // stop the animation thread
              animator = null ;
         } // end of stop
         public void run(){
              Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
              while(Thread.currentThread() == animator)
              try {
                 Graphics g = getGraphics();
                   Graphics2D g2 = (Graphics2D)g;          
                  // drawing arrow which is calling from other class
                 downArrow.drawPoly(g2);
                 // sleeps for a while                               
                 Thread.sleep(5400);
                    // drawing circle which is calling from other class
                    circle.drawCircle(g2);
                    // sleeps for a while
                 Thread.sleep(5400);
                 repaint();   
                       } catch (InterruptedException e) {
                             break;
         } // end of run
         public void paint(Graphics g){
         } // end of paint method
    // drawing circle
    public class DrawCircle{
       java.awt.geom.Ellipse2D.Double circle;
       public void drawCircle(Graphics2D g2)
              int xPoint = 340,yPoint = 340, width = 30, height = 30;
          circle = new java.awt.geom.Ellipse2D.Double(xPoint,yPoint,width,height);
          // black interior
          g2.setColor(Color.black);
          g2.fill(circle);
            // black border
          g2.setColor(Color.black);
          g2.draw(circle);
    // drawing Arrow
    public class DrawArrow{
          Polygon downArrow = new Polygon();
       public void drawPoly(Graphics2D g2)
              // drawing downArrow
            int xPointA = 340,yPointA = 340, widthA = 30, heightA = 30;
             downArrow.addPoint(xPointA,yPointA);
            downArrow.addPoint(xPointA+25,yPointA-25);
            downArrow.addPoint(xPointA+13,yPointA-25);
            downArrow.addPoint(xPointA+13,yPointA-75);
            downArrow.addPoint(xPointA-13,yPointA-75);
            downArrow.addPoint(xPointA-13,yPointA-25);
            downArrow.addPoint(xPointA-25,yPointA-25);
            g2.setColor(Color.white);
            g2.fill(downArrow);
            // black border
            g2.setColor(Color.black);
            g2.draw(downArrow);
    }

  • Draw polygon in applet

    HI,
    I want to be able to draw polygon using java applet. I have code to draw poly in an applet but I am wondering if the java tool can draw directly on html image not on image inside of applet.
    Please help
    Ying

    You can generate a gif which is displayed inside a HTML screen.
    Note: HTML does not support polygon drawing so you cannot write HTML to draw a polygon. Unless you want a rectangle (use a table :)

  • Looking for a 2D Polygon Bevel Algorithm

    I am looking for a graphics algorithm to give two-dimensional polygons a 3D beveled edge look. The polygon should look like as though it is lit from the top-left. This is trivial for rectangular figures, but obviously much more complex for general polygons. The algorithm needs to support any type of polygon, including those with splines or other curved edges, as well as convex figures. Making matters even more complicated, it must also handle texture-filled polygons.
    A Java class that provides this functionality would be perfect. Short of that, an implementation in any language, or even a description of an algorithm, would be helpful.
    I created a java applet to illustrate the problem I am trying to solve - see the source below. This applet with source is also available at
    http://www.keithhilen.com/Java/bevel/
    Keith Hilen
    [email protected]
    Polygons.java :
    import java.applet.*;
    import java.awt.*;
    import java.awt.image.*;
    import java.awt.geom.*;
    public class Polygons extends Applet
    Image image;
    int polyWidth = 100;
    int polyHeight = polyWidth;
    int polyOfsX1 = 100;
    int polyOfsY1 = 100;
    int polyOfsX2 = 250;
    int polyOfsY2 = 100;
    int polyOfsX3 = 100;
    int polyOfsY3 = 240;
    int polyOfsX4 = 250;
    int polyOfsY4 = 240;
    Polygon octagon;
    Ellipse2D.Double ellipse;
    GeneralPath ornament;
    BufferedImage imageBuf1, imageBuf2, imageBuf3, imageBuf4;
    public void init()
    image = loadImage("background.jpg");
    createPolygons();
    createImageBufs();
    private Image loadImage(String name)
    MediaTracker tracker = new MediaTracker(this);
    Image image = getImage(getDocumentBase(), name);
    tracker.addImage(image, 0);
    for ( ; ; )
    try { tracker.waitForAll(); } catch (InterruptedException e) { }
    if (tracker.checkAll())
    break;
    return image;
    public void createPolygons()
    double sqrt2 = Math.sqrt(2);
    int m1 = (int) (polyWidth / (2 + sqrt2));
    int m2 = (int) (polyWidth * sqrt2 / (2 + sqrt2));
    octagon = new Polygon();
    octagon.addPoint(-m2/2, -(m1 + m2/2));
    octagon.addPoint(+m2/2, -(m1 + m2/2));
    octagon.addPoint(+(m1 + m2/2), -(m2/2));
    octagon.addPoint(+(m1 + m2/2), +(m2/2));
    octagon.addPoint(+m2/2, +(m1 + m2/2));
    octagon.addPoint(-m2/2, +(m1 + m2/2));
    octagon.addPoint(-(m1 + m2/2), +(m2/2));
    octagon.addPoint(-(m1 + m2/2), -(m2/2));
    ellipse = new Ellipse2D.Double(-polyWidth/2, -polyWidth/2, polyWidth, polyWidth*3/4);
    ornament = new GeneralPath();
    int l = polyWidth/2;
    int m = polyWidth/16;
    int s = polyWidth/32;
    ornament.moveTo(+0+0, -l+0);
    ornament.quadTo(+0+s, -l+0, +0+s, -l+s);
    ornament.quadTo(+m+0, -m+0, l-s, 0-s);
    ornament.quadTo(+l+0, +0-s, l0, +0+0);
    ornament.quadTo(+l+0, +0+s, l-s, 0+s);
    ornament.quadTo(+m+0, m0, +0+s, +l-s);
    ornament.quadTo(+0+s, l-0, 0+0, l0);
    ornament.quadTo(+0-s, l0, +0-s, +l-s);
    ornament.quadTo(-m+0, m0, -l+s, +0+s);
    ornament.quadTo(-l+0, +0+s, -l+0, +0+0);
    ornament.quadTo(-l+0, +0-s, -l+s, +0-s);
    ornament.quadTo(-m+0, -m+0, +0-s, -l+s);
    ornament.quadTo(+0-s, -l+0, -0+0, -l+0);
    public void createImageBufs()
    Graphics2D g2d;
    Composite saveAlpha;
    // Figure 1
    // Create image buf and get context
    imageBuf1 = new BufferedImage(polyWidth, polyHeight,
    BufferedImage.TYPE_INT_ARGB_PRE);
    g2d = (Graphics2D) imageBuf1.getGraphics();
    // Fill with transparent color
    saveAlpha = g2d.getComposite();
    g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.CLEAR,
    0.0f));
    g2d.fillRect(0, 0, polyWidth, polyHeight);
    g2d.setComposite(saveAlpha);
    // Draw figure
    g2d.translate(polyWidth/2, polyHeight/2);
    g2d.setClip(octagon);
    g2d.setColor(Color.blue);
    g2d.fillRect(-polyWidth/2, -polyHeight/2, polyWidth, polyHeight);
    // Figure 2
    // Create image buf and get context
    imageBuf2 = new BufferedImage(polyWidth, polyHeight,
    BufferedImage.TYPE_INT_ARGB_PRE);
    g2d = (Graphics2D) imageBuf2.getGraphics();
    // Fill with transparent color
    saveAlpha = g2d.getComposite();
    g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.CLEAR,
    0.0f));
    g2d.fillRect(0, 0, polyWidth, polyHeight);
    g2d.setComposite(saveAlpha);
    // Draw figure
    g2d.translate(polyWidth/2, polyHeight/2);
    g2d.setClip(octagon);
    g2d.drawImage(image, -polyWidth/2, -polyWidth/2, null);
    // Figure 3
    // Create image buf and get context
    imageBuf3 = new BufferedImage(polyWidth, polyHeight,
    BufferedImage.TYPE_INT_ARGB_PRE);
    g2d = (Graphics2D) imageBuf3.getGraphics();
    // Fill with transparent color
    saveAlpha = g2d.getComposite();
    g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.CLEAR, 0.0f));
    g2d.fillRect(0, 0, polyWidth, polyHeight);
    g2d.setComposite(saveAlpha);
    // Draw figure
    g2d.translate(polyWidth/2, polyHeight/2);
    g2d.setClip(ellipse);
    g2d.drawImage(image, -polyWidth/2, -polyWidth*5/8, null);
    // Figure 4
    // Create image buf and get context
    imageBuf4 = new BufferedImage(polyWidth, polyHeight, BufferedImage.TYPE_INT_ARGB_PRE);
    g2d = (Graphics2D) imageBuf4.getGraphics();
    // Fill with transparent color
    saveAlpha = g2d.getComposite();
    g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.CLEAR, 0.0f));
    g2d.fillRect(0, 0, polyWidth, polyHeight);
    g2d.setComposite(saveAlpha);
    // Draw figure
    g2d.translate(polyWidth/2, polyHeight/2);
    g2d.setClip(ornament);
    g2d.drawImage(image, -polyWidth/2, -polyWidth/2, null);
    public void paint(Graphics g)
    g.drawImage(imageBuf1, polyOfsX1 - polyWidth/2, polyOfsY1 - polyHeight/2, null);
    g.drawImage(imageBuf2, polyOfsX2 - polyWidth/2, polyOfsY2 - polyHeight/2, null);
    g.drawImage(imageBuf3, polyOfsX3 - polyWidth/2, polyOfsY3 - polyHeight/2, null);
    g.drawImage(imageBuf4, polyOfsX4 - polyWidth/2, polyOfsY4 - polyHeight/2, null);
    Polygons.html :
    <applet
    code=Polygons.class
    name=Polygons
    width=360
    height=300>
    </applet>

    Think you'll be lucky to find anything in the forum on push down automata. Do a search on google, perhaps with the keyword "parser" or "grammer checker" thrown in. There may well be whole books devoted to it!

  • Please Help : Big problem with applets

    Hi,
    I have a big problem which I have not been able to figure out. The applet I have enclosed works fine on some computers but not on others .
    I am running :
    java version "1.3.1_02"
    Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.1_02-b02)
    Java HotSpot(TM) Client VM (build 1.3.1_02-b02, mixed mode)
    The applet runs perfectly Ok at university but not on other computers.
    The objective of the applet is to display 3 shapes on the screen once. On the university computer it works, however at home and work the 3 shapes are continuously displayed on the screen ( like they are in some infinite loop). I have been struggling with this for days any help would be greatly appreciated.
    It would help me if some of you could run this applet and let me know if the shapes are displayed once or if the shapes are continuously updated on the screen ( assuming that the applet window is not resized covered over etc. )
    =========================================
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    /*<html><applet code = "stage1.class" height = "600" width="810"></applet></html>*/
    public class stage1 extends JApplet{
    JPanel extraPanel = new JPanel();
    public void init()
    Container c = getContentPane();
    extraPanel.setBackground (Color.white);
    extraPanel.setLayout (new FlowLayout());
    c.add(extraPanel);
    public void paint (Graphics g)
    //super.paint( g ); // call superclass's paint method
    // populate the two dimensional array - store the coordinates for the circle and square shapes
    int circle[][] = { {100,100,200,200}, // display circle in the left hand side of the screen
    {350,100,200,200}, // display circle in the middle of the screen
    {600,100,200,200} }; // display circle in the right hand side of the screen
    int rect[][] = { {100,100,200,200}, // display square in the left hand side of the screen
    {350,100,200,200}, // display square in the middle of the screen
    {600,100,200,200} }; // display square in the right hand side of the screen
    // populate the two dimensional array - store the coordinates for the polygon shapes
    int polyX[][] = { {150,250,300,250,150,100 }, // display polygon in the left hand side of the screen
    {400,500,550,500,400,350 }, // display polygon in the middle of the screen
    {650,750,800,750,650,600} }; // display polygon in the right hand side of the screen
    int polyY[] = { 100,100,200,300,300,200    }; // the "Y" co-ordinates wont change for all three polygons
    int xCoordinates[] = new int[ 6 ];
    int pickAShape
    , pickAColour
    , polygonCount = 0
    , circleCount = 0
    , squareCount = 0
    , redCount = 0
    , greenCount = 0
    , blueCount = 0
    , yellowCount = 0 ;
    for ( int position = 0;position < 3;position++ )
    pickAShape = 1 + ( int )( Math.random() * 3 );
    pickAColour = 10 + ( int )( Math.random() * 4 );
    switch ( pickAShape )
    case 1: // draw a circle
    switch ( pickAColour )
    case 10:
    g.setColor( Color.red );
    redCount++;
    break;
    case 11:
    g.setColor( Color.green );
    greenCount++;
    break;
    case 12:
    g.setColor( Color.blue );
    blueCount++;
    break;
    case 13:
    g.setColor( Color.yellow );
    yellowCount++;
    break;
    g.fillOval( circle[position][0],circle[position][1],circle[position][2],circle[position][3] );
    circleCount++;
    break;
    case 2: // draw a rectangle
    switch ( pickAColour )
    case 10:
    g.setColor( Color.red );
    redCount++;
    break;
    case 11:
    g.setColor( Color.green );
    greenCount++;
    break;
    case 12:
    g.setColor( Color.blue );
    blueCount++;
    break;
    case 13:
    g.setColor( Color.yellow );
    yellowCount++;
    break;
    g.fillRect(rect[position][0],rect[position][1],rect[position][2],rect[position][3] );
    squareCount++;
    break;
    case 3: // draw a polygon
    switch ( pickAColour )
    case 10:
    g.setColor( Color.red );
    redCount++;
    break;
    case 11:
    g.setColor( Color.green );
    greenCount++;
    break;
    case 12:
    g.setColor( Color.blue );
    blueCount++;
    break;
    case 13:
    g.setColor( Color.yellow );
    yellowCount++;
    break;
    xCoordinates[0]=polyX[position][0];
    xCoordinates[1]=polyX[position][1];
    xCoordinates[2]=polyX[position][2];
    xCoordinates[3]=polyX[position][3];
    xCoordinates[4]=polyX[position][4];
    xCoordinates[5]=polyX[position][5];
    Polygon polygonInstance = new Polygon ( xCoordinates, polyY, 6 );
    g.fillPolygon( polygonInstance );
    polygonCount++;
    break;
    // display the results
    setFont(new Font("Serif",Font.BOLD,24));
    g.setColor(Color.black);
    if ((circleCount ==3 || squareCount ==3 || polygonCount==3) && (redCount ==3 || greenCount ==3 || blueCount==3 || yellowCount ==3))
    g.drawString( "YOU WIN !!!!" ,380,400 );
    else
    g.drawString( "YOU LOSE !!!!",380,400 );
    } // end method paint
    } // end class stage4
    =========================================
    Regards and thanks

    O.K. You will not believe this, but the problem will be solved if you change the following line from:
        setFont(new Font("Serif",Font.BOLD,24));to:
        g.setFont(new Font("Serif",Font.BOLD,24));Now, since I am also learning JAVA, the way I went about solving was to create your applet from scratch, using cut and paste, and running it piece by piece till I ran into the flashing display. Took me a while. I am enclosing my version of the program for your comments.
    import java.awt.*;
    import java.applet.*;
    public class Slotto extends Applet {
        int circle[][] = { {100,100,200,200}, // circle in the left hand side of the screen
                           {350,100,200,200}, // circle in the middle of the screen
                           {600,100,200,200} }; // circle in the right hand side of the screen
        int rect[][] = { {100,100,200,200}, // square in the left hand side of the screen
                         {350,100,200,200}, // square in the middle of the screen
                         {600,100,200,200} }; // square in the right hand side of the screen
        int polyX[][] = { {150,250,300,250,150,100 }, // polygon in the left hand side of the screen
                          {400,500,550,500,400,350 }, // polygon in the middle of the screen
                          {650,750,800,750,650,600} }; // polygon in the right hand side of the screen
        int polyY[] = { 100,100,200,300,300,200 }; // the "Y" co-ordinates wont change for all three polygons
        int xCoordinates[] = new int[ 6 ];
        int pickAShape
        , pickAColour
        , polygonCount = 0
        , circleCount = 0
        , squareCount = 0
        , redCount = 0
        , greenCount = 0
        , blueCount = 0
        , yellowCount = 0
        , position ;
        public void paint(Graphics g) {
            for (position = 0; position < 3; position++) {
                chooseColor(g);
                drawTheShape(g);
            declareResults(g);
        public void chooseColor(Graphics g) {
            pickAColour = 10 + ( int )( Math.random() * 4 );
            switch ( pickAColour )
                case 10:
                    g.setColor( Color.red );
                    redCount++;
                    break;
                case 11:
                    g.setColor( Color.green );
                    greenCount++;
                    break;
                case 12:
                    g.setColor( Color.blue );
                    blueCount++;
                    break;
                case 13:
                    g.setColor( Color.yellow );
                    yellowCount++;
                    break;
        public void drawTheShape(Graphics g) {
            pickAShape = 1 + ( int )( Math.random() * 3 );
            switch ( pickAShape )
                case 1: // draw a Circle
                    g.fillOval( circle[position][0],
                                circle[position][1],
                                circle[position][2],
                                circle[position][3] );
                    circleCount++;
                    break;
                case 2: // draw a Square
                    g.fillRect(rect[position][0],
                               rect[position][1],
                               rect[position][2],
                               rect[position][3] );
                    squareCount++;
                    break;
                case 3: // draw a Polygon
                    xCoordinates[0]=polyX[position][0];
                    xCoordinates[1]=polyX[position][1];
                    xCoordinates[2]=polyX[position][2];
                    xCoordinates[3]=polyX[position][3];
                    xCoordinates[4]=polyX[position][4];
                    xCoordinates[5]=polyX[position][5];
                    Polygon polygonInstance = new Polygon ( xCoordinates,
                                                            polyY, 6 );
                    g.fillPolygon( polygonInstance );
                    polygonCount++;
                    break;
        public void declareResults(Graphics g) {
            g.setFont(new Font("Serif",Font.BOLD,24));
            g.setColor(Color.black);
            if ((circleCount ==3 || squareCount ==3 || polygonCount==3)
             && (redCount ==3 || greenCount ==3 || blueCount==3 || yellowCount ==3))
                g.drawString( "YOU WIN !!!!" ,380,400 );
            else
                g.drawString( "YOU LOSE !!!!",380,400 );
    }Regards.

  • Linking from a applet

    Hi,
    I am making a image map applet where depending on the area(polygon shapes) that you click on the image the applet needs to direct the user to different places. So my basic problem is how do i link to a http page from the applet?
    can anyone pls help with this.. thanks in advance..
    udam

    try calling this from your applet:
    getAppletContext().showDocument(new URL("http://java.sun.com/"));of course, if you want to show it in a new window:
    getAppletContext().showDocument(new URL("http://java.sun.com/"),"_blank");

  • JVue applet integration/extension

    I'm trying to communicate with the AutoVue applet in a browser.
    My initial efforts were based on the suggestions in http://docs.oracle.com/cd/E29155_02/otn/pdf/E29158_01.pdf. This document suggests writing a second applet and use getAppletContext().getApplet(”Jvue”) to access the Jvue applet.
    But in this document http://docs.oracle.com/javase/tutorial/deployment/applet/iac.html it is stated that getApplet should NOT be used for communication between applets. Probably because the security around applets has been tightened lately.
    Next attempt is to have only a single applet. To keep the existing look and functionality of AutoVue, I extend JVue and implement my own methods that can access the VueControllers and be called from javascript.
    The problem right now is to get this applet to launch.
    I have tried to use the APPLET tag in html, but couldn't get the loading of the extra jars to work.
    Now I'm trying to launch the applet with jnlp, but no matter what I do I get a SecurityException:
    java.lang.SecurityException: JAR manifest requested to run in all-permissons only: http://localhost/plugin/autovue/cfmAvue.jar
        at com.sun.deploy.security.DeployManifestChecker.verify(Unknown Source)
        at com.sun.deploy.security.DeployManifestChecker.verify(Unknown Source)
        at com.sun.deploy.security.SandboxSecurity.isPermissionGranted(Unknown Source)
        at com.sun.javaws.security.JNLPSignedResourcesHelper.checkSignedResourcesHelper(Unknown Source)
        at com.sun.javaws.security.JNLPSignedResourcesHelper.checkSignedResources(Unknown Source)
        at sun.plugin2.applet.JNLP2Manager.prepareLaunchFile(Unknown Source)
        at sun.plugin2.applet.JNLP2Manager.loadJarFiles(Unknown Source)
        at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
        at java.lang.Thread.run(Unknown Source)
    In my manifest for the applet jar I have tried "Permissions: Sandbox", "Permissions: All-permissions" and omitting the Permissions.
    In my jnlp I have tried  <security><all-permissions/></security> and omitting the security.
    <?xml version="1.0" encoding="UTF-8"?>
    <jnlp spec="1.0+" >
        <information>
            <title>CoreFM AutoVue Applet</title>
            <vendor>Byggeweb/Docia</vendor>
            <homepage href="www.docia.com"></homepage>
        </information>
        <resources>
            <!-- Application Resources -->
            <j2se version="1.7+"
                href="http://java.sun.com/products/autodl/j2se" />
            <jar href="cfmAvue.jar" main="true" />
            <jar href="jvue.jar"></jar>
            <jar href="jogl.jar"></jar>
            <jar href="gluegen-rt.jar"></jar>
        </resources>
        <applet-desc
             name="CoreFM AutoVue Applet"
             main-class="com.docia.cfm.autovue.CFM_Avue_Applet.class"
             width="1"
             height="1">
         </applet-desc>
         <update check="background"/>
    </jnlp>        
    In html I have tried "permissions: 'sandbox'", "permissions: 'all-permissions'" and omitting it.
    <script src="https://www.java.com/js/deployJava.js"></script>
    <script>
      var attributes = { code:'com.docia.cfm.autovue.CFM_Avue_Applet',  width:'100%',  height:'100%'} ;
      var parameters = {jnlp_href: '/plugin/autovue/cfmAvue.jnlp',
                        permissions: 'sandbox',
                        EMBEDDED: 'true',
                        VERBOSE: 'false',
                        ONINIT: 'onAppletInit();',
                        LOCALE: 'EN',
                        JVUESERVER: '<%= Configuration.getValue( "AUTOVUE_SERVLET_SERVER")%>'} ;
      deployJava.runApplet(attributes, parameters, '1.7');
    </script>               
    Can anyone tell me what combination will work?
    Preferably the solution should work for java 6 and all versions of java 7.
    Thanks
    Michael Bjerregaard-Pedersen

    What I'm trying to implement is just to be able to paint a polygon or highlight an IfcSpace in a drawing shown in AutoVue.
    As I don't much like the idea of implementing the bulk of the necessary code for this in javascript, I'm looking for a way to implement it in java and just use javascript to call highlevel functions from the webpage.
    When I use this applet tag:
    <APPLET
        NAME="JVue"
        CODE="com.docia.cfm.autovue.CFM_Avue_Applet"
        CODEBASE="//<%= Configuration.getValue("HostUrl")%>/plugin/autovue"
        ARCHIVE="cfmAvue.jar, jvue.jar,jogl.jar,gluegen-rt.jar"
        HSPACE="0" VSPACE="0"
        WIDTH="100%" HEIGHT="100%"
        MAYSCRIPT="on"
    >
    <PARAM NAME="EMBEDDED" VALUE="true">
    <PARAM NAME="VERBOSE" VALUE="false">
    <PARAM NAME="ONINIT" VALUE="onAppletInit();">
    <PARAM NAME="LOCALE" VALUE="EN">
    <PARAM NAME="JVUESERVER" VALUE="<%= Configuration.getValue( "AUTOVUE_SERVLET_SERVER")%>">
    <p><b>Requires a browser that supports Java.</b></p>
    </APPLET>
    to launch my applet that is basically just this:
    public class CFM_Avue_Applet extends JVue {
        public void init()
            super.init();
        public void start()
            super.start();
    Compiled with this manifest:
    Application-Name: CoreFM AutoVue Applet
    Manifest-Version: 1.0
    Permissions: all-permissions
    Codebase: executor.interlogic.com.ua testserver.corefm.dk mbp.corefm.int corefm.dk byggeweb.dk docia.com
    Caller-Allowable-Codebase: executor.interlogic.com.ua testserver.corefm.dk mbp.corefm.int corefm.dk byggeweb.dk docia.com
    Application-Library-Allowable-Codebase: executor.interlogic.com.ua testserver.corefm.dk mbp.corefm.int corefm.dk byggeweb.dk docia.com
    Extension-List: jvue jogl gluegen-rt
    jvue-Extension-Name: com.cimmetry.jvue.JVue
    jvue-Specification-Version: 1.1
    jvue-Implementation-Version: 1
    jvue-Implementation-Vendor-Id: com.cimmetry
    jvue-Implementation-URL: http://www.byggeweb.dk/plugin/autovue/jvue.jar
    jvue-Application-Name: JVue
    jvue-Permissions: all-permissions
    jvue-Codebase: executor.interlogic.com.ua testserver.corefm.dk mbp.corefm.int corefm.dk byggeweb.dk docia.com
    jogl-Extension-Name: com.cimmetry.jvue.jogl
    jogl-Specification-Version: 1.1
    jogl-Implementation-Version: 1
    jogl-Implementation-Vendor-Id: com.cimmetry
    jogl-Implementation-URL: http://www.byggeweb.dk/plugin/autovue/jvue.jar
    jogl-Application-Name: jogl
    jogl-Permissions: all-permissions
    jogl-Codebase: executor.interlogic.com.ua testserver.corefm.dk mbp.corefm.int corefm.dk byggeweb.dk docia.com
    gluegen-rt-Extension-Name: com.cimmetry.jvue.gluegen-rt
    gluegen-rt-Specification-Version: 1.1
    gluegen-rt-Implementation-Version: 1
    gluegen-rt-Implementation-Vendor-Id: com.cimmetry
    gluegen-rt-Implementation-URL: http://www.byggeweb.dk/plugin/autovue/jvue.jar
    gluegen-rt-Application-Name: gluegen-rt
    gluegen-rt-Permissions: all-permissions
    gluegen-rt-Codebase: executor.interlogic.com.ua testserver.corefm.dk mbp.corefm.int corefm.dk byggeweb.dk docia.com
    I first get a warning that an applet will be running, which is OK.
    Then I get a message that the applet will load jvue.jar, which is annoying and will confuse all users. (as far as I can tell JNLP should solve this, but AutoVue is apparently not compatible with JNLP, says Oracle)
    After that I get this in the java console:
    Missing Application-Name: manifest attribute for: http://www.byggeweb.dk/plugin/autovue/jvue.jar
    Missing Permissions manifest attribute for: http://www.byggeweb.dk/plugin/autovue/jvue.jar
    Missing Codebase manifest attribute for: http://www.byggeweb.dk/plugin/autovue/jvue.jar
    Missing Application-Name: manifest attribute for: http://www.byggeweb.dk/plugin/autovue/jvue.jar
    Missing Permissions manifest attribute for: http://www.byggeweb.dk/plugin/autovue/jvue.jar
    Missing Codebase manifest attribute for: http://www.byggeweb.dk/plugin/autovue/jvue.jar
    Missing Application-Name: manifest attribute for: http://www.byggeweb.dk/plugin/autovue/jvue.jar
    Missing Permissions manifest attribute for: http://www.byggeweb.dk/plugin/autovue/jvue.jar
    Missing Codebase manifest attribute for: http://www.byggeweb.dk/plugin/autovue/jvue.jar
    Missing Application-Name: manifest attribute for: http://www.byggeweb.dk/plugin/autovue/jvue.jar
    Missing Permissions manifest attribute for: http://www.byggeweb.dk/plugin/autovue/jvue.jar
    Missing Codebase manifest attribute for: http://www.byggeweb.dk/plugin/autovue/jvue.jar
    Missing Application-Name: manifest attribute for: http://www.byggeweb.dk/plugin/autovue/jvue.jar
    Something is looping.
    Thanks
    Michael

  • Speed up applet

    Hi!
    I�ve made a minigolf applet that�s runs slow on most computers, I know that the problem lies with the drawing of the course. You can check it out at http://130.243.99.7/pph/pph0215/websida/java/minigolf_jar.htm.
    What can I do to speed it up?
    My course image is 27 parts that I cut out to make the courses.
    My code looks like this:
    public class threadPanel_c extends Panel
    implements Runnable, MouseListener, MouseMotionListener
    private golfCourse_c course;
    private golfBall_c ball;
    private int nrOfStrokes;
    private Graphics bgG;
    private Image courseImage, ballImage;
    private Image bgImage;
    private Thread thread;
    private gameApplet_c parent;
    private Polygon coursePolygon, obstaclePolygons[];
    private int holeX, holeY;
    private int mouseX, mouseY;
    private boolean isStandalone = false;
    //Construct the applet
    public threadPanel_c(gameApplet_c parent)
    this.parent = parent;
    public void run()
    String strokes, nrOf;
    Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
    this.nrOfStrokes++;
    strokes = new String().valueOf(this.nrOfStrokes);
    nrOf = "Antal slag: ";
    this.parent.strokes_lbl.setText(nrOf + strokes );
    while ( !this.ball.stop() )
    this.ball.moveBall();
    this.repaint();
    try
    Thread.sleep (this.ball.getSpeed());
    catch (InterruptedException ex)
    Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
    if(this.ball.getballInHole() || this.nrOfStrokes >= 7)
    if(this.nrOfStrokes >= 7)
    this.nrOfStrokes = 7;
    parent.next(this.nrOfStrokes);
    public void paint (Graphics g)
    this.course.initialize();
    int part = 0;
    do
    this.course.switchXY(part);
    g.drawImage( this.courseImage, this.course.getDX1(),
    this.course.getDY1(),
    this.course.getDX2(), this.course.getDY2(),
    this.course.getSX1(),
    this.course.getSY1(), this.course.getSX2(),
    this.course.getSY2(), this );
    part++;
    }while( this.course.hasNext() );
    if( !this.ball.getballInHole() )
    g.drawImage( this.ballImage, this.ball.getX()-5,
    this.ball.getY()-5, this);
    if( this.ball.stop() )
    double a = Math.abs(this.mouseX - this.ball.getX());
    double b = Math.abs(this.mouseY - this.ball.getY());
    if( a < 1 )
    a = 1;
    if( b < 1 )
    b = 1;
    double hypo = Math.sqrt(Math.pow(a , 2.0) + Math.pow(b, 2.0));
    if( hypo < 150 && !this.ball.getballInHole() )
    g.setColor(Color.white);
    g.drawLine(this.ball.getX(), this.ball.getY(), this.mouseX, this.mouseY);
    public void update (Graphics g)
    if (this.bgImage == null)
    this.bgImage = createImage (this.getSize().width, this.getSize().height);
    this.bgG = bgImage.getGraphics ();
    this.bgG.setColor (getBackground ());
    this.bgG.fillRect (0, 0, this.getSize().width, this.getSize().height);
    this.bgG.setColor (getForeground());
    this.paint (bgG);
    g.drawImage (bgImage, 0, 0, this);
    public void mouseMoved( MouseEvent e)
    if(this.ball.stop())
    this.mouseX = e.getX();
    this.mouseY = e.getY();
    if( !this.ball.getballInHole() )
    this.repaint();
    public void mouseClicked(MouseEvent event)
    public void mousePressed(MouseEvent event)
    public void mouseDragged( MouseEvent e )
    public void mouseReleased(MouseEvent event)
    if( this.ball.stop() )
    double a = Math.abs(this.mouseX - this.ball.getX());
    double b = Math.abs(this.mouseY - this.ball.getY());
    if( a < 1 )
    a = 1;
    if( b < 1 )
    b = 1;
    double hypo = Math.sqrt(Math.pow(a , 2.0) + Math.pow(b, 2.0));
    if( hypo < 150 )
    int speed = new Double(hypo).intValue();
    this.ball.hitBall(this.mouseX, this.mouseY, speed);
    this.thread = new Thread( this );
    this.thread.start();
    public void mouseEntered(MouseEvent event)
    public void mouseExited(MouseEvent event)
    //Course initialize
    public void initialize(Polygon course, int holeX, int holeY,
    Polygon obstacle[], int imgParts[], int partX[], int partY[])
    this.coursePolygon = course;
    this.obstaclePolygons = obstacle;
    this.holeX = holeX;
    this.holeY = holeY;
    this.nrOfStrokes = 0;
    this.course = new golfCourse_c( imgParts, partX, partY );
    this.courseImage = this.parent.getImage( this.parent.getCodeBase(), "all_im_j.jpg" );
    this.ball = new golfBall_c( 40,60 );
    this.ballImage = this.parent.getImage( this.parent.getCodeBase(), "boll.gif" );
    this.ball.setWorld( this.coursePolygon, this.holeX, this.holeY, this.obstaclePolygons );
    this.init();
    //Applet initialize
    public void init()
    try
    jbInit();
    catch(Exception e)
    e.printStackTrace();
    MediaTracker tracker;
    tracker = new MediaTracker(this);
    tracker.addImage(this.courseImage, 0);
    tracker.addImage(this.ballImage, 0);
    try
    tracker.waitForAll();
    catch( InterruptedException ie )
    this.addMouseListener( this );
    this.addMouseMotionListener( this );
    //Component initialization
    private void jbInit() throws Exception
    //Get Applet information
    public String getAppletInfo()
    return "Applet Information";
    //Get parameter info
    public String[][] getParameterInfo()
    return null;
    }

    Create an offscreen image and paint your course images once. Then you have only one call to drawImage in your paint method. All other elements that needs to be painted must remain in your paint method.
    But the worst thing is that you call course.initialize() in your paint method. I guess it is not necessary to initialize the course every time it gets painted (especially the loading of the images).
    J&ouml;rg

  • Render Swing on 3D Surface APPLET

    Here is the purpose. In Java 3D we can set Image, even video (using JMF) on a 3D surface in my APPLET.
    I would like to display a clickable Swing component, HTML or RSS on a 3D surface of a cube in an Applet. It's been several weeks I am on this problem and I am blocked.
    Does anyone have worked on this case or have a solution ?
    Anthony

    I made a mesh viewer and am working on a GUI generator that takes XHTML as input so these kinds of problems are on my mind a lot.
    Rendering a Java object in 3D isn't too hard because you could just invoke it's paint method providing it with an image that could be mapped as a texture on a 3D object.
    For RSS and XHTML, you could use standard XML parsers in the API to parse them but you'd have to write your own methods to make a graphical representation of them. Rendering the very unrestricted HTML that is often used in websites is extremely difficult.
    For interaction with the 3D objects such as making working clicks, it could be done a few different ways.
    1. You could have a hidden buffer. In this buffer, you could keep the polygons for each interactive region drawn as indexed parts. It would be updated as you render the display. When the display area is clicked, it could check the hidden buffer at the clicked point to see what hyperlink, button, or whatever interactive component's action should be invoked.
    2. You could handle each click, mousemove, or whatever by casting a ray into the model to see what it intersects. I think option one might be easier.
    After you find that a certain link has been clicked, you know what to do in Java but to change the page, the first solution that comes to mind for me is in JavaScript. In JavaScript, you would set the page location using document.location.href. Your applet can invoke JavaScript functions using a few techniques described here: http://www.buttontool.com/50help/jscript.htm

  • Multi-colored polygon?

    I want to make a multi-colored polygon, as in every pixel of the polygon is a different color. I looked in the java.awt.Polygon documentation, but they didn't have anything like that. Another thing I want to do is have it flexible- like if it hits the wall, the whole polygon won't keep it's shape and bounce off. The pixels should bounce off as they hit the wall. I did make a code for all of that, but I was wondering if there's an easier way. Look:
    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
    public class Happy extends Applet implements Runnable {
         Image buffer;
         Graphics2D gfx;
         int[] dotx, doty, xdir, ydir;
         int speed;
         Color[] color, random;
         Thread thread;
         public void init() {
              int j;
              thread = new Thread(this);
              thread.start();
              random = new Color[] {Color.red,Color.green,Color.blue,Color.magenta,Color.yellow,Color.cyan,Color.yellow};
              buffer = null;
              makeDots();
         private void makeDots() {
              int j,x,y;
              dotx = new int[320];
              doty = new int[320];
              xdir = new int[320];
              ydir = new int[320];
              color = new Color[320];
              speed = 1;
              x = 100;
              y = 100;
              for (j = 0;j < 20;j++,x-=2) makeDot(j,x,y); // top
              for (y+=2,j = 20;j < 40;j++,x-=2,y+=2) makeDot(j,x,y); // top-left
              for (j = 40;j < 60;j++,y+=2) makeDot(j,x,y); // left
              for (x+=2,j = 60;j < 80;j++,x+=2,y+=2) makeDot(j,x,y); // bottom-left
              for (j = 80;j < 100;j++,x+=2) makeDot(j,x,y); // bottom
              for (y-=2,j = 100;j < 120;j++,x+=2,y-=2) makeDot(j,x,y); // bottom-right
              for (j = 120;j < 140;j++,y-=2) makeDot(j,x,y); // right
              for (x-=2,j = 140;j < 160;j++,x-=2,y-=2) makeDot(j,x,y); // top right
              makeSquare(160,50,130); // left eye
              makeSquare(220,95,130); // right eye
              x = 42;
              y = 170;
              for (j = 280;j < 290;j++,x+=2,y+=2) makeDot(j,x,y); // left side of mouth
              for (j = 290;j < 310;j++,x+=2) makeDot(j,x,y); // mouth
              for (y-=2,j = 310;j < 320;j++,x+=2,y-=2) makeDot(j,x,y); // right side of mouth
         private void makeSquare(int i, int startx, int starty) {
              int j;
              int c = 0;
              int x = startx;
              int y = starty;
              for (j = i;j < i+60;j++,x+=2,c++) {
                   makeDot(j,x,y);
                   if (c == 9) {
                        c = -1;
                        x = startx-2;
                        y += 2;
         private void makeDot(int j, int x, int y) {
              dotx[j] = x;
              doty[j] = y;
              xdir[j] = speed;
              ydir[j] = speed;
              color[j] = getRandomColor();
         private Color getRandomColor() {
              return random[(int)Math.round(Math.random()*(random.length-1))];
         public void run() {
              while (true) {
                   checkDots();
                   repaint();
                   try {
                        Thread.sleep(10);
                   catch (InterruptedException e) {}
         private void checkDots() {
              int x,y,j;
              for (j = 0;j < dotx.length;j++) {
                   x = dotx[j];
                   y = doty[j];
                   if ((x <= 0 && xdir[j] < 0) || (x >= getSize().width && xdir[j] > 0)) {
                        xdir[j] -= xdir[j]*2;
                        color[j] = getRandomColor();
                   if ((y <= 0 && ydir[j] < 0) || (y >= getSize().height && ydir[j] > 0)) {
                        ydir[j] -= ydir[j]*2;
                        color[j] = getRandomColor();
                   dotx[j] += xdir[j];
                   doty[j] += ydir[j];
         public void paint(Graphics g) {
              int j;
              if (buffer == null) {
                   buffer = createImage(getSize().width,getSize().height);
                   gfx = (Graphics2D)buffer.getGraphics();
              gfx.setColor(Color.black);
              gfx.fillRect(0,0,getSize().width,getSize().height);
              for (j = 0;j < dotx.length;j++) {
                   gfx.setColor(color[j]);
                   gfx.fillRect(dotx[j],doty[j],2,2);
              g.drawImage(buffer,0,0,null);
         public void update(Graphics g) {
              paint(g);
    }This is what it does: http://smigs.co.uk/skittles/Happy.html
    The main part to look at in the code is the makeDots() method. As you can see, it's quite messy. Is there some easier way? (If there isn't, that's okay- I just want to know)

    I can give you 2 programs, that I did with random images - it should get you going, though I did them when I was a newbie myself!!! So, I'm sure you could improve on them. Neither of them are 'exactly' what you want, but will - no doubt - give you ideas;-
    ONE: wasn't what I was looking for ...I do have 2 better one's somewhere.import javax.swing.*;
    import java.awt.Graphics;
    import java.awt.Color;
    public class randImg extends JApplet {
       int a  = (int)(Math.random()*280);          // Rectangle position
       int b  = (int)(Math.random()*280);
       int c  = (int)(Math.random()*100);          // Rectangle size
       int d  = (int)(Math.random()*100);
       int red = (int)(Math.random()*255);          // Rectangle colours
       int green = (int)(Math.random()*255);
       int blue = (int)(Math.random()*255);
       public void init() {
          setBackground(Color.white);
       public void paint(Graphics g) {
            for (int i=0 ; i<=10 ; i++) {       
            g.drawRect( 0, 0, 300, 300);
            g.drawRect( a, b, c, d);
            g.fillRect( a, b, c, d);  
            g.setColor( new Color( red, green, blue) );
    }TWO: yeah its a bit primitive, but ... whatever random polygon shapes and one of 6 random sounds.
    import java.applet.*;
    import java.awt.*;
    import java.awt.Graphics;
    import java.awt.Color;
    import java.awt.Polygon;
    public class fillShapes extends Applet {
       private AudioClip sound1;
       private AudioClip sound2;
       private AudioClip sound3;
       private AudioClip sound4;
       private AudioClip sound5;
       private AudioClip sound6;
       public void init() {
       sound1 = getAudioClip (getDocumentBase(),"sound1.au");
       sound2 = getAudioClip (getDocumentBase(),"sound2.au");
       sound3 = getAudioClip (getDocumentBase(),"sound3.au");
       sound4 = getAudioClip (getDocumentBase(),"sound4.au");
       sound5 = getAudioClip (getDocumentBase(),"sound5.au");
       sound6 = getAudioClip (getDocumentBase(),"sound6.au");
       setBackground(Color.white);
       public void paint(Graphics g) {
         g.setColor(new Color(225, 235, 255));
         g.fillRect(0,0,900,350);  
            int s = 0;       
            do {
            int a  = (int)(Math.random()*240);          
               int b  = 12 + (int)(Math.random()*240);
               int c  = 15 + (int)(Math.random()*244);          
               int d  = 15 + (int)(Math.random()*344);
            int e  = (int)(Math.random()*244);          
               int f  = (int)(Math.random()*344);
            int h  = (int)(Math.random()*344);
            int j  = (int)(Math.random()*244);
            int l  = (int)(Math.random()*244);
            int m  = 5 + (int)(Math.random()*395);          
               int n  = (int)(Math.random()*395);
            int o  = 5 + (int)(Math.random()*399);
            int p  = 5 + (int)(Math.random()*399);
            int u  = (int)(Math.random()*244);          
               int red = (int)(Math.random()*255);          
               int green = (int)(Math.random()*255);
               int blue = (int)(Math.random()*255);      
            g.setColor( new Color( red, green, blue) );
            int xVal[] = {a,b,c,d,e,f,h};
            int yVal[] = {j,l,m,n,o,p,u};                  
            g.fillPolygon(xVal, yVal, 7);       
            ++s;
                } while (s <= 4);
            int i = 0;         
            do {
            int a  = (int)(Math.random()*270);          
               int b  = (int)(Math.random()*220);
               int c  = 50 + (int)(Math.random()*399);          
               int d  = 50 + (int)(Math.random()*399);
               int red = (int)(Math.random()*255);          
               int green = (int)(Math.random()*255);
               int blue = (int)(Math.random()*255);
            a = a + 5;
            b = b + 5;        
            g.fillOval(a, b, c, d);         
            g.setColor( new Color( red, green, blue) );
            ++i;
                } while (i <= 3);  
            int k = 0;       
            do {
            int a  = (int)(Math.random()*300);          
               int b  = (int)(Math.random()*200);
               int c  = 50 + (int)(Math.random()*499);          
               int d  = 50 + (int)(Math.random()*499);
               int red = (int)(Math.random()*255);          
               int green = (int)(Math.random()*255);
               int blue = (int)(Math.random()*255);
            a = a + 5;
            b = b + 5;        
            g.fillRect(a, b, c, d);         
            g.setColor( new Color( red, green, blue) );
            ++k;
                } while (k <= 3);
            int x = 0;       
            do {
            int a  = 200 + (int)(Math.random()*300);          
               int b  = 32 + (int)(Math.random()*411);
               int c  = 15 + (int)(Math.random()*425);          
               int d  = 15 + (int)(Math.random()*445);
            int e  = 250+ (int)(Math.random()*495);          
               int f  = 150+ (int)(Math.random()*444);
            int h  = 250+ (int)(Math.random()*244);
            int j  = (int)(Math.random()*444);
            int l  = (int)(Math.random()*444);
            int m  = (int)(Math.random()*445);          
               int n  = 150+ (int)(Math.random()*495);
            int o  = 220 + (int)(Math.random()*455);
            int p  = 129 + (int)(Math.random()*455);
            int u  = (int)(Math.random()*244);          
               int red = (int)(Math.random()*255);          
               int green = (int)(Math.random()*255);
               int blue = (int)(Math.random()*255);
            a = a + 5;
            b = b + 5;
            g.setColor( new Color( red, green, blue) );
            int xVal[] = {a,b,c,d,e,f,h};
            int yVal[] = {j,l,m,n,o,p,u};                  
            g.fillPolygon(xVal, yVal, 7);       
            ++x;
                } while (x <= 4);
            int q = 0;       
            do {
            int a  = 150 + (int)(Math.random()*344);          
               int b  = 12 + (int)(Math.random()*240);
               int c  = 50 + (int)(Math.random()*299);          
               int d  = 50 + (int)(Math.random()*299);
               int red = (int)(Math.random()*255);          
               int green = (int)(Math.random()*255);
               int blue = (int)(Math.random()*255);
            a = a + 5;
            b = b + 5;        
            g.fillOval(a, b, c, d);         
            g.setColor( new Color( red, green, blue) );
            ++q;
                } while (q <= 2);
            int r = 0;       
            do {
            int a  = 200 + (int)(Math.random()*274);          
               int b  = 21 + (int)(Math.random()*240);
               int c  = 50 + (int)(Math.random()*399);          
               int d  = 50 + (int)(Math.random()*399);
               int red = (int)(Math.random()*255);          
               int green = (int)(Math.random()*255);
               int blue = (int)(Math.random()*255);
            a = a + 5;
            b = b + 5;        
            g.fillRect(a, b, c, d);         
            g.setColor( new Color( red, green, blue) );
            ++r;
                } while (r <= 3);
            int t = 0;       
            do {
            int a  = 150 + (int)(Math.random()*314);          
               int b  = 12 + (int)(Math.random()*240);
               int c  = 50 + (int)(Math.random()*399);          
               int d  = 50 + (int)(Math.random()*399);
               int red = (int)(Math.random()*255);          
               int green = (int)(Math.random()*255);
               int blue = (int)(Math.random()*255);
            a = a + 5;
            b = b + 5;        
            g.fillOval(a, b, c, d);         
            g.setColor( new Color( red, green, blue) );
            ++t;
                } while (t <= 2);
            int w = 0;       
            do {
            int a  = 350 + (int)(Math.random()*314);          
               int b  = 21 + (int)(Math.random()*240);
               int c  = 50 + (int)(Math.random()*299);          
               int d  = 50 + (int)(Math.random()*299);
               int red = (int)(Math.random()*255);          
               int green = (int)(Math.random()*255);
               int blue = (int)(Math.random()*255);
            a = a + 5;
            b = b + 5;        
            g.fillRect(a, b, c, d);         
            g.setColor( new Color( red, green, blue) );
            ++w;
                } while (w <= 2);
            int v = 0;       
            do {
            int a  = 350 + (int)(Math.random()*314);          
               int b  = 12 + (int)(Math.random()*240);
               int c  = 50 + (int)(Math.random()*299);          
               int d  = 50 + (int)(Math.random()*299);
               int red = (int)(Math.random()*255);          
               int green = (int)(Math.random()*255);
               int blue = (int)(Math.random()*255);
            a = a + 5;
            b = b + 5;        
            g.fillOval(a, b, c, d);         
            g.setColor( new Color( red, green, blue) );
            ++v;
                } while (v <= 2);
            g.setColor(Color.white);
            g.setFont(new Font("Arial",Font.BOLD,48));
            g.drawString("Welcome to the MULTIMEDIA", 30, 80);
            g.drawString("SUPPORT CENTRE", 100, 160);
            g.drawString("Please sign in below:", 70, 240);
            g.setColor(Color.lightGray);
            g.setFont(new Font("Arial",Font.BOLD,48));
            g.drawString("Welcome to the MULTIMEDIA", 29, 79);
            g.drawString("SUPPORT CENTRE", 99, 159);
            g.drawString("Please sign in below:", 69, 239);
            g.setColor(Color.darkGray);
            g.setFont(new Font("Arial",Font.BOLD,48));
            g.drawString("Welcome to the MULTIMEDIA", 28, 78);
            g.drawString("SUPPORT CENTRE", 98, 158);
            g.drawString("Please sign in below:", 68, 238);
            g.setColor(Color.black);
            g.setFont(new Font("Arial",Font.BOLD,48));
            g.drawString("Welcome to the MULTIMEDIA", 27, 77);
            g.drawString("SUPPORT CENTRE", 97, 157);
            g.drawString("Please sign in below:", 67, 237);
            int red = (int)(Math.random()*255);          
               int green = (int)(Math.random()*255);
               int blue = (int)(Math.random()*255);
            g.setColor(new Color(red, green, blue));
            g.setFont(new Font("Arial",Font.BOLD,48));
            g.drawString("Welcome to the MULTIMEDIA", 26, 76);
            g.drawString("SUPPORT CENTRE", 96, 156);
            g.drawString("Please sign in below:", 66, 236);       
            int sound = 1+(int)(Math.random()*6);
              if (sound == 1)sound1.play();
              else if (sound == 2)sound2.play();
              else if (sound == 3)sound3.play();
              else if (sound == 4)sound4.play();
              else if (sound == 5)sound5.play();
              else sound6.play();
         class delay extends Thread {
            try {
            Thread.sleep(2000);
            catch (InterruptedException exception) {
              System.err.println( exception.toString() );
            repaint();     
    }THREE: Polygon Shapes, I think this was a pre-runner of 'TWO'
    import java.awt.Graphics;
    import java.awt.Color;
    import javax.swing.*;
    import java.awt.Graphics;
    import java.awt.Color;
    import java.awt.Polygon;
    public class polyShapes extends JApplet {  
       public void init() {
       setBackground(Color.white);
       public void paint(Graphics g) {          
            int s = 0;       
            do {
            int a  = (int)(Math.random()*240);          
               int b  = 12 + (int)(Math.random()*340);
               int c  = 15 + (int)(Math.random()*244);          
               int d  = 15 + (int)(Math.random()*344);
            int e  = (int)(Math.random()*244);          
               int f  = (int)(Math.random()*344);
            int h  = (int)(Math.random()*344);
            int j  = (int)(Math.random()*244);
            int l  = (int)(Math.random()*244);
            int m  = 23 + (int)(Math.random()*395);          
               int n  = (int)(Math.random()*395);
            int o  = 15 + (int)(Math.random()*399);
            int p  = 15 + (int)(Math.random()*399);
            int u  = (int)(Math.random()*244);          
               int red = (int)(Math.random()*255);          
               int green = (int)(Math.random()*255);
               int blue = (int)(Math.random()*255);
            a = a + 5;
            b = b + 5;
            g.setColor( new Color( red, green, blue) );
            int xVal[] = {a,b,c,d,e,f,h};
            int yVal[] = {j,l,m,n,o,p,u};                  
            g.fillPolygon(xVal, yVal, 7);       
            ++s;
                } while (s <= 7);
            int x = 0;       
            do {
            int a  = 200 + (int)(Math.random()*400);          
               int b  = 32 + (int)(Math.random()*411);
               int c  = 15 + (int)(Math.random()*425);          
               int d  = 15 + (int)(Math.random()*445);
            int e  = 250+ (int)(Math.random()*495);          
               int f  = 150+ (int)(Math.random()*444);
            int h  = 250+ (int)(Math.random()*244);
            int j  = (int)(Math.random()*444);
            int l  = (int)(Math.random()*444);
            int m  = (int)(Math.random()*445);          
               int n  = 150+ (int)(Math.random()*495);
            int o  = 220 + (int)(Math.random()*455);
            int p  = 129 + (int)(Math.random()*455);
            int u  = (int)(Math.random()*244);          
               int red = (int)(Math.random()*255);          
               int green = (int)(Math.random()*255);
               int blue = (int)(Math.random()*255);
            a = a + 5;
            b = b + 5;
            g.setColor( new Color( red, green, blue) );
            int xVal[] = {a,b,c,d,e,f,h};
            int yVal[] = {j,l,m,n,o,p,u};                  
            g.fillPolygon(xVal, yVal, 7);       
            ++x;
                } while (x <= 7);
    }FOUR: My favourite! Always liked this one (htough 16,000 iterations is arguably excessive) ho-ho ;-
    import javax.swing.*;
    import java.awt.Graphics;
    import java.awt.Color;
    public class randomImg extends JApplet {  
       public void init() {
       setBackground(Color.white);
       public void paint(Graphics g) {     
         int i = 0;       
            do {
            int a  = (int)(Math.random()*295);          // Rectangle position
               int b  = (int)(Math.random()*295);
               int c  = (int)(Math.random()*7);          // Rectangle size
               int d  = (int)(Math.random()*7);
               int red = (int)(Math.random()*255);          // Rectangle colours
               int green = (int)(Math.random()*255);
               int blue = (int)(Math.random()*255);
            a = a + 5;
            b = b + 5;        
            g.fillOval(a, b, c, d);  
            g.setColor( new Color( red, green, blue) );
            ++i;
                } while (i <= 16000);
    }As I said they were all done when I was a java 'baby' and playing with random objects and GUI's as a general concept - so if you come accross some newbie methods, then that would be no surprise.
    Finally - you are no doubt aware of java's awt.geom.Point2D API, that it also worth a look.
    Good luck, Sue x

  • Polygons--stars

    ok--I have to create polygons--figured I would do stars. I need 2 polygons of the same shape--no larger than 120,120 pixels. I'm not sure where to put the 120 pixels in my code. This is what I have--the code for the polygon is near the bottom of all of this. These are my questions: 1. Where does the 120 pixels go in the code?? 2. How do I position the stars where I want them??
    import java.applet.*;
    import java.awt.*;
    public class Project3Appletdemo extends Applet
    public void paint(Graphics gr)
         gr.setColor(Color.blue);
         setBackground(Color.white);
         gr.drawRoundRect(20,20,80,80,0,0);
    gr.drawString("Square", 40, 60);
         Font bigFont = new Font("Helvetica", Font.BOLD, 12);
         gr.setFont(bigFont);
    gr.drawRoundRect(20,20,80,80,0,0);
    gr.setColor(Color.magenta);
    setBackground(Color.white);
         gr.drawString("Rounded", 240, 60);
         gr.drawString("Square", 240,80);
    gr.drawRoundRect(220,20,80,80,40,40);
    gr.setColor(Color.cyan);
    setBackground(Color.white);
         gr.drawString("Circle", 40,220);
    gr.drawRoundRect(20,180,80,80,80,80);
         gr.setColor(Color.green);
         setBackground(Color.white);
         gr.drawOval(190,190,120,60);
         gr.drawString("Oval", 240, 220);
         gr.setColor(Color.red);
         setBackground(Color.white);
         gr.fillRoundRect(20,320,75,75,80,80);
         gr.setColor(Color.white);
         gr.drawString("Filled", 40, 350);
         gr.drawString("Circle", 40, 370);
         gr.setColor(Color.orange);
         setBackground(Color.white);
         gr.fillRect(200,330,120,60);
         gr.setColor(Color.gray);
         gr.drawString("Filled", 240, 350);
         gr.drawString("Rectangle", 240, 370);
         int xPoints[] = {120,182,202,182,190,170,145,158,139,162,120};
         int yPoints[] = {130,190,198,210,245,215,232,205,188,190,130};
         gr.setColor(Color.black);
         gr.drawPolygon(xPoints, yPoints, xPoints.length);
         gr.setColor(Color.orange);
         gr.drawString("Polygon", 130,130);
    }

    1. Where does the 120 pixels go in the code??
    2. How do I position the stars where I want them?? well take in consider the declration of the
    Graphicsmethods and you'll know where to put them...
    take this for example
    public abstract void drawPolygon(int xPoints[], int yPoints[],
                             int nPoints);

  • Copy polygons

    I want to copy the polygon that I have in this code to another location also. How do I do this?? Also how do I make this polygon filled?
    Thanks
    import java.applet.*;
    import java.awt.*;
    public class Project3Appletdemo extends Applet
    public void paint(Graphics gr)
         gr.setColor(Color.blue);
         setBackground(Color.white);
         gr.drawRoundRect(20,20,80,80,0,0);
    gr.drawString("Square", 40, 60);
         Font bigFont = new Font("Helvetica", Font.BOLD, 12);
         gr.setFont(bigFont);
    gr.drawRoundRect(20,20,80,80,0,0);
    gr.setColor(Color.magenta);
    setBackground(Color.white);
         gr.drawString("Rounded", 240, 60);
         gr.drawString("Square", 240,80);
    gr.drawRoundRect(220,20,80,80,40,40);
    gr.setColor(Color.cyan);
    setBackground(Color.white);
         gr.drawString("Circle", 40,220);
    gr.drawRoundRect(20,180,80,80,80,80);
         gr.setColor(Color.green);
         setBackground(Color.white);
         gr.drawOval(190,190,120,60);
         gr.drawString("Oval", 240, 220);
         gr.setColor(Color.red);
         setBackground(Color.white);
         gr.fillRoundRect(20,320,75,75,80,80);
         gr.setColor(Color.white);
         gr.drawString("Filled", 40, 350);
         gr.drawString("Circle", 40, 370);
         gr.setColor(Color.orange);
         setBackground(Color.white);
         gr.fillRect(200,330,120,60);
         gr.setColor(Color.gray);
         gr.drawString("Filled", 240, 350);
         gr.drawString("Rectangle", 240, 370);
         int xPoints[] = {160,150,180,150,158,138,123,126,107,130,160};
         int yPoints[] = {136,160,166,175,198,183,200,173,156,158,136};
         gr.setColor(Color.black);
         gr.drawPolygon(xPoints, yPoints, xPoints.length);
         gr.setColor(Color.orange);
         gr.drawString("Polygon", 130,130);
    }

    //  <applet code="P3ADemo" width="500" height="400"></applet>
    import java.applet.Applet;
    import java.awt.*;
    public class P3ADemo extends Applet
        public void init()
            // We need to set this just once.
            setBackground(Color.white);
        public void paint(Graphics gr)
            gr.setColor(Color.blue);
    //        setBackground(Color.white);
            gr.drawRoundRect(20,20,80,80,0,0);
            gr.drawString("Square", 40, 60);
            Font bigFont = new Font("Helvetica", Font.BOLD, 12);
            gr.setFont(bigFont);
            gr.drawRoundRect(20,20,80,80,0,0);
            gr.setColor(Color.magenta);
    //        setBackground(Color.white);
            gr.drawString("Rounded", 240, 60);
            gr.drawString("Square", 240,80);
            gr.drawRoundRect(220,20,80,80,40,40);
            gr.setColor(Color.cyan);
    //        setBackground(Color.white);
            gr.drawString("Circle", 40,220);
            gr.drawRoundRect(20,180,80,80,80,80);
            gr.setColor(Color.green);
    //        setBackground(Color.white);
            gr.drawOval(190,190,120,60);
            gr.drawString("Oval", 240, 220);
            gr.setColor(Color.red);
    //        setBackground(Color.white);
            gr.fillRoundRect(20,320,75,75,80,80);
            gr.setColor(Color.white);
            gr.drawString("Filled", 40, 350);
            gr.drawString("Circle", 40, 370);
            gr.setColor(Color.orange);
    //        setBackground(Color.white);
            gr.fillRect(200,330,120,60);
            gr.setColor(Color.gray);
            gr.drawString("Filled", 240, 350);
            gr.drawString("Rectangle", 240, 370);
            int xPoints[] = {160,150,180,150,158,138,123,126,107,130,160};
            int yPoints[] = {136,160,166,175,198,183,200,173,156,158,136};
            gr.setColor(Color.black);
            gr.drawPolygon(xPoints, yPoints, xPoints.length);
            gr.setColor(Color.orange);
            gr.drawString("Polygon", 130,130);
            // Use the Polygon class to make a Polygon object.
            Polygon polygon = new Polygon(xPoints, yPoints, xPoints.length);
            // Save the bounds for later use;
            Rectangle r = polygon.getBounds();
            polygon.translate(250, -100);
            gr.setColor(Color.green.darker());
            gr.drawPolygon(polygon);
            polygon.translate(0, 200);
            gr.setColor(Color.pink);
            gr.fillPolygon(polygon);
            gr.setColor(Color.blue);
            gr.drawPolygon(polygon);
            // Another option.
            gr.copyArea(r.x, r.y, r.width, r.height, 250, 0);
    }

  • Problem with threads within applet

    Hello,
    I got an applet, inside this applet I have a singleton, inside this singleton I have a thread.
    this thread is running in endless loop.
    he is doing something and go to sleep on and on.
    the problem is,
    when I refresh my IE6 browser I see more than 1 thread.
    for debug matter, I did the following things:
    inside the thread, sysout every time he goes to sleep.
    sysout in the singleton constructor.
    sysout in the singleton destructor.
    the output goes like this:
    when refresh the page, the singleton constructor loading but not every refresh, sometimes I see the constructor output and sometimes I dont.
    The thread inside the singleton is giving me the same output, sometime I see more than one thread at a time and sometimes I dont.
    The destructor never works (no output there).
    I don't understand what is going on.
    someone can please shed some light?
    thanks.
    btw. I am working with JRE 1.1
    this is very old and big applet and I can't convert it to something new.

    Ooops. sorry!
    I did.
         public void start() {
         public void stop() {
         public void destroy() {
              try {
                   resetAll();
                   Configuration.closeConnection();
                   QuoteItem.closeConnection();
              } finally {
                   try {
                        super.finalize();
                   } catch (Throwable e) {
                        e.printStackTrace();
         }

  • Polygonal lasso tool not working with stylus on Surface Pro 2

    Hi guys,
    I'm new here so please bare with me if I'm posting in the wrong place or don't make immediate sense.
    Hardeware / software used: surface pro 2 and I'm running photoshop CS5.
    Problem is a fairly basic one: I am trying to make basic selections using the polygonal lasso tool (using the stylus that comes with the tablet) but it simply will not work. All I get when I touch the stylus to the screen is the circle that appears then fades. If I attach the keypad and try using that then it works no problem at all.
    Does anyone have any ideas as to whether there is a particular setting that I need to switch on or off e.g. something relating to pressure sensitivity (although not sure why this would affect this particular selection tool)?
    I've searched the web and come up with nothing so far so any help is greatly appreciated!
    Thanks for your time
    Scott

    same problem on surface pro 3 !!!!
    and i think quite significant, for lot of artists using polygonal lasso a lot (including me). How come that the stupid surface cannot work properly

  • Open web pages from an applet

    I'm developing an applet that has to open some web pages.
    The only way I know is to use the method showDocument that
    needs and URL. So, how to pass parameters to the web page?
    I don't want to put them in the URL: everyone can see
    "secret data"!
    Help me, please.

    Take a look at this example that uses a JEditor pane to hold the HTML page.
    //Create a new JEditor Pane
    jep = new JEditorPane( );
    //Ensure the pane is not editable
    jep.setEditable(false);  
    //Use this to get local HTML file
    URL fileURL = this.getClass().getResource("Manual/Manual.htm");
    //add the html page to the JEditorPane
    jep.setPage(fileURL);Ok the core line of code here is this.
    URL fileURL = this.getClass().getResource("Manual/Manual.htm");The standard method for accessing a html file via a URL is thus. As you can see its very similar.
    URL fileURL = new URL("http://www.comp.glam.ac.uk/pages/staff/asscott/progranimate/docs/Manual/Manual.htm");this.getClass().getResourse() will return the file location of the class you are working with.
    By doing the following you can access manual.html in a folder called Manual that sits in the same file location as the class file you are using.
    URL fileURL = this.getClass().getResource("Manual/Manual.htm");I hope this helps.
    Andrew.

Maybe you are looking for