Results 1 to 8 of 8
  1. #1
    Tucan is offline Member
    Join Date
    Jul 2009
    Location
    Rotterdam
    Posts
    8
    Rep Power
    0

    Unhappy JavaApplet (JEraseImage)

    I am kind of confused about how applet works so, I am appealing for some explinations from any good sumariton with Applets savy. Here is what I have done so far but, I still cant get the faults to stop and get mycodes to work.

    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    import java.util.List.*;
    import javax.swing.*;
    public class JEraseImage extends JApplet implements ActionListener
    {
    ImageIcon image = new ImageIcon("event.gif");
    JButton press = new JButton("Press");
    List<Rectangle> list = new ArrayList<Rectangle>();
    int width = 10;
    int height = 10;
    int startPosX = 10;
    int startPosY = 10;
    int imageWidth, imageHeight;

    public void init ()
    {
    Container con = getContentPane();
    con.setLayout(new BorderLayout() );
    press.addActionListener(this);
    con.add(press, "South");

    imageWidth = Image.getIconWidth();
    imageHeight = Image.getIconHeight();
    }
    list.add Rectangle;
    repaint();


    public void paint(Graphics g)
    {
    super.paint(g);
    g.drawImage(image.getImage(), 10, 10, this);

    for (int count = 0; count <20; ++count)
    {
    int X = (int) (Math.random() * imageWidth) + startPosX;
    int Y = (int) (Math.random() * imageHeight) + startPosY;
    g.fillOvals(X, Y, 10, 10);
    }
    }
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Does your current code compile? It appears as if you have curly brackets misplaced and are trying to call methods outside of a method block. Since this program is so small, you may wish to just start over and code in progressive way where you compile the code after adding each line. This will help prevent errors from accumulating.
    Last edited by Fubarable; 07-05-2009 at 05:32 AM.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    I've been looking at your code a bit more, and have some questions and comments for you.
    1) It looks as if you are borrowing some other code you have found and are trying to add to it. Is this so? If so, where did you find the code?

    2) When calling methods, make sure that you call them on the object (i.e., "image") and not the class (i.e., "Image").'

    3) Again, you have added method calls that are hanging in the class but are not placed in any method or constructor. This simply won't work. Again, if you compile after placing each new line of code, you'll catch this as soon as it has happened and be able to promptly correct it.

    4) You are trying to call methods that don't exist such as g.fillOvals(...) You should look up the Java API for the Graphics class to find the exact names and signatures of the methods that are available. You have to be extremely precise when programming and can't just make up method names and hope that they'll work.

    5) I'm not sure what you're trying to do here:
    Java Code:
    list.add Rectangle;
    as you're not calling the add() method without using parenthesis. What does "Rectangle" mean here?

    Again, good luck.

  4. #4
    Tucan is offline Member
    Join Date
    Jul 2009
    Location
    Rotterdam
    Posts
    8
    Rep Power
    0

    Unhappy JavaApplet(JEraseImage)

    Dear Fuarable,
    Thanks for your advice and recomendations,it's true I amgetting my codes from somewhere and I am trying to put it together to get a working sulation.(I also have a nothen version I was working on but,I got stuck there too.)
    The second attempt is as follows:

    Java Code:
     import javax.swing.*;
       import java.awt.*;
       import java.awt.event.*;
       import java.util.*;
       import java.util.List;
      
        public class JEraseImage4 extends JApplet implements ActionListener
       {    
          ImageIcon image = new ImageIcon("event.gif");
          JButton press = new JButton("press");
          int width, height;
          int imageWidth, imageHeight;                                  
          int startPosX = 10;
          int startPosY = 10;
          Container con = getContentPane();
           
           public void init()
          {        
             con.setLayout(new FlowLayout());
             press.addActionListener(this);
             con.add(press); 
             imageWidth = image.getIconWidth();
             imageHeight = image.getIconHeight();
          }
       
           public void actionPerformed (ActionEvent e)
          {
             int minWidth = width*2;
             int minHeight =height*2;       
             int w = minWidth  + (int)(Math.random() * width);
             int h = minHeight + (int)(Math.random() * height);
             int availWidth  = getContentPane().getWidth();
             int availHeight = getContentPane().getHeight();
             int x = (int) (Math.random() * (availWidth - w));
             int y = (int) (Math.random() * (availHeight - h));
             
             repaint();
          }
       
           public void paint(Graphics g)
          {
             super.paint(g);
             g.drawImage(image.getImage(), 10, 10, width, height, this);
             repaint();
          
          
             for(int count = 0; count < 20; ++count)
             {
                int x = (int) (Math.random() * imageWidth) + startPosX;
                int y = (int) (Math.random() * imageHeight) +startPosY;
                g.fillOval(x, y,10, 10);
             }
          }
       }
    The only piece of code I was given is:
    Java Code:
    for (int count =  0; count < 20;  ++count);   {
    int X = (int)  (Math.random() * imageWidth) + startPosX;
    int Y = (int)  (Math.random() * imageHight)  + startPosY;
    g.fillOval (X, Y, 10, 10);
    }
    I do hope that we can get one to compile and run. Thanks a lot.
    Tucan.
    Last edited by Fubarable; 07-05-2009 at 03:36 PM. Reason: code tags added

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Tucan View Post
    The second attempt is as follows:
    //....
    I do hope that we can get one to compile and run. Thanks a lot.
    Tucan.
    1) please use code tags when posting code so that the code will retain formatting. I've edited your post above to place them in, but please use them yourself in future postings. That way more folks will be willing to read your posted code.
    2) What error messages is the compiler giving you? Those messages will tell you exactly what needs to be fixed.
    3) Also, what is the goal of this program? What are you trying to achieve?

    Again, best of luck.
    Last edited by Fubarable; 07-05-2009 at 03:45 PM.

  6. #6
    Tucan is offline Member
    Join Date
    Jul 2009
    Location
    Rotterdam
    Posts
    8
    Rep Power
    0

    Unhappy JavaApplet(JEraseImage)

    Dear Fubarable,
    (1) The second examlpe compiles without any errors but,when I press the "run applet for current file", the applet viewer appears with the ovals constantly appearing ,even before I press the "Press" to start the ovals appearing.

    The idea is to create a JApplet that paints an ImageIcon the first time its paint() methods is called , and then randomly draws small, filled ovals in the background color over the image each time a JButton is clicked. The resulting effect is that the image seems to be erased by an increasing number of small, overlapping ovals. For example if, you place an ImageIcon at the coordinates contained in the variables named startPosX and startPosY, you can create a series of 10-by-10 filled ovals placed randomly on the ImageIcon's surface using the following for loop:

    for (int count = 0; count < 20; ++count;
    {
    int x = (int) (Math.random) * imageWidth) * startPosX;
    int y = (int) (Math.random) * imageHeight) * startPosY;
    g.fillOval (x, y, 10, 10);
    }

    Save Japplet as JEraseImage.java

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Perhaps you want a LinkedList of Point objects, and with every button press add a new Point(x, y) to the list, then call repaint(). In your paint method (which actually should be a paintComponent method in a JPanel-extended class), you would iterate through the list using each item's x and y value to create your ovals of background color.

  8. #8
    Tucan is offline Member
    Join Date
    Jul 2009
    Location
    Rotterdam
    Posts
    8
    Rep Power
    0

    Unhappy JavaApplet (JEraseImage)

    Dear Fubarable,
    Thank you very mutch for your suggestion of the linkedlist but I am not allowed to use it in this case besauce it is not even mentioned in the studies.
    I am allowed to use the " paint and repaint methods " to produce the filloval.
    Thanks,
    Tucan.

Posting Permissions

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