Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 09-07-2008, 02:55 AM
Member
 
Join Date: Sep 2008
Posts: 9
Rep Power: 0
maggie_2 is on a distinguished road
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);

}

}


}
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 09-07-2008, 03:50 AM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 6,481
Rep Power: 8
Fubarable is on a distinguished road
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 [code] at the top of your block of code and the tag [/code] at the bottom, like so:

Code:
[code]
  // your code block goes here.
  // note the differences between the tag at the top vs the bottom.
[/code]
good luck.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 09-07-2008, 04:02 AM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: SouthWest Missouri, USA
Posts: 2,229
Rep Power: 4
Norm is on a distinguished road
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?
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 09-09-2008, 07:36 PM
Member
 
Join Date: Sep 2008
Posts: 9
Rep Power: 0
maggie_2 is on a distinguished road
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:

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);

}

 }


}
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 09-09-2008, 07:51 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: SouthWest Missouri, USA
Posts: 2,229
Rep Power: 4
Norm is on a distinguished road
Default
Quote:
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.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 09-09-2008, 09:51 PM
hardwired's Avatar
Senior Member
 
Join Date: Jul 2007
Posts: 1,577
Rep Power: 4
hardwired is on a distinguished road
Default
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);
        }
    }
}
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 09-21-2008, 09:07 PM
Member
 
Join Date: Sep 2008
Posts: 9
Rep Power: 0
maggie_2 is on a distinguished road
Default Solved
I was able to get this to work, thanks for the suggestions..
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Applet in a GUI serfster New To Java 1 06-13-2008 12:09 AM
applet amith AWT / Swing 1 05-16-2008 04:24 AM
First Applet HELP???? nvidia New To Java 0 08-13-2007 11:11 PM
Applet kapoorje Java Applets 0 07-24-2007 05:06 PM
Applet, To center text and To open I engage in a dialog in an Applet Marcus Java Applets 4 06-08-2007 07:15 AM


All times are GMT +2. The time now is 04:24 AM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org