I am creating a program in which users choose options (which are buttons) and ultimately the program should display a graphics output based on their options. I have created the program with the buttons (which works fine) but I was having trouble with the graphics. I was wondering if it is possible to have both graphics (ex: a black rectangle) and the buttons on the same frame. Or, do I need to exit the frame with the buttons and create a new frame with the graphics? Thank you for any help.
Related
My desktop Java application has a JMenubar with several JMenuitems, and underneath it is a JPanel which I re-render when an item in the dropdown menu is clicked.
It all works okay, but my JPanel is re-rendering (my overridden paintComponent is being called) when I hover or click on my JMenuitems.
That is a problem, because on the JPanel are programmatically constructed images (randomly seeded), and the construction takes a while, so my program hangs if i hover over the menu too much..
Why is this and how do I fix it?
Edit: Even if I seed the random values and get the same image, the program does too many unecessary calculations and it becomes slow.
… (my overridden paintComponent is being called) when I hover or click on my JMenuitems. That is a problem, … Why is this …
It is expected behavior. The toolkit will repaint a panel whenever it determines it is necessary to do so: E.G.s
A menu appearing or disappearing over it
Another window or dialog dis/appearing over it
The user resizing the window …
… on the JPanel are programmatically constructed images (randomly seeded), and the construction takes a while, …
To avoid having to recreate a complex paint, draw the details to a BufferedImage then either paint the image in the paint method, or (simpler) display it in a label.
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 have just finished a a game applet. Now I want to create a start screen that has a background image, and a start game button. When the user clicks the start button I want to clear the start game screen and I want the game background and all other game component to be painted to the screen. Additionally, I want the start game button to be an image that is clickable. My problem is how do you paint one screen not both in the paint() method.
How would i do this? Thanks in advance.
I see...
A JPanel acting as the primary container for the splash screen
A JLabel to hold the background image
A JButton for the user to click.
I also see a CardLayout to help make it easier to switch between your SplashScreen and your Game screen.
Check out How to use CardLayout for more details.
From your description, it sounds like you've started by overriding paint of the JApplet class. Welcome to the wonderful world of "Why you shouldn't override paint of a top level container"
You need to move all you custom painting and control logic to a different component (something like a JPanel would do) and use it's paintComponent method instead. This way, you can control where the panel goes (could be made into a stand alone application) and provide support for swappable screens/panes
Check out Performing Custom Painting for more details
I am making a game. It consists of a JPanel that contains several other JPanels. 1 bottom panel, with buttons, 1 top panel, with buttons, one panel in between those two panels that is the regular Game screen, and then several SlidingPanes and dynamic JPanels that slide in or pop up from clicking buttons on the top or bottom panel or from clicking somewhere on the game screen.
Right now, I am drawing a truck that follows a path on the game screen. The truck draws fine, but whenever I click a button to open the sliding pane, it gets drawn over by the Game Screen panel, making it flicker as it opens and then not show until I mouse over it. The Game Screens paintComponent method is the cause of this, it is bringing the Game Screen to the front whenever it's repaint method is called, when I want it to stay behind the Sliding Panes or panels that pop up on top of it.
Is there a way to make the game screen stay at the layer it is without paintComponent bringing it to the top?
Please let me know if you have any questions.
Thank you
I want to create a applet which alows the user to select an area on the users screen.
The overlay I want to create should be the same as screenr.com uses it.
http://www.screenr.com/record
// Edit
What I want to setup is a applet which allows me to create screenshots from a area.
At the moment I create a new frame with the applet. This frame is transparent and contains a panel which can be dragged and resized. This panel is also transparent but it has borders.
So more or less everything works right.
Taking screenshots works well, uploading them too.
My problem now is, that the user cant click anything on his screen because my frame overlays the whole screen.