How to place images on top of each other in java - java

I have a background image of a road, which I have displayed on a JFrame using an ImageIcon. I want to put a car on top of that background image at a certain (x,y) location.
I tried using another ImageIcon and the background does not appear when I run the program , however the car does.
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Gui extends JFrame {
private ImageIcon northcar = new ImageIcon("src/north.gif");
private ImageIcon usIcon = new ImageIcon("src/trafficLight.jpg");
public Gui() {
add(new JLabel(usIcon)); // background image does not appear after the i added the northcar label
add(new JLabel(northcar)); // this picture can be seen
}
public static void main(String[] args) {
Gui frame = new Gui();
frame.setTitle("TestImageIcon");
frame.setLocationRelativeTo(null); // Center the frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(0, 0, 650, 650);
frame.setVisible(true);
}
}
I heard about using a canvas but have no clue. Any ideas ?
Many Thanks

How about using LayeredPane, this might help.

add(new JLabel(usIcon));
add(new JLabel(northcar));
Change the order to:
add(new JLabel(northcar));
add(new JLabel(usIcon));
Read up on Z-Order. Simply the last component added gets painted first.

Related

How to create background of JFrame transparent in java but keeping buttons visible?

I am making my JFrame transparent by:
myFrame.setUndecorated(true);
myFrame.setBackground(new Color(1.0f,1.0f,1.0f,0.0f));
It makes JFrame transparent but also lost window strip and close button.
To make window appear visible I used
AWTUtilities.setOpacity(this, 0.5f);
but I found that this method is only available for java 6 and packaged in AWT package which is restricted in java 7. and the method is also changed in java 7 which is now
Window.setOpacity(float opacity);
but it does not working, Can anyone tell me how to make transparent window and buttons visible along with transparent frame.
This will make the frame look a bit different, but I think this is what you want:
import java.awt.Color;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class TransparentExample extends JFrame {
public TransparentExample() {
super("TransparentExample");
JPanel panel = new JPanel();
panel.add(new JButton("Button"));
setContentPane(panel);
setBackground(new Color(0, 0, 0, 0));
setSize(500, 500);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
JFrame.setDefaultLookAndFeelDecorated(true);
TransparentExample frame = new TransparentExample();
frame.setVisible(true);
}
}
If it works it should look like this:

My JFrame window won't appear

My JFrame window won't appear, though, by the tutorial I've been watching, I've been doing everything spot on, yet nothing happens. It doesn't even give me an error, which makes it so much worse. This is the code:
import javax.swing.*;
import java.awt.*;
public class Window {
public class Window {
public void newWindow() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
JLabel label = new JLabel("I am a star! A beautiful shining star!", SwingConstants.CENTER);
frame.getContentPane().add(label);
frame.setVisible(true);
}
}
(This is a second class, the main one properly links to this one)
JFrame uses a layout manager named BorderLayout by default. To see components added to the frame, you should refer to its javadocs. However, the easiest choice here is to use FlowLayout. You also should use JFrame's pack() method, which, according to Oracle:
Causes this Window to be sized to fit the preferred size and layouts of its subcomponents. The resulting width and height of the window are automatically enlarged if either of dimensions is less than the minimum size as specified by the previous call to the setMinimumSize method.
Thus, this should work for you:
import javax.swing.*;
import java.awt.*;
public class Window {
public void newWindow() {
JFrame frame = new JFrame();
frame.getContentPane().setLayout(new FlowLayout()); // specify the layout manager
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
JLabel label = new JLabel("I am a star! A beautiful shining star!", SwingConstants.CENTER);
frame.getContentPane().add(label);
frame.pack(); // handles sizing of the window
frame.setVisible(true);
}
}
Notice that I removed the duplicate public class Window declaration, which might have been the reason you didn't see a frame at all. If it's still not working for you, I think you aren't calling the newWindow() method. If you want the window to show by simply calling new Window();, then you should change public void newWindow() to public Window().
import javax.swing.*;
import java.awt.*;
public class Window {
public void newWindow() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel label = new JLabel("I am a star! A beautiful shining star!", SwingConstants.CENTER);
frame.getContentPane().add(label);
frame.setVisible(true);
frame.pack();
}
}
From what I see, your not setting the size of the JFrame. Try,
frame.setSize(500, 500);

Putting everything into a JFrame

I want to know how to put put console output into a JFrame. For example, putting this output into a JFrame:
import static java.lang.System.out;
public class frame{
public static void main(String [] args){
out.println("hello");
}
}
How is it possible?
You need to set up the JFrame first.
JFrame frame = new JFrame("title");
Then, set the properties of the JFrame:
frame.setSize(1280,720); //Sets the program's size
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Tells the program to exit on close
frame.setResizable(true); //Tells the program if resizing is enabled
Then, create a panel to store the components:
JPanel p = new JPanel();
After that, you must add the panel to the JFrame like so:
frame.add(p);
Then, with that done, you can use the components supplied in the swing framework, and add them to the panel. A reference for these components can be found here: http://docs.oracle.com/javase/tutorial/uiswing/components/componentlist.html.
To create a component, use the following code:
JLabel label = new JLabel();
Then, use it's build in functions to change it:
label.setText("new text");
Then, once again, to add a component to a panel, use the panel's add() method:
panel.add(label);
Those are just the basics of making a GUI with java. A full tutorial can be viewed here:
http://docs.oracle.com/javase/tutorial/uiswing/
Good Luck!
I can help you with this, but let me please fix some syntax errors you have. When you put the import, an import can't be static (that I know of) and when you want to print out something using "System.out.print" or "System.out.println" you MUST include the "System" part of the line. If you want to add text to a a JFrame use the JLabel to import both just do this bit of code:
import javax.swing.*;
That should import all of your swing elements such as JLabel and JFrame and JPanel, and try this code it will make a window that will have a button and a label. The button doesn't do anything in this code:
import javax.swing.*;
public class main{
public static void main(String[] args)
{
/*
* Creates the frame, makes it visible, and makes
* appear in the center of the screen. While also making it have a close operation
*/
JFrame frame = new JFrame("Button");
frame.setVisible(true);
frame.setSize(300, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
//Creates the panel, and adds it to the frame
JPanel panel = new JPanel();
frame.add(panel);
//Creates the label and adds it to the panel, also sets the text
JLabel label = new JLabel();
label.setText("Welcome" + "\n" + "\n");
panel.add(label);
//Creates the button and adds it to the panel
JButton button1 = new JButton("Button 1");
panel.add(button1);
}
}
1.If you want to use JFrame you have to extend your class to a subclass of JFrame:
public class frame extends JFrame {}
2.a)If you want to put Text in your Frame use JLabel and add it to your frame:
JLabel hello = new JLabel("Hello");
add(hello);
2.b)If you want a console output just call System.out.println() in the constructor
Here is a small example class:
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Frame extends JFrame {
public static void main(String args[]) {
new Frame();
}
Frame() {
System.out.println("Hello");
JLabel hello = new JLabel("Hello");
add(hello);
this.setSize(100, 100);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
}
have a look to the oracle lessons... or any java book!
If that is the case, I don't want to get into GUI just yet. (Learning from a book) Can I convert my current project to a jar file and have it automatically open a command prompt window upon double click?

Image/text not showing in JFrame

I want to create a Window with an image and a text so far i've got this:
public void ShowPng1() {
ImageIcon theImage = new ImageIcon("Icon_Entry_21.gif");
panel.setSize(270, 270);
JLabel label = new JLabel("Hello, World!");
JLabel imageLabel = new JLabel(theImage);
imageLabel.setOpaque(true);
panel.add(imageLabel);
panel.add(label);
panel.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel.setVisible(true);
}
My panel:
private JFrame panel = new JFrame();
For some reason it won't load nor image nor text, it just pops up as a white window. What can be the problem? I've also tried changing the format to .png, didn't work.
UPDATE
import java.awt.BorderLayout;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Img {
private JFrame panel = new JFrame();
public Img(){
ShowPng1();
}
public void ShowPng1() {
ImageIcon theImage = new ImageIcon("Icon_Entry_21.gif");
panel.setSize(300, 300);
panel.setResizable(false);
JLabel label = new JLabel("Hello, World!");
JLabel imageLabel = new JLabel(theImage);
imageLabel.setOpaque(true);
panel.add(imageLabel);
panel.add(label, BorderLayout.PAGE_END);
panel.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel.setVisible(true);
}
public static void main(String[] args) {
new Img();
}
}
I've managed to get this working, which is ridiculous because I can't figure out how to make it work with my program. Reimeus gave me an idea on creating this script separately, the fix and that worked. I will have to look through my entire program to see if I'm missing anything. Creating it in a separate class should work as well.
it just pops up as a white window
Sounds like you're blocking the EDT on startup. You may need to use one of Swing's concurrency mechanisms to solve it. Post a Minimal, Complete, Tested and Readable example so we can determine this for sure.
In the meantime...
You're displacing the component containing the theImage component in the BorderLayout.CENTER location
panel.add(label);
You could organize your labels so that they can appear simultaneously (placing the components at 2 different BorderLayout locations will do)
panel.add(imageLabel);
panel.add(label, BorderLayout.PAGE_END);
You should make a JPanel and add it to the frame, and then add the labels to the panel
Something like
private JPanel panel = new JPanel;
and then add it to the frame in your method calling
frame.add(panel);

JScrollPane scrollbars not scrollable

I have a class that draw some very simple graphics like lines, circles and rectangles. The lines are dynamically expandable and sometimes when they expand beyond the resolution, it is impossible to see without a scroll bar. Therefore, I've added JScrollPane to my JFrame but unfortunately, the scroll bar is not scrollable despite calling the Layout Manager already.
Here's what I have:
- A class that draws components (lines, rectangles, circles)
- A class that sets up the JFrame/JScrollPane
Here's an excerpt code of my GUI class:
JFrame frame = new JFrame("GUIFrame");
frame.setLayout(new BorderLayout()); // Layout already set
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
DrawComponent comp = new DrawComponent(); // Reference to class that draw components
JScrollPane sp = new JScrollPane(comp, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
sp.setPreferredSize(new Dimension(1000, 1000));
frame.add(sp, BorderLayout.CENTER);
frame.setSize(500,500);
frame.setVisible(true);
With the code above, I've got Java to show me a JFrame with scrollpane containing my jcomponents. I have set the scrollbars to always appear as shown above but they are not scrollable, gray-ed out.
As suggested by Andrew, I took sometime to create a SSCCE to reflect what I'm trying to do:
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Line2D;
import java.util.Random;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
public class DrawTest {
public static void main(String[] args){
JFrame frame = new JFrame("SSCCE");
frame.setLayout(new BorderLayout());
frame.setSize(1000, 1000);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
DrawComp d = new DrawComp();
JScrollPane sp = new JScrollPane(d, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
frame.add(sp);
frame.setVisible(true);
}
}
class DrawComp extends JComponent {
public void paintComponent(Graphics g){
Graphics2D g2 = (Graphics2D)g;
Random ran = new Random();
int ranNum = ran.nextInt(10);
System.out.println(ranNum);
double length = 100 * ranNum;
g2.draw(new Line2D.Double(10, 10, length, length));
}
}
The code above draws a diagonal line based on a random input. What I intend to do is that when the line gets so long that it goes out of the frame size, I hope that I'll be able to scroll and have a view of the full line. Again I have added the line component to JScrollPane but it's not scroll-able.
My apologies. You are using JScrollPane the right way. I ran your code and I think I got the reason why JScrollPane is not working. Picture this, when your set the jframe's background to all red (by paiting a red dot everywhere) should the jscrollpane be scrollable? No, because painting the background color is in the background. The actual VIEW isnt changing and it is not bigger then the display size so the scrollpane does not see the point of scrolling. Your paint component method is doing something similar. It is just drawing something in the background. The actual VIEW didnt change so scrollpane wont work.
public class DrawTest {
public static void main(String[] args){
JFrame frame = new JFrame("SSCCE");
frame.setLayout(new BorderLayout());
frame.setSize(500, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final DrawComp d = new DrawComp();
final JScrollBar hbar,vbar;
hbar = new JScrollBar(JScrollBar.HORIZONTAL, 0, 1, 0, 500);
vbar = new JScrollBar(JScrollBar.VERTICAL, 0, 1, 0, 500);
frame.setLayout(null);
frame.add(d);
frame.add(hbar);
frame.add(vbar);
d.setBounds(0, 0, 300, 300);
vbar.setBounds(460, 0, 20, 480);
frame.setVisible(true);
vbar.addAdjustmentListener(new AdjustmentListener()
{
public void adjustmentValueChanged(AdjustmentEvent e)
{
d.setLocation(d.getX(), -vbar.getValue());
}
});
}
}
Here is the code for sliding a component vertically. I made some changes to your existing code. The DrawComp is still the same
I have found a makeshift solution to my problem. Given how simple the drawing is, I have decided to use the end point of the line to override preferredSize.
Pertaining to the SSCCE I posted above, I added a setPreferredSize(new Dimension(length, length) in paintComponent() so that the preferredSize will always be the end point of the line when it is called. This makes sure that I have got the whole area of the painting covered and yet be able to scroll, should there be a need to.

Categories

Resources