1

I have a simple question, I have created an Applet to display some results:

public class Plot2D extends JApplet {

public void paint(Graphics g) {
    super.paint(g);
    Graphics2D g2d = (Graphics2D) g;
    g2d.setColor(Color.red);
    Dimension size = getSize();
    Insets insets = getInsets();
    int w = size.width - insets.left - insets.right;
    int h = size.height - insets.top - insets.bottom;
    Random r = new Random();

    for (int i = 0; i < 1000; i++) {
        int x = Math.abs(r.nextInt()) % w;
        int y = Math.abs(r.nextInt()) % h;
        g2d.drawLine(x, y, x, y);
    }
}

public void main(String[] args) {
    JFrame frame = new JFrame("Points");
    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    // Timer timer = new Timer(2000, new ActionListener() {
    // @Override
    // public void actionPerformed(ActionEvent e) {
    frame.add(new Plot2D());
    frame.setSize(200, 200);
    frame.setResizable(false);
    frame.setVisible(true);
    // }
    // });
    // timer.setRepeats(true);
    // timer.start();

};
}

Which works fine and displays random points. Now If I call that Applet from another class using:

    Plot2D plotting = new Plot2D();
    plotting.main(null);

it displays the same figure but the applet doesn't last on screen. How can I enable that? I've tried the timer which doesn't seem to work. Any thoughts around this?

Thank you.

Edit. As an answer to questions about how is that Applet called, here is the class:

public class PedestrianSpawnerTest {

//  @Before
//  public void initialise(){
//      frame = new JFrame("Points");
//  }



public void test() {

    int numberOfPedestrians = 10;

    PedestrianSpawner pedestrianSpawner = new PedestrianSpawner();

    pedestrianSpawner.SpawnRandomlyStandardPedestrians(numberOfPedestrians);

    List<StandardPedestrian> listOfPedestrians = pedestrianSpawner
            .getListOfPedestrians();

    for (int i = 0; i < listOfPedestrians.size(); i++) {
        System.out.println(listOfPedestrians.get(i).getId());
        System.out.println(listOfPedestrians.get(i).getPosition());
        System.out.println(listOfPedestrians.get(i).getVelocity());
        System.out.println(listOfPedestrians.get(i).getTarget());
    }


    Plot2D plotting = new Plot2D();
    plotting.main(null);
}
}

Edit 2: Ok I've found a sort of hacky trick by adding

try {
        Thread.sleep(2000);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

just after the two lines mentioned above. It seems to work.

Roland
  • 11
  • 4
  • What do you mean with "applet doesn't last on screen"? Does it disappear? – Felix Gerber Dec 04 '15 at 12:14
  • Well it prints on screen but disappears straight away. When I run the Applet itself (without calling it from a class), it prints on screen and stays on screen until I close it. Does it make sense ? – Roland Dec 04 '15 at 13:38
  • Nope, not really... Could you show us your other class, where you calling Plot2D from? I tried your code, calling from another class with only a main method and it just works perfectly... – Felix Gerber Dec 04 '15 at 13:42
  • Ok I've published an edit about it. Thanks for your help. – Roland Dec 04 '15 at 13:59

1 Answers1

0

You should use this instead :

frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

detailed discussion here:

JFrame Exit on close Java

Inside an classical app (main): the application waits JFrame before exiting.

Or you can close everything with System.exit()

with JUNIT:

JUNIT closes all GUI at end of tests (by exit())

For prevent this: see this:

JUnit - stop it from exiting on finish?

For example, if you prevent JUNIT to finish (just append one test with infinite loop), and you launch your tests in a certain order (with @FixMethodOrder(MethodSorters.NAME_ASCENDING) before the class ), it should work.

Community
  • 1
  • 1
  • Hmm no difference indeed. I tried various close operations without real success. But thanks for the post. – Roland Dec 04 '15 at 12:41
  • I dont have the same behaviour. In fact, in all cases, directly, or by another classes, JFrame remains and suspends end of the program. I have to do System.exit(0); Can you describe your environment, how is it called ? – guillaume girod-vitouchkina Dec 04 '15 at 13:23
  • Yes sure, I'm working with Eclipse. The Applet is called in a unit test. I test a couple of classes, which for now return random numbers, and I want to visualise them on screen. After returning the results, I call the Applet via the two lines described above just to check that I'm doing things right. Is that clear ? – Roland Dec 04 '15 at 13:52
  • Ok I'll try that. Let's see. Thank you. – Roland Dec 04 '15 at 14:13
  • Okay, but it doesn't work either. But closer to the answer. – Roland Dec 04 '15 at 14:23
  • last advice in my answer – guillaume girod-vitouchkina Dec 04 '15 at 14:30