|
|
Welcome to the Java Forums.
You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:
- have access to post topics
- communicate privately with other members (PM)
- not see advertisements between posts
- have the possibility to earn one of our surprises if you are an active member
- access many other special features that will be introduced later.
Registration is fast, simple and absolutely free so please, join our community today!
If you have any problems with the registration process or your account login, please contact us.
|
|

09-07-2008, 03:55 AM
|
|
Member
|
|
Join Date: Sep 2008
Posts: 7
|
|
|
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);
}
}
}
|
|

09-07-2008, 04:50 AM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Posts: 883
|
|
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]
// your code block goes here.
// note the differences between the tag at the top vs the bottom.
[/code]
good luck.
|
|

09-07-2008, 05:02 AM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,223
|
|
|
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?
|
|

09-09-2008, 08:36 PM
|
|
Member
|
|
Join Date: Sep 2008
Posts: 7
|
|
|
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:
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);
}
}
}
|
|

09-09-2008, 08:51 PM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,223
|
|
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.
|
|

09-09-2008, 10:51 PM
|
|
Senior Member
|
|
Join Date: Jul 2007
Posts: 1,222
|
|
// <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);
}
}
}
|
|

09-21-2008, 10:07 PM
|
|
Member
|
|
Join Date: Sep 2008
Posts: 7
|
|
|
Solved
I was able to get this to work, thanks for the suggestions.. 
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|