I have this idea that I want to implement into my project.
I know it is doable, but I do not know where to start.
I have a JPanel,
in there will be a Jbutton , a JLabel, and a ComboBox, and a JtextArea.
Originally, there is only the JtextArea and JButton on my panel.
When I hit the button, the Combobox, and JLabel will slide in or fade in, or drop down, or event rotate in.
So how would I accomplish this, I know I have to use graphic g, repaint(). It is easy to do the Jlabel but what about the combobox.
Should I add the combobox and Jlabel into a separate Jpanel and make that panel animated in. but even that, I do not know how to make this panel in motion.
Please help me on this, and example would be appreciated
Thank you!
To get started, you'll probably need to research these terms:
Slide layout (thanks #Andrew Thompson)
setLocation
TimerTask (I think, or maybe just Timer)
clobbering graphics objects
Once you know about that stuff, you'll be in a position to make the design decisions that you're asking about.
It sounds like you'll have a child panel with a slide layout. Try not to use null layouts when possible.
You'll set its location or style per each tick in a timer task.
If you're setting a style, you'll need to clone your graphics object to avoid "clobbering" it - that is, work on a copy of the graphics object, so the original isn't changed or repainted accidentally while you're working on it.
Related
I am very new to Java Swing programming and I have a small problem.
When I design a user interface using Jframe and JPanels, JPanel does not allow to drag and drop an item in a place I desire (At design time ). Lets say I add a button to JPanel, then the button is stuck in the middle of the panel and I cannot drag it to a place I desire.This does not happen when I add items directly to the JFrame.
I tried using setBounds(int x, int y, int width, int height) method to move the button in JPanel but it did not work.Following image would describe my problem well. I want to drag the button in the JPanel to the left, just like the button added in the JFrame.
Button in JPanel - Top and JFrame - Bottom:
Is there a way to drag and drop items freely in the JPanel as well?
Thanks.
Posting as an answer since I do not have enough reputation to comment. I see you are using eclipse for building your GUI. However, Netbeans IDE allows you to design the interface as you have mentioned.
While creating the GUI manually is a good idea, if you want to speed up the process, you would need to use an IDE.If you are not restricted to Eclipse and want to use an IDE, I would suggest Netbeans.
Go to the properties of the Jframe or JPanel, there is a property as Layout, select the 'Abosolute Layout' from them. Then you can place any AWT Component you desire.
I recently did a program which moves an image along the window and forces it to change its direction when it hits the borders. The animation happened within a JPanel class which was then of course added to a JFrame class which contains the main() function. Now my question is why must it be done in this manner. Can't I simply have just the JFrame and use that both as my window and my animation class which moves the image? Is the JFrame incapable of representing animation on it's own.
Regards.
Omar's answer is correct but I thought I might elaborate just a bit:
Though JFrame's are capable of hosting your animation, it is not a typical use of the JFrame. JFrame's are typically the outermost visual container and host one or more other visual containers (such as JPanel). Though there are exceptions, I would consider the JPanel the most flexible and common way to contain your animation (or other GUI 'controls') to leave you flexibility for adding visual features later.
But as Omar points out, either JFrame or JPanel can be used as they are both considered "containers" in Swing. Tying back to my last comment, using a JPanel is a bit more flexible. For instance, you might later want to incorporate your animation into an Applet/JApplet which you might find confusing and/or difficult if you used a JFrame.
I tried to just comment on Omar's answer but I am a new user and it won't let me add comments yet. Therefore, upvotes are appreciated :)
It's just better to use JPanel as you may want multiple Panels in one frame each with different animations.
Yes, you could do it with JFrame as well.
I'm struggling with two Java GUI things at the moment.
This is the situation: I'm designing a word game using Swing components. I have a main JFrame where everything is placed (my GUI class extends JFrame). There are two things I want to do:
1st: I would like to set an image as the background image of the main frame, it has to be displayed behind all components. i've searched around but haven't found a working solution. I tried making an extended BackGroundPanel class but when I create an instance of BackGroundPanel I have no idea how to make it the background of the frame... I also haven't find a good way to load in an image from an 'images' directory in my src folder...
2nd: when the program starts the user is greeted with an undecorated JDialog, the main frame needs to be disabled, which I figured out, but I would also like to make it a bit darker. I believe it should be possible with the GlassPane, but I have no idea how to set the GlassPane to cover the panel with one color...
Help will be much appreciated, I don't think I have any helpful code to share, but I think the situation explained above gives a general idea? I would just like someone to get me on track with this so I can further work this out! Thanks!
My Main class extends JFrame and it has a BorderLayout.
Add your BorderLayout to a JPanel having, e.g. GridLayout().
This AnimationTest illustrates painting a background image behind components.
This Translucent example illustrates using an AlphaComposite; see also this AlphaTest.
Well for your first question, you can use a label and set the icon of it:
JLabel lblimage = new JLabel("");
lblimage.setIcon(new ImageIcon(Main.class.getResource("/img/background.png")));
lblimage.setBounds(0, 0, 794, 711); //size of frame
contentPane.add(lblimage); //bottom
contentPane.add(component1); //middle low
contentPane.add(component2); //middle top
contentPane.add(component3); //top
as for your second question.. you could possibly do the same thing, just use an image with a solid color and lower the transparency, and place on top of your other components (not sure on this solution though).
I have a problem when I try to resize a JLabel. In my application appears the next strucutre. Understand every list item like something inside the previous list item.
JFrame (Layout null, fixed size, not resizeable, used by different people).
JPanel (Layout null, with a size of all the window, the place were I put my work).
various JPanel with different Layouts (the areas of content inside the main panel, you can think about it like html divs...).
Inside one of this "divs" with Layout null there are extended Classes of JPanel with Flow Layout.
Inside every one of this extedend Classes are labels with preferredSizes.
The thing is that when after create all of this i call theFrame.setVisible(true); and all works propertly.
But in a moment of the flow of my application I have to change the size of one of the labels. Then, I simply call label.setPreferredSize(d) and the change doesn't change. The function works propertly if I call it before set visible the frame, but not after.
I have the feeling that the problem is that I don't use nothing like pack(), validate(), repaint(), etc. Because I don't know what it works. I try call repaint and validate to the jlabel, and repaint the main panel, but doesn't works.
I'm relative new with awt and swing, and this is for homework. Sorry for my bad use of English language, and thank you for your help.
After you change the size of your JLabel, call revalidate(). This will cause the JLabel to be resized without waiting for an event that triggers a re-layout (such as resizing the parent Frame, etc...).
JLabel lbl_test;
lbl_test.setPreferredSize(new Dimension(100, 100) );
lbl_test.revalidate();
One other thing to keep in mind, as I'm not certain which class you're having problems with. Null layouts (absolute positioning) mixed with layout managers are going to cause some strange things to happen.
Been developing a game for a while, and currently re working the GUI, or at least trying to. Had the massive problem of not being able to resize the frame (without issues), as I didn't understand layout managers very well. A few projects later, and time to come back and do some more on the game, and I hit a problem...
The basic layout of the main frame is, mainPane, containing one gameScrollPane and one controlPanel. The scroll pane is a scroll pane, and the control panel a normal panel. The scroll pane contains the main game panel.
As I wanted the scroll pane to take up most of the screen, with the control panel taking up a small lower area, much the same as many Sim like games, so chose the Border layout for the mainPane. I added the scroll pane and set the constraints CENTER and the control panel added and constriants SOUTH. This didn't show the scroll pane, so I played around trying different constraints, and it seems that only when I set the scroll pane constraint to North, does it display at all.
To demonstrate this, I have created a quick video...
http://screenjel.ly/q5RjczwZjH8
As you can see, when I change the value of NORTH to CENTER and re run, it's like its not there!
Bonus points for anyone who can see a clear second problem which I may start another question for after this issue is solved!
I thank you for your time to read this.
Thanks in advance for any ideas or thoughts :)
Rel
If you'd posted some code to start with then you might have gotten a really quick answer. Luckily, you posted a link in the comments to the other response.
The setContentPane() stuff is weird, especially after doing some things to it that will then get wiped out. However, that's not your problem.
The issue is that you are adding levelMaker and personMover right to mainPane without any constraints. These will then be blowing away anything you set for CENTER... in this case the previously set gameScrollPane.
That's why you see it for NORTH and not for CENTER.
I can't get the video to show. It's been buffering for ages.
My guess would be that the scrollpane is in fact filling the center; it's just your game panel that's not being shown.
Your game panel needs to return reasonable values for getPreferredSize().
Update
Another thing you may want to do is have your game panel implement the Scrollable interface. You can then override getScrollableTracksViewportWidth and ...height to return true so your panel will be forced to the scrollpane's dimensions.