Java Applet: Random Circles

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 2045 Views - Last Post: 15 February 2017 - 09:13 AM Rate Topic: -----

#1 Huski  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 14-February 17

Java Applet: Random Circles

Posted 14 February 2017 - 05:13 PM

Hello,

I have an assignment requiring me to generate circles at random. I've conducted online research, but I can't find how to implement a random generator into an applet project. I've found code like,

import java.util.Random;
public class RandomTest {;

  public static void main(String[] args) {
    Random rand = new Random(20071969);

    for (int j = 0; j<10; j++)
    {
      int pick = rand.nextInt(10);
      System.out.println(pick);
    }
  }
}


This example is a Java class, can this be used in an Applet? public static void main(String[] args) isn't used for Applets, but is similarly compared to public void init(){ which is used in Applets. Or instead of a random generator, would using loops only, be a solution? Or do I need both? I just need an idea on how to start the project so I don't waste time trying to figure out if I can use either a generator or only loops or even both.

Is This A Good Question/Topic? 0
  • +

Replies To: Java Applet: Random Circles

#2 Huski  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 14-February 17

Re: Java Applet: Random Circles

Posted 14 February 2017 - 05:32 PM

P.S. I won't be able to look and respond until tomorrow morning, where I'll wake up to all the wonderful and intriguing insights from my peers and colleagues that have spent their time examining and analyzing the situation in which they will courageously iron out the problem and for myself to respectfully pay back and discuss with, and learn to achieve new knowledge that has yet to be gained. :)
Was This Post Helpful? 0
  • +
  • -

#3 NormR  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 635
  • View blog
  • Posts: 4,827
  • Joined: 25-December 13

Re: Java Applet: Random Circles

Posted 14 February 2017 - 05:35 PM

Are you sure you are supposed to be writing an Applet? Applets are out.
Was This Post Helpful? 2
  • +
  • -

#4 Huski  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 14-February 17

Re: Java Applet: Random Circles

Posted 14 February 2017 - 05:35 PM

View PostNormR, on 14 February 2017 - 05:35 PM, said:

Are you sure you are supposed to be writing an Applet? Applets are out.


At the moment I'm still here. What do you mean by "out"?
Was This Post Helpful? 0
  • +
  • -

#5 NormR  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 635
  • View blog
  • Posts: 4,827
  • Joined: 25-December 13

Re: Java Applet: Random Circles

Posted 14 February 2017 - 06:16 PM

Applets are not supported by most browsers and soon will not be supported by Oracle.
Most java programmers would not recommend learning how to write Applets.
Was This Post Helpful? 2
  • +
  • -

#6 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3417
  • View blog
  • Posts: 15,410
  • Joined: 20-September 08

Re: Java Applet: Random Circles

Posted 15 February 2017 - 06:01 AM

That's right. Having said that, an applet is actually a glorified panel. Look at overriding paintComponent in a JPanel subclass and you can place that in various contexts, including that of applets.
Was This Post Helpful? 1
  • +
  • -

#7 Huski  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 14-February 17

Re: Java Applet: Random Circles

Posted 15 February 2017 - 06:05 AM

Aw :(, I wasted money learning how to program applets. Our professor said using firefox to run applets since that's still supported for now. So does that mean most people's knowledge of programming in Java Applets are fading away and I can no longer receive help?
Was This Post Helpful? 0
  • +
  • -

#8 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3417
  • View blog
  • Posts: 15,410
  • Joined: 20-September 08

Re: Java Applet: Random Circles

Posted 15 February 2017 - 06:30 AM

No it doesn't mean that. I've already helped you ;)
Was This Post Helpful? 1
  • +
  • -

#9 Huski  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 14-February 17

Re: Java Applet: Random Circles

Posted 15 February 2017 - 06:33 AM

View Postg00se, on 15 February 2017 - 06:30 AM, said:

No it doesn't mean that. I've already helped you ;)/>


Oh sorry good sir. So what you are telling me is that I put code similar to above in the public void paint(Graphics g) {?
Was This Post Helpful? 0
  • +
  • -

#10 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3417
  • View blog
  • Posts: 15,410
  • Joined: 20-September 08

Re: Java Applet: Random Circles

Posted 15 February 2017 - 06:48 AM

No, that's not what i stated. You need to read up on custom painting in Java Swing
Was This Post Helpful? 1
  • +
  • -

#11 NormR  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 635
  • View blog
  • Posts: 4,827
  • Joined: 25-December 13

Re: Java Applet: Random Circles

Posted 15 February 2017 - 06:55 AM

Here's a link to the tutorial: http://docs.oracle.c...ting/index.html
Was This Post Helpful? 1
  • +
  • -

#12 Huski  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 14-February 17

Re: Java Applet: Random Circles

Posted 15 February 2017 - 08:13 AM

I've looked at the paintCompenent, I understand how to draw shapes, but I can't figure out how to get them to spawn randomly.

import java.awt.*;
import java.applet.*;

public class MilkyWay extends Applet {

    public void paint(Graphics g) {
        int y = 20;

        for (int floor = 0; floor < 4; floor++)
        {
            int x = 0;
            for (int MilkyWay = 1; MilkyWay <=3; MilkyWay++)
            {
                g.fillOval(x, y, 40, 50);
                x = x+50;
            }//end of inner loop
            y = y+80;
        }//end of outer loop
    }
}



What component would be helpful so when I compile, circles will appear randomly? I'm still searching though.
Was This Post Helpful? 0
  • +
  • -

#13 xclite  Icon User is offline

  • I wrote you an code
  • member icon


Reputation: 1230
  • View blog
  • Posts: 4,016
  • Joined: 12-May 09

Re: Java Applet: Random Circles

Posted 15 February 2017 - 08:15 AM

Also if I had a line to your professor I would strongly advise them to modernize their course material. You can learn some interesting things that are transferable, but there's no reason to be learning a technology that is dying and NOT coming back when they are alternatives in both HTML5 and Swing/JavaFx.
Was This Post Helpful? 1
  • +
  • -

#14 Huski  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 14-February 17

Re: Java Applet: Random Circles

Posted 15 February 2017 - 08:19 AM

View Postxclite, on 15 February 2017 - 08:15 AM, said:

Also if I had a line to your professor I would strongly advise them to modernize their course material. You can learn some interesting things that are transferable, but there's no reason to be learning a technology that is dying and NOT coming back when they are alternatives in both HTML5 and Swing/JavaFx.


The way the course is set up, the book I use isn't very informative and most of the projects I have to research to implement these little details. It's a sad world.
Was This Post Helpful? 0
  • +
  • -

#15 Thomas1965  Icon User is offline

  • D.I.C Head

Reputation: 52
  • View blog
  • Posts: 206
  • Joined: 09-September 16

Re: Java Applet: Random Circles

Posted 15 February 2017 - 08:22 AM

To get random numbers use java.util.Random class
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2