View Single Post
  #6 (permalink)  
Old 09-09-2008, 10:51 PM
hardwired hardwired is offline
Senior Member
 
Join Date: Jul 2007
Posts: 1,266
hardwired is on a distinguished road
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); } } }
Reply With Quote