I was trying to make a simple GIF capturing app, but I needed to make a hole/transparent space in a JFrame based on a JPanel.
I tried many things, from per-pixel transparency (only works with undecorated windows...), custom shapes (again, undecorated windows only), etc...
This is what I want (edited):
This is what I have:
In .NET Framework, it's as simple as setting the TransparencyKey color of the form, and setting the panel's background color to that color.
I'm wondering if this is possible....
Related
in a game, I would like to display simultaneously two JComponent instances in a JFrame. One would be a background board, the another - a player character.
So, one component (a background) would be behind another (a character). The character would be drawn of several rectangles and thus it will most commonly have some wholly transparent area.
How to do that? I know that normally, when I add two components to a frame (method add(Component)), only the last-added component is visible. This is done by following code:
frame.add(backg); // backg is an instance of a certain class that derives from JComponent
// (...)
frame.add(psc); // psc is an instance of an another class that derives from JComponent
frame.pack();
frame.setVisible(true);
How should I change the code above?
First of all, if you are looking to write a game in Java, try out Slick2D provides numerous tools and far better graphical capabilties (being wrapped around LWJGL which wraps around OpenGL). Secondly, if you do decide to go the Swing route, here's a simple solution:
//Player + background components defined here
playerComp.setOpaque(false);
JLayeredPane layer = new JLayeredPane();
layer.add(backgroundComp,1,0);
layer.add(playerComp,2,0);
I believe that both these solutions were mentioned above in the comments. Setting the player component opaquity to false allows those transparent areas to show components behind the player, or the background image. If you're familiar with a z-index in CSS/HTML, a JLayeredPane basically adds a z-index to Swing by allowing you to set the order in which components are rendered. So, set the player to opaque, and then render it in front of the background component.
I have been using jFrame to build a GUI. I had to insert images in the GUI, for which i inserted a label and put the image as an icon for the label. Now, i have to find out the position of the image in terms of the x and y co-ordinates and i am unable to do that. I have used
setLocaction(x,y);
but it still doesn't seem to work. I even disabled the layout manager by using
setLayout(null);
What is the possible solution for this problem?
Edit
Basically i am creating a Solar system GUI using Swing, so the positions of the planets are to be set by me. I being new to java, there is being some difficulty in implementing the layouts.
This isn't a layout issue at all, but a drawing and possibly an animation issue. If this were my project, I'd
First and foremost, separate out the program logic from its display, a la the MVC or Model-View-Control pattern, or one of its many variants.
Create one JPanel for the GUI's graphics and do all my drawing in this
I would not display my planet images using ImageIcons inside of JLabels.
Rather, I'd create a background image and draw my planet sprites inside of the drawing JPanel's paintComponent method.
I'd create non-GUI Planet classes that a non-GUI model would hold.
In the GUI portion of my program, I would associate a BufferedImage with each Planet, probably using a HashMap<Plant, Image>.
I'd then draw each Planet's associated image in the drawing JPanel's paintComponent(...) method, and place it depending on the Planet's position field values.
If I wanted to animate this, I'd use a Swing Timer to drive my simple animation.
With null layout you should use setSize and setLocation methods on you label to get your image visible correctly inside your frame.
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.
In c# you could change the form shape to be as some picture shape that you draw..
I wonder if there is the same option to do this on jFrame in java? (I'm using netbeans)
and for example this is the picture I want to be used as the jFrame shape
so inside the "phone screen" I want to add some buttons.. is it possible?
See How to Create Translucent and Shaped Windows for details & especially How to Implement a Shaped Window.
Android Look and Feel
Everything on an Android screen is a rectangle. Widgets are rectangles. Launcher icons are 96 x 96 pixel squares. The text under a launcher icon makes them a rectangle.
The screen resolution of a Samsung Galaxy S3 is 1,280 x 720 pixels. The screen resolution of my 22" Samsung monitor is 1,680 X 1,050 pixels. You're going to have to turn the "phone" on its side and use your entire display to get the sharpness of the text on the S3.
This would be a great look and feel for a dashboard application. Your users are probably already accustomed to the smart phone appearance. Obviously, the gestures in your Swing application would have to use a mouse.
GUI Design
First, you create an ordinary JFrame.
Second, you create a drawable JPanel by extending JPanel and overriding the paintComponent method. You would paint the background image on this JPanel, then paint the launcher icons. The launcher icons are BufferedImages created from PNG files, so they can have transparent areas.
The drawable JPanel would listen for mouse clicks. This is so the launcher icons can be moved, and also so the launcher icons can be executed.
The Java application launched would replace the drawable JPanel with it's own JPanel. You would have to follow the Android developer guidelines in developing these JPanels, so your users feel like they're in an Android look and feel. Each application JPanel would have to be 1,280 x 720 pixels.
Your GUI model would hold all of the launcher icons, as well as the positions of these icons for each user. A relational database could hold all of this information so each user would have his or her own display.
Apps / Widgets
I hadn't worked out all the details in my mind, but there would have to be an Apps / Widgets drawable JPanel that shows all of the launcher icons and widgets. The user can drag the launcher icons from the Apps JPanel to the main drawable JPanel.
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.