How to put or draw stable image background in java - java

I'm having problems with putting a stable image background.
I successfully create/draw an image background but when I consecutively run it many times, the image is not showing. The background image seems to be not stable. When I drag the frame to a side of my laptop screen, the image is being erased. How do I create/draw a stable background image that doesn't flicker or erased when dragged?
This code below is what I used for my background image:
public void paint( Graphics g ) {
super.paint(g);
g.drawImage(img, -30, 0, null); //draw image to background
}

Use the JLabel.setIcon(Icon icon) method.
The benefit of this is you don't need to overwrite any method to put a background.
Indeed you can also use JLabel in alternative to JPanel, if you want a container that has an image background. Below can explain this trick in code:
public class JPanelWithBackground extends JLabel {
public JPanelWithBackground() {
add(new JButton("I can attached to JLabel? Isn't cool? "));
setBackgroundImage("path_to_image.png");
}
public void setBackgroundImage(String imagePath) {
setIcon(new ImageIcon(imagePath));
}
}

Related

Drawing text over a bufferedimage on a panel

I am writing a program that consists of three different panels.
This is part of my bottom panel. I display an image using BufferedImage, and I would like to create some text that will appear around 350px into the image, and 15px down. I cannot manage to get this to work and overlay over the bufferedImage.
My current code is as follows (For the bottom panel):
public class BtmPanel extends JPanel {
BtmPanel(){
try {
JLabel imgLabel = new JLabel();
final BufferedImage img = ImageIO.read(new File("image.png"));
ImageIcon icon = new ImageIcon(img);
imgLabel.setIcon(icon);
this.add(imgLabel);
}
catch(IOException ie){
System.out.println("image does not exist");
}
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawString("Y: " + MiddlePanel.y ,350,15);
}
}
There are a couple of ways you might achieve this, depending on your needs and desires.
For example, rather than using a JLabel to display the image, you could paint it directly yourself, for example...
public class BtmPanel extends JPanel {
private BufferedImage image;
BtmPanel(){
try {
image = ImageIO.read(new File("image.png"));
}
catch(IOException ie){
System.out.println("image does not exist");
}
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (image != null) {
g.drawImage(0, 0, image, this);
}
g.drawString("Y: " + MiddlePanel.y ,350,15);
}
}
The reason for doing it this way is you don't (really) control the placement of the image within a JLabel (lots of things can affect it's positioning).
Another solution would be to draw the text directly onto the BufferedImage itself. Which you would use would depend on how dynamic the text might be and a bunch of other factors
A way to do this is doing the exact inverse of your example, overriding the JPanel's paint method painting a BufferedImage (creating a background with your image), and then you can normally add the JLabel with text on your JPanel extension.
Please note that if JPanel is resized and you don't want to have a bad quality background, you'll have to manually create a resized version to adapt the container, or, at least, resize your buffered image with a little logic to keep Width/Height ratio... not that easy!
For the basic version you can try something like the following (taken from a working example) paintComponent overridden function:
#Override
protected void paintComponent(Graphics g) {
Image background = backgroundImage.getScaledInstance(this.getWidth(), this.getHeight(), Image.SCALE_DEFAULT);
super.paintComponent(g);
if (backgroundImage != null) {
g.drawImage(background, 0, 0, null);
}
}
Hope this helps ;)

How to start a .gif file from the first frame in Java Swing?

I'm using two .gif files and switching them in a frame.
private Image currentGIF;
if(x == 0) gif = "image1.gif"
if(x == 1) gif = "image2.gif"
ImageIcon reference = new ImageIcon(gif);
currentGIF = reference.getImage();
--
public void paint(Graphics g)
{
Graphics2D graphic = (Graphics2D) g;
graphic.drawImage(gif, 0, 0, this);
g.dispose();
}
--
#Override public void actionPerformed(ActionEvent arg0)
{
repaint();
}
And this is my problem:
image1.gif starts at the first frame.
image2.gif starts at the first frame.
image1.gif starts at a random frame.
It seems the .gif keeps running and when I show image1 again it doesn't start at the first frame.
Issues:
Never override a JFrame's paint method, but instead do your drawing within a JPanel's paintComponent method.
Never dispose of a Graphics object given to your painting method (paint or paintComponent) by the JVM as this breaks the painting chain.
Don't change the state of your GUI (for example swap images) within a painting method. Instead do it within a listener of some type that reacts to the event that you want to use to make the image change -- such as a button's ActionListener if it's to change on button press, or a Swing Timer's ActionListener if the image is to change on timer change.
If this is not an animated GIF, strongly consider making the image into an ImageIcon and simply swapping the icon of a JLabel using setIcon(...) again with the listener that you desire to use.

Background image for a jPanel not working

I am new to making GUIs so I decided to try the the windows builder for eclipse, and while great I do have some doubts. I have been searching but I cannot seen to find a good way to add a background image to my "menu". For example I tried this:
public Menu() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(50, 50, 300, 250); //Dimensiones
contentPane = new JPanel() { //Imagen de Fondo
public void paintComponent(Graphics g) {
Image img = Toolkit.getDefaultToolkit().getImage(
Menu.class.getResource("/imgs/rotom.jpg"));
g.drawImage(img, 0, 0, this.getWidth(), this.getHeight(), this);
}
};
And adding the following classes:
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
But to no avail the window remains with its dull grey color, so far my code is just the standard one WindowsBuilder cooks for you plus 4 buttons but I doubt they're of importance here. Shouldn't the code I added override the paintComponent() method of the jPanel and draw the image in it?
The class for the menu is in a package within my project and the image is within a imgs package is within the same project as well.
Thanks a lot in advance.
A simple method, if you're not interested in resizing the background image or applying any effects is to use a JLabel...
BufferedImage bg = ImageIO.read(Menu.class.getResource("/imgs/rotom.jpg"));
JLabel label = new JLabel(new ImageIcon(bg));
setContentPane(label);
setLayout(...);
There are limitations to this approach (beyond scaling), in that the preferred size of the label will always be that of the image and never take into account it's content. This is both good and bad.
The other approach, which you seem to be using, is to use a specialised component
public class BackgroundPane extends JPanel {
private BufferedImage img;
public BackgroundPane(BufferedImage img) {
this.img = img;
}
#Override
public Dimension getPreferredSize() {
return img == null ? super.getPreferredSize() : new Dimension(img.getWidth(), img.getHeight());
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(img, 0, 0, this);
}
}
You should avoid trying to perform any task in the paintComponent method which may take time to complete as paintComponent may be called often and usually in quick succession....
Getting the image to scale when the component is resized is an entire question into of it self, for some ideas, you could take a look at...
Java: maintaining aspect ratio of JPanel background image
Java: JPanel background not scaling
Quality of Image after resize very low -- Java
Reading/Loading images
Oh, and, you should avoid extending directly from top level containers, like JFrame, they reduce the reusability for your components and lock you into a single container

Overlay images Java

I'm trying to overlay a background picture with a .png sample, and I don't know how it works.
I tried to create 2 JPanels, but didn't find out how to overlay them, then I tried to create 2 JLabels and once again, they just stay separated.
Have you any idea ?
Thanks !
You can use one jPanel.Override paint or paintComponent method and draw your picture with graphic object.
public class MyCustomPanel extends JPanel{
private Image img;
public MyCustomPanel(// use constructor to get img or load it directly from below){
//load image
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
// Draws the img to the BackgroundPanel.
g.drawImage(img, 0, 0, this);
}
}
Then you can go to your main class and use this JPanel
MyCustomPanel mcp=new MyCustomPanel(//img path);
mcp.add(new JLabel("Hello"),BorderLayout.CENTER);
add(mcp),BorderLayout.CENTER);
//etc
I hope you understand
Next time post some code if you want to have answr faster and more complex/better suited for your case.

Zoom an Image after ActionListener

I try to create an Image View Program and have a problem in Java Image's zoom in and zoom out :D
I create a JPanel and using BufferedImage to display an image in my computer. After clicking a button, it should be zoom .
But the problem in here that, I overload the method paintComponent() in the JPanel to display a image as I want. After searching in the Google, I think I should use Graphic2D to deal with this problem. Follow on this post, the line
Graphics2D g = resizedImage.createGraphics();
g.drawImage(originalImage, 0, 0, newImageWidth , newImageHeight , null);
should be put in the overloaded method paintComponent(). Howerver, in my case, I want to zoom the image after clicking a button, so, how can i access to the paintComponent() to do the zoom ??
public class MiddlePanel extends JPanel {
private BufferedImage img;
private JButton jbtZoom = new JButton("Zoom");
public MiddlePanel(int width){
img = ImageIO.read(new FileInputStream(new File("C:\\Picture\\pic1.jpg")));
this.setPreferredSize(new Dimension(800,460));
}
public void paintComponent(Graphics g) {
g.drawImage(img......);
}
public void addComponentActionListener(){
jbtZoom.addActionListener(new ActionListener{
public void actionPerformed(){
//What should I do in here to zoom the image....
}
});
}
Thank for your help!
You need to change your design like so:
Store in a variable your zoom state and then your
overridden paintComponent method should look at that variable to
decide if/how much to zoom.
Your ActionListener will update the variable then call repaint() on
the panel.

Categories