Results 1 to 7 of 7
  1. #1
    maggie_2 is offline Member
    Join Date
    Sep 2008
    Posts
    9
    Rep Power
    0

    Default Need with an applet

    Please help,

    I'm trying to create an applet that paints an image icon the first time its paint method is called (which I was able to do), the problem I'm having is trying to get ovals to randomly fill the background and cover the image when the "press" button is clicked. Any help would be greatly appreciated:




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



    public class JEraseImage extends JApplet implements ActionListener
    {

    ImageIcon image = new ImageIcon("event.gif");
    JButton press = new JButton("press");
    int width, height;
    Container con = getContentPane();

    public void init()
    {

    con.setLayout(new FlowLayout());
    press.addActionListener(this);
    con.add(press);
    width = image.getIconWidth();
    height = image.getIconHeight();
    }

    public void actionPerformed(ActionEvent event)
    {

    width = width * 2;
    height = height * 2;

    }

    public void paint(Graphics g)
    {

    g.drawImage(image.getImage(), 150, 100, width, height, this);
    press.repaint();

    for(int count = 0; count < 20; ++count)
    {

    int x = (int) (Math.random() * imageWidth) + startPosX;
    int y = (int) (Math.tandon() * imageHeight) + startPosY;
    g.fillOval(x, y, 150, 100);

    }

    }


    }

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

    Default

    1) Your code won't compile. You'll need to fix that first, then repost.
    2) Please use code tags when posting here. To do this, paste your formatted code here, highligh it, then press the "#" button just above the edit window. It will place the tags above and below your code and make it readable here. Another way is to place the tag &#91;code] at the top of your block of code and the tag &#91;/code] at the bottom, like so:

    Java Code:
    &#91;code]
      // your code block goes here.
      // note the differences between the tag at the top vs the bottom.
    &#91;/code]
    good luck.

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,755
    Rep Power
    25

    Default

    To have paint() called when the button is pressed, you need to call repaint() to tell the system to call the paint method when it gets a chance
    What is the press.repaint() call for?

  4. #4
    maggie_2 is offline Member
    Join Date
    Sep 2008
    Posts
    9
    Rep Power
    0

    Default Please I need help with an applet

    I managed to get the program to compile, but I'm still having trouble getting it to do what I want. Basicaly I need small ovals to appear at random until they cover the entire background this is supposed to happen as I press the "press" buttin. What I get is one large oval that just sits there and the text goes off to the bottom of the screen as I click the "Press" button. If someone could point me in the correct direction as to what I can do to fix this it would be greatly appreciated:

    Java Code:
    
    import java.awt.*;
    import java.awt.event.*;
    import java.applet.*;
    import javax.swing.*;
    
    
    
    public class JEraseImage extends JApplet implements ActionListener
    {
    
       ImageIcon image = new ImageIcon("event.gif");
       JButton press = new JButton("press");
       int width, height;
       int imageWidth, imageHeight;
       int startPosX = 150;
       int startPosY = 100;
       Container con = getContentPane();
    
    public void init()
    {
    
      con.setLayout(new FlowLayout());
      press.addActionListener(this);
      con.add(press);
      width = image.getIconWidth();
      height = image.getIconHeight();
    }
    
    public void actionPerformed(ActionEvent event)
    {
    
      width = width * 2;
      height = height * 2;
      repaint(); 
    
    }
    
    public void paint(Graphics g)
    {
    
      g.drawImage(image.getImage(), 150, 100, width, height, this);
      press.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, 150, 100);
    
    }
    
     }
    
    
    }

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,755
    Rep Power
    25

    Default

    I get is one large oval that just sits there
    What controls where the oval is being drawn? Are the values used correct for your purpose? Add a println() to show the values of x and y used by fillOval. Do they change?
    Perhaps you should add an array of colors and use a different color for each oval you fill.

  6. #6
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    //  <applet code="FillWithOvals" width="400" height="400"></applet>
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    import java.util.List;
    import javax.swing.*;
    
    public class FillWithOvals extends JApplet implements ActionListener
    {
        ImageIcon image = new ImageIcon("event.gif");
                                        //"images/hawk.jpg");
        List<Rectangle> list = new ArrayList<Rectangle>();
        int width = 50;
        int height = 40;
        int startPosX = 150;
        int startPosY = 100;
        int imageWidth, imageHeight;
    
        public void init()
        {
            Container con = getContentPane();
            con.setLayout(new BorderLayout());
            JButton press = new JButton("press");
            press.addActionListener(this);
            con.add(press, "South");
            imageWidth = image.getIconWidth();
            imageHeight = image.getIconHeight();
        }
    
        public void actionPerformed(ActionEvent event)
        {
            int minWidth = 40;
            int minHeight = 30;
            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));
            list.add(new Rectangle(x, y, w, h));
            repaint();
        }
    
        public void paint(Graphics g)
        {
            super.paint(g);
            g.drawImage(image.getImage(), 150, 100, this);
    //        press.repaint();
    
            for(int count = 0; count < list.size(); ++count)
            {
                Rectangle r = list.get(count);
                g.fillOval(r.x, r.y, r.width, r.height);
            }
        }
    }

  7. #7
    maggie_2 is offline Member
    Join Date
    Sep 2008
    Posts
    9
    Rep Power
    0

    Default Solved

    I was able to get this to work, thanks for the suggestions..:D

Similar Threads

  1. Applet in a GUI
    By serfster in forum New To Java
    Replies: 1
    Last Post: 06-13-2008, 12:09 AM
  2. applet
    By amith in forum AWT / Swing
    Replies: 1
    Last Post: 05-16-2008, 04:24 AM
  3. First Applet HELP????
    By nvidia in forum New To Java
    Replies: 0
    Last Post: 08-13-2007, 11:11 PM
  4. Applet
    By kapoorje in forum Java Applets
    Replies: 0
    Last Post: 07-24-2007, 05:06 PM
  5. Replies: 4
    Last Post: 06-08-2007, 07:15 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
  •