Draw an Image on Foreground - java

I try to draw a .PNG Image using graphic2D.drawImage() method.
And I make it right, but I've one JPanel on my Frame, and when I draw my image, it appears in background. I want it to appear on the foreground, and obviously in front of my JPanel.

Use JLayer. (http://docs.oracle.com/javase/tutorial/uiswing/misc/jlayer.html)
Override the paint method on the LayerUI set to the JLayer and draw your .PNG image there. Add your JPanel to the JLayer.

You should add one more JPanel and draw image in its paintComponent method like that.
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (yourImage != null)
g.drawImage(yourImage, 0, 0, this);
}
and than you can hide other JPanel and show this JPanel with setVisible() function.

Related

JSplitPane Background image on right side component

I have a JSplitPane. i want to add background image to the left side of the JSplitPane.
mySplitPane.getLeftComponent
this returs the left side component. and then i want to add an image background to the left side. i think i can use the Paint()
to set background image to the JSplitPane. but how can i set on the only left side component.
I have a JTable on left side of my JSplitPane. i want to have JTable transparent. and then showing the background image.
Right now i have set background to my JTable. i dont want the background to be scrolled with JTable Scroll. Thats why i want to add background image to the Split Pane not the Table.
Could this be what you're looking for?
class ImagePanel extends JPanel {
private Image image;
public ImagePanel(Image image) {
this.image = image;
}
#Override
protected void paintComponent(Graphics g) {
g.drawImage(image, 0, 0, getWidth(), getHeight(), this);
}
}
Usage:
BufferedImage myImage = ImageIO.load(...);
JPanel leftPanel = new ImagePanel(myImage);
//Add panel to splitpanel
JSplitPane mySplitPane= new JSplitPane(JSplitPane.HORIZONTAL_SPLIT)
mySplitPane.setLeftComponent(leftPanel);
Above I have created a subclass of JComponent. Override the
paintComponent(Graphics g) method to paint the image that I want to
display. I then set the content pane of the JPanel and then finally
pass the panel to the left of the split pane
For more on Watermark background, see here for examples and code samples.
Edited

JButton is drawing behind an image

I am making a starting screen, and it's working out pretty fine, got a background image when it starts, now I am trying to draw a JButton on the startmenu, which is a JFrame. But when I run my program, the button appears behind the background picture. If I hover above the area where the button is placed, it's flickering, and when I click it that happens too. Is there any way to draw the Button INFRONT of the background? I made the button as last in the code. My code to draw the background and button:
public void drawStartScreen(){
startScreenOn = true;
Graphics2D b = buffer.createGraphics();
b.setColor( Color.WHITE );
b.fillRect(0, 0, 800, 600);
b.drawImage(start,0,0,null);
setLayout( null );
button = new JButton("Start Game");
button.setBounds(10,10,100,100);
button.setVisible( true );
add(button);
}
It draws the image first, and then the Button, but the button still draws behind the image.
You are mixing painting and adding of components and you definitely shouldn't be doing this. Instead add components when you create the screen or when you first need them but make sure you are only doing this once. Then separate modify the components that need painting changes inside the paintComponent() method.
I recommend you'd use a JLayeredPane (I go for custom painting only as a last resort).
If you're still interested in mixing the 'low-level' painting with 'higher-level' JComponent hierarchy, look at a question about a JFrame that has multiple layers.
Override the paint method on the JFrame:
#Override
public void paint(Graphics g) {
super.paint(g);
Graphics2D b = (Graphics2D)g;
b.setColor( Color.WHITE );
b.fillRect(0, 0, 800, 600);
b.drawImage(start.getImage(),0,0,null);
b.dispose();
}
Notice that this calls paint() on the parent and dispose() on the graphics context when done. I just tried this code and it worked for me.

Background settings for a JDesktopPane

I'm using a self-made DesktopPaneUI for a JDesktopPane, I've written the proper methods for the class, and I'm running into trouble. When I resize the JDesktopPane, the background image doesn't resize with the frame. The image appears to be clipped at the size it was when the window initially opened. I'm giving it an image larger than the window, and I'm still having this problem.
Here's my method call inside the constructor of my desktopUI class.
super();
this.background = javax.imageio.ImageIO.read(new File(fileName));
Is there a way I can change my main class where I set the UI, or the myDesktopPaneUI class such that the background still fills the window when the JDesktopPane changes size?
setUI(new myDesktopPaneUI("media/bg.jpg"));
Override paint() to invoke drawImage() in a way that scales the image to the pane's size:
#Override
public void paint(Graphics g, JComponent c) {
g.drawImage(image, 0, 0, c.getWidth(), c.getHeight(), null);
}
If you are only creating the custom UI to add the background image, the easier approach is to do custom painting of the JDesktopPane so that it works for all LAF's:
JDesktopPane desktop = new JDesktopPane()
{
protected void paintComponent(Graphics g)
{
g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
}
};
Normally you would invoke super.paintComponent(g) first, but since the image will cover the entire background there is no need to do this.
Use a component Listener to know when the windows has been resized and then rescale the image using
image.getScaledInstance(getWidth(), getHeight(), 0);

Drawing graphics in java - NetBeans IDE

I created a new JApplet form in NetBeans:
public class UI extends javax.swing.JApplet {
//generated code...
}
And a JPanel in design mode named panou:
// Variables declaration - do not modify
private javax.swing.JPanel panou;
How do I get to draw a line on panou? I've been searching for this for 5 hours now so a code snippet and where to place it would be great. Using Graphics2D preferably.
Go to design mode
Right Click on the panel "panou"
Click "Costumize code"
In the dialog select in the first combobox "costum creation"
add after = new javax.swing.JPanel() this, so you see this:
 
panou = new javax.swing.JPanel(){
#Override
public void paintComponent(Graphics g)
{
super.paintComponent(g); // Do the original draw
g.drawLine(10, 10, 60, 60); // Write here your coordinates
}
};
Make sure you import java.awt.Graphics.
The line that you will see is always one pixel thick. You can make it more "line" by doing the following:
Create this method:
public static final void setAntiAliasing(Graphics g, boolean yesno)
{
Object obj = yesno ? RenderingHints.VALUE_ANTIALIAS_ON
: RenderingHints.VALUE_ANTIALIAS_OFF;
((Graphics2D) g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, obj);
}
And add after super.paintComponent(g); (in your costum creation) this:
setAntiAlias(g, true);
Edit:
What you are doing wrong is: you paint the line once (by creating the frame).
When you paint the line the frame is also invisible. The first draw is happening when the frame becomes visible. The frame will be Repainted, so everything from the previous paint will disappear.
Always you resize the frame, everything will be repainted. So you have to make sure each time the panel is painted, the line also is painted.
To do custom painting in a JPanel, one would need to make a subclass of a JPanel, and then overload the paintComponent method:
class MyPanel extends JPanel {
public void paintComponent(Graphics g) {
// Perform custom painting here.
}
}
In the example above, the MyPanel class is a subclass of JPanel, which will perform whatever custom painting is written in the paintComponent method.
For more information on how to do custom painting in Swing components, Lesson: Performing Custom Painting from The Java Tutorials have some examples.
If one wants to do painting with Java2D (i.e. using Graphics2D) then one could do some painting on a BufferedImage first, then draw the contents of the BufferedImage onto the JPanel:
class MyPanel extends JPanel {
BufferedImage image;
public MyPanel() {
Graphics2D g = image.createGraphics();
// Do Java2D painting onto the BufferedImage.
}
public void paintComponent(Graphics g) {
// Draw the contents of the BufferedImage onto the panel.
g.drawImage(image, 0, 0, null);
}
}
Further reading:
Painting in AWT and Swing
Trail: 2D Graphics

Images and Panels

I'm having a problem adding a JPanel on top of an Image. This is what I'm trying to do:
Image bgImage = loadImage(filename);
JPanel jp = new JPanel();
jp.setBounds(100,100,100,100);
jp.setOpaque(true);
jp.setBackgroudColor(Color.red);
bgImage.add(jp);
After doing this, I only see the bgImage. I tried everything but I still can't show the panel. Can somebody help me?
You cannot place a component inside an Image. What you want to do is paint the Image onto the background of a swing component (like JPanel). All swing components have a paint() method that calls these three methods (perhaps not quite this order): paintComponent(), paintChildren(), paintBorder(). So, you want to override the paintComponent() method to paint your background image over the panel. When this runs, your custom method will be called, and then the paintChildren() method will be called, which will paint all "child" components over the top of your background image:
class BackgroundImagePanel extends JPanel {
public void setBackgroundImage(Image backgroundImage) {
this.backgroundImage = backgroundImage;
}
#Override
protected void paintComponent(Graphics graphics) {
super.paintComponent(graphics);
graphics.drawImage(backgroundImage, 0, 0, this);
}
private Image backgroundImage;
}
BackgroundImagePanel panel = new BackgroundImagePanel();
panel.setBackgroundImage(image);
panel.add(new JTextField("Enter text here..."));
panel.add(new JButton("Press Me"));
The "BackgroundImagePanel" solution paints the image at its actual size. If this is a requirement, then you can just use a JLabel instead of creating a custom component.
The BackgroundPanel entry shows how you can do this. It also provides a background panel with more custom image painting solutions, that will allow you to scale and tile the image, if this is part of your requirement.

Categories

Resources