0

I am currently working on an assignment and need to create basic controls (buttons) for the Mandelbrot set that will operate as a JApplet. The graphics is initialised in the init() method and a method which draws the Mandelbrot is called in start(). The problem is, I have searched high and low and cannot figure out how to add a GUI to my applet because

  1. I don't explicitly add the mandelbrot to a JPanel , and..
  2. I have no room left it seems to add a GUI because the Mandelbrot takes up the entire JFrame.

I had one idea which was to set the size of the JFrame, set the size of the Mandelbrot graphic to only be say 4/5 of the whole frame, and add buttons to the remaining portion?

Does that sound like a good solution?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433

1 Answers1

1

Does that sound like a good solution?

No. The panel in which the Mandelbrot is drawn should return a sensible preferred size. Add the rendering panel and the buttons to a layout or groups of layouts using layout padding, borders and button margins for white space.

But I am a little confused by the reference to tag yet the body of the question mentions both JApplet and (twice) JFrame.

  • An applet has to make do with whatever size it is provided by the HTML (or JS, in the case of the deployment toolkit script) that launches it.
  • A frame can call pack() on a properly laid out UI and expect to be the smallest size needed to display the components.

General tips

  1. Why code an applet? If it is due to spec. by teacher, please refer them to Why CS teachers should stop teaching Java applets.
  2. For deploying Java desktop apps., the best option is usually to install the app. using Java Web Start. JWS allows the user to launch a JFrame from a link in a web page.
  3. Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead use layout managers, or combinations of them1, along with layout padding & borders for white space2.


Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Thanks for your reply, you say "the panel in which the mandelbrot is drawn" I dont know how to add it to a panel, its drawn for me without me specifying a panel. Thats why I cannot split up the space as shown in your two pics. – user3574558 May 01 '14 at 07:33
  • *"its drawn for me without me specifying a panel"* Do you have ***any*** idea what you are doing, or are you attempting 'coding by magic' (which doesn't work BTW)? – Andrew Thompson May 01 '14 at 07:35
  • What I mean is, when you code an application you create frames, panels explicitly , with an applet , a frame is made behind the scenes and the graphic gets placed in it. I do not know how to use layout managers with an applet . Could somebody please give me a template or rough idea of how to have say two panels, one for the graphic, one for buttons. Thanks alot . – user3574558 May 01 '14 at 09:29
  • *"I do not know how to use layout managers with an applet"* Do you know how to use layout managers with a panel? If so, add a panel to the applet in the `init()` method and take it from there (add other things to the main panel). – Andrew Thompson May 01 '14 at 09:32
  • I can use layout managers and panels, and adding components with a panel , the trouble i have is that graphics / image isnt a component so I cant add it like i have been doing with other projects. – user3574558 May 01 '14 at 09:35
  • I havent been programming for that long and so i'm open to any criticisms about my lack of knowledge. Would you like me to post my code ? – user3574558 May 01 '14 at 09:45
  • For more help, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete and Verifiable Example). – Andrew Thompson May 01 '14 at 10:21