Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Draw same image in random position with time delay

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Draw same image in random position with time delay

    Hi, I would like to insert the image bomb.png in a random screen position in each 5 seconds(not delete the previous and insert again after 5 seconds, is to add another one) . Can someane help me?

    import java.awt.Graphics;
    import java.awt.Graphics2D;
     
    import java.awt.Image;
     
    import javax.swing.ImageIcon;
    import javax.swing.JPanel;
     
    public class Board extends JPanel {
     
        Image bomb;
     
        public Board() {
            ImageIcon ii = new ImageIcon(this.getClass().getResource("bomb.png"));
            bomb = ii.getImage();
        }
     
        public void paint(Graphics g) {
     
            Graphics2D g2d = (Graphics2D) g;
            g2d.drawImage(bomb, x, y, null); // x, y are random positions on the screen,
        }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Draw same image in random position with time delay

    A couple of techniques:
    Keep a list of the locations to draw the images, add to the list and use that list in the drawing method.
    Define a BufferedImage, draw the next image on that image and draw that image in the painting method.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. About deleting image on a certain time without the pausing
    By poldz123 in forum Java Theory & Questions
    Replies: 1
    Last Post: February 18th, 2013, 07:26 AM
  2. [SOLVED] Random image
    By JGriff in forum What's Wrong With My Code?
    Replies: 7
    Last Post: December 3rd, 2012, 07:53 AM
  3. How do I draw an image, knowing the co-ordinates and rgb values??
    By byrtis in forum What's Wrong With My Code?
    Replies: 3
    Last Post: June 25th, 2011, 09:15 AM
  4. Draw part of an image
    By DSquire36 in forum AWT / Java Swing
    Replies: 5
    Last Post: June 2nd, 2011, 07:51 PM
  5. Change the random draw line here for a mouseListener draw
    By Panda23 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 10th, 2011, 03:29 AM