JSplitPane Background image on right side component - java

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

Related

Java Swing Button Positioning

The situation here is the following: I have a JFrame, in which I store a list of images. When the right-key is pressed, I display the next image in this fullscreen JFrame. I want a Exit JButton in the upper right corner of the screen, which is the problem. To achieve that, I create a new JPanel everytime the image is changed, remove the old JPanel from the frame, and add the JButton to the new JPanel. The issue is the following: when I start the program, the JButton is in the middle of the JFrame. Once I load the next Image by pressing the right key the JButton is on the right position. However, workarounds like calling the method to display the next Image fail. Even if I set the JButton invisible until the right key was pressed and the second image loaded, it still will be in the center of the screen instead of the upper right. The following code is used:
BufferedImage image = ImageIO.read(images.get(number));
JPanel panel = new JPanel() {
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, 0, 0, null);
}
};
if (currentPanel != null) {
this.remove(currentPanel);
}
panel.setSize(Gallery.this.getSize());
currentPanel = this.add(panel);
jButton1.setBounds(Gallery.this.getWidth() - jButton1.getWidth(), 0, jButton1.getWidth(), jButton1.getHeight());
panel.add(jButton1);
currentPanel.repaint();
This code is executed once at startup. The JButton then is in the middle.
It is executed again when loading the next Image, JButton now is in correct position.
I already tried many things, like adding the JButton to the JFrame instead, setting JPanels layout to null (makes button invisible), repaint, pack, invalidate, nothing I try seems to work. Is anyone able to instruct Swing to place that JButton in the upper right corner of my JFrame? Thank you a lot!
After cleaning up the mess I programmed there, an easy solution proved to work:
I added a custom JPanel to the JFrame at startup and set a fixed width to the JButton. Here the JPanel:
public class ImagePanel extends javax.swing.JPanel {
Image image;
public Image getImage() {
return image;
}
public void setImage(Image image) {
this.image = image;
}
public ImagePanel() {
initComponents();
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, 0, 0, this);
}
}
I set a fixed width to the JButton and added it to that JPanel:
jButton1 = new JButton();
jButton1.setText("Exit");
imagePanel = new ImagePanel();
imagePanel.setSize(Gallery.this.getSize());
add(imagePanel);
imagePanel.add(jButton1);
jButton1.setBounds(Gallery.this.getWidth() - 50, 0, 50, 30);
jButton1.addActionListener((ActionEvent e) -> exit());
displayImage(0);
The displayImage method consists of these lines:
BufferedImage image = ImageIO.read(images.get(number));
imagePanel.setImage(image);
imagePanel.repaint();

Draw an Image on Foreground

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.

How do i have a background image resize in a java gui?

So im making a gui, and i have a background image for it. i dont know how to make it set as the background, so any help with that would be good. an explination would also be good. also, after we get that image as a background, how do we get the image resize to the size of the window. such as
image.setSize(frame.getHeight(), frame.getWidth());
but i dont know if that would work. the image name is ABC0001.jpg and the frame name is frame. thanks!
To get the image to resize, you can either use
public void paintComponent(Graphics g) {
g.drawImage(img, 0, 0, getWidth(), getHeight(), this); // draw the image
}
or you can use a componentlistener, implemented like:
final Image img = ...;
ComponentListener cl = new ComponentAdapter() {
public void componentResized(ComponentEvent ce) {
Component c = ce.getComponent();
img = im1.getScaledInstance(c.getWidth(), c.getHeight(), Image.SCALE_SMOOTH);
}
};
Image quality will degrade over time with the second solution, so it is recommended that you keep the original and the copy separate.
Create a class the extends JPanel. Have that class load the image by overriding paintComponent
class BackgroundPanel extends JPanel
{
Image img;
public BackgroundPanel()
{
// Read the image and place it in the variable img so it can be used in paintComponent
img = Toolkit.getDefaultToolkit().createImage("ABC0001.jpg");
}
public void paintComponent(Graphics g)
{
g.drawImage(img, 0, 0, null); // draw the image
}
}
Now that you have this class, simply add this to your JFrame (or whereever you want the background).
//create refrence if you want to add stuff ontop of the panel
private BackgroundPanel backGroundPanel;
//constructor
add(backGroundPanel, BorderLayout.CENTER);
The size of the background will fill the entire frame so no need to scale it unless you want it smaller

I have extended jEditorPane to contain and display a bufferedimage from memory, but it does not paint properly. What am I doing wrong?

I have extended jEditorPane as shown below (minus the instantiation code). However, when I set the image and call update on the object, it only draws a small portion of the image (equivalent to where one line of text would go). Can somene tell me what I am doing wrong here?
public class JEditorPaneImg extends JEditorPane {
private BufferedImage bi = null;
public JEditorPaneImg() {
initComponents();
}
#Override
public void paint(Graphics g) {
super.paint(g);
if (bi != null) {
Graphics2D g2 = (Graphics2D) g;
g2.drawImage(bi, 0, 0, this);
}
}
public void setImage(BufferedImage image){
bi = image;
}
}
I don't understand what you are attempting to do. It looks like you are trying to paint an image on top fo the the text in the editor pane.
First of all you should never invoke update(). Swing will determine when painting needs to be done.
If you want to paint an image on top of the editor pane, then there is no need to add custom painting to the editor pane. All you do is is create a JLabel and add an ImageIcon to the label. Then add the label to the editor pane. Make sure you use:
label.setSize( label.getPreferredSize() );
and the label will simply be painted as a child component of the editor pane.
If you need more help post your SSCCE showing the problem.

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