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
I don't explicitly add the mandelbrot to a JPanel , and..
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?
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 japplet 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
Why code an applet? If it is due to spec. by teacher, please refer them to Why CS teachers should stop teaching Java applets.
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.
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.
Related
-- Little note, I attempted to upload an image of my game to illustrate my question, however I do not yet have the required reputation to do so. I apollogise for this.
I would like to create a drop down screen from the top HUD element on my game which the player can type into, effectively becoming a chat window, the actually window is not an issue and I understand that you can disable background and boarder rendering of Java's Swing components so that isn't an issue.
My question is simple, can I take advantage of java's Swing components like JTextField and position them exactly within the bounds of this area, without having to deal with java's layout classes. So this is a summary:
How do I set the final size of the swing components MANUALLY and
How do I set coordinates of the components MANUALLY With out using a layout manager
Yes you can use a null layout on the container and call setBounds(...) on the component to MANUALLY place them. And this is usually a VERY BAD THING to do as it forces you to paint yourself into a layout corner making it very hard to upgrade or enhance your GUI later. It also guarantees that your GUI will look terrible on all platforms and screen resolutions other than one. Many newbies usually go this route initially, and then most leave it eventually after gaining more experience with Swing as they run into its failings, weaknesses and limitations.
For a more complete answer, consider giving more specifics and in image (we can help with this) of your GUI layout requirements.
I am using the NetBeans Designer to create a JFrame. Also it is worth mentioning I am fairly new to Java so I might not understand some things / do things correctly. The frame has about 100 panels, more buttons than I would even think about counting, about ~40 tables, basically most swing components the NetBeans designer provides are being used within the frame. Also for the main frame I am using Null Layout (in order to have a background image inside a JLabel). I know it is not recommended but it doesn't affect the general layout of things as I'm using panels/LayeredPane/TabbedPane for everything, each with it's own design (most of them on Free Design with no Layout specified - that's how I started, didn't know about Layouts and it would take ages now to rearange everything after using Grid Bag Layout for example).
Now getting to my problem, I need to be able to resize the frame and make it resize all components contained. I have to carry a presentation tomorrow of it and I just noticed it doesn't fit on smaller displays (and resizing it doesn't do it properly, it just hides components). I do not care much if it's just an improvisation / not the best approach to the problem as after the 15 min presentation I will probably never open it again.
Thanks.
I would try to go through all the components tree and try to set them smaller font and reduce all their bounds to some static %.
In other words for each component multiply x,y,widht,height to e.g. 0.75 and call setFont() passing derived font of 25% smaller.
I'm using Java Swing and I want to it to set its JFrame size and the size of inner components size based on Screen resolution. I also want it to be compatible on any device like Tablet, laptop. Any easy solution for this?
I also want it to be compatible on any device like Tablet,laptop.Any
easy solution for this.
Put JScrollPane to JFrame, then put JPanel nested all JComponents to JScrollPane, then all JComponents are accesible for non-standard screens e.g net_books, crazy tablets with two or more Native OS (most of screen in Native OS there is scrolled by default)
I'm using Java Swing and I want to it to set its JFrame size and the
size of inner components size based on Screen resolution.
This is a job for LayoutManager
for really better look is not possible without manage with Font size, basically there are a few screen ratios (4:3, 16:9 ...) with a corresponding pixels (screen) resolutions, rest of then to leave for JScrollPane
there is not universal advice, everything depends of your GUI, how JComponents are laid, number of JComponents, its Rectangle on the screen
about the arts, make it look nice and good concept, (then nothing complicated)
Can anyone recommend a decent Swing widget for navigating JScrollPanes? I refer to the type of navigation window you often see in CAD applications that allow you to reposition the viewport by dragging the position of small box (the view port) within a larger box (the underlying component).
To date I've found this, but wondered if there was anything else out there, perhaps as part of a UI library?
Ideally I would also like to render a scaled version of the component in the navigation window.
EDIT
To address some of mKorbel's questions:
The application I'm developing is Swing-based.
The component within the JScrollPane is a JPanel for which I've overridden the paintComponent method in order to draw shapes using Java 2D.
Hence I'm effectively mixing Java 2D and Swing.
Swinglabs incubator has a JXScrollMap, a candidate for future addition. It's in Karl Schaefer's section under scrollmap (he's restructuring, so not entirely sure how stable that latter link is).
I have made a Java game, but it is applet based. I want to convert it into a standalone application. Unfortunately I have 0 knowledge of swing/java applications so I'm not exactly sure where to start and how to get what I want.
My main issue is that the game screen is essentially divided into two parts. The "game screen" and what I refer to as the "dashboard". If you can imagine age of empires, star craft, or any other RTS type of game, that's what the layout is.
So what I want is the screen to have its own graphics panel (all the drawing/animation is done with the graphics and image class) that also has scroll bars in it, that way the size of the game isn't limited to the size of your screen. And I want the dashboard to be it's own separate independent panel, which also uses graphics methods. But in the end, both panels are in the same window.
Is there anyway this is possible?
PS: feel free to request any code or screenshots of the game
Edit: if it is possible, how should I go about doing this?
Simple Swing applications usually based on JFrame class. As i understand, whole game is rendered and not using standard components in UI. Then, roughly, almost no difference between JApplet and JFrame classes. If you used specific JApplet methods for loading resources or something similar, almost all of them would easily replaced with counterparts.
You could create a component class (by extending JComponent) and use it to render main game window. And create another one to render dashboard. Then use BorderLayout as layout manager on JFrame, place main window on center and dashboard on any edge.
There are pretty clear HowTo`s on Oracle site:
How to Make Frames and How to Use Panels.
I think Mersenne's answer covers it pretty well, though I'd consider rendering the game play in a BufferedImage and adding that to (an ImageIcon in) a JLabel in a JScrollPane.