-
Jpanel and Jframe help!!
Hi, I'm trying to make a background image and change that image while running the program. Can someone take a look at my code and see what im doing wrong. Right now the image changes but i have to resize the window to make it update.
Thanks
Code:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.*;
public class TextOnBackground extends JPanel {
BufferedImage image;
String text = "Hello World";
static TransparentMenuItem b1, b2;
static int x = 0;
static int y = 0;
static int h = 0;
static int w = 0;
static boolean menuShow = false;
public TextOnBackground(BufferedImage image) {
this.image = image;
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
add(b1).setBounds(x,y,w , h);
if(menuShow){
add(b2);
}
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
g2.drawImage(image,0, 0, this);
}
public static void funtion() {
b1 = new TransparentMenuItem("THIS IS A TEST, Click Here");
b2 = new TransparentMenuItem("And it worked.");
}
public void actionPerformed(ActionEvent e) {
System.out.println("CLICK");
}
public static void main(String[] args) throws IOException {
funtion();
BufferedImage one = ImageIO.read(new URL("http://www.wallpaperweb.org/wallpaper/babes/1920x1080/marisa_miller_20091211_1477.jpg"));
BufferedImage two = ImageIO.read(new URL("http://www.wallpaperland.net/wp-content/uploads/2010/02/Rebecca_Loos_sexy_1080p.jpg"));
BufferedImage three = ImageIO.read(new URL("http://www.wallpaperweb.org/wallpaper/user/PlayBoy_16687/gallery_1.htm#58459"));
BufferedImage four = ImageIO.read(new URL("http://www.wallpaperweb.org/wallpaper/user/PlayBoy_16687/gallery_1.htm#58349"));
BufferedImage five = ImageIO.read(new URL("http://www.wallpaperweb.org/wallpaper/user/PlayBoy_16687/gallery_1.htm#58278"));
// BufferedImage second = ImageIO.read(new URL(""));
TextOnBackground test1 = new TextOnBackground(one);
TextOnBackground test2 = new TextOnBackground(two);
TextOnBackground test3 = new TextOnBackground(three);
TextOnBackground test4 = new TextOnBackground(four);
TextOnBackground test5 = new TextOnBackground(five);
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(test1);
f.setSize(400,400);
f.setLocation(200,200);
f.setVisible(true);
for(int i= 0; i<200 ;i++){
try {
Thread.sleep(8);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
w+=1;
if(h<20)h+=1;
f.repaint();
}
f.getContentPane().add(test2);
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
f.remove(test1);
f.repaint();
}
}
-
Interesting choice of images there.
You shouldn't call Thread.sleep(...) on the main Swing thread but instead should use a Swing Timer. The tutorials will show you how to use this if you're not familiar with it.
-
I put in the swing timer, do you think thats a smart way of changing images? I feel like Im doing stupid there except for the images XD.
That last part probably makes me look bad... a little.
-
If I were simply displaying images, I'd put them in ImageIcons and display them in a JLabel. Then to swap, I'd simply swap out the icons via the JLabel's setIcon method. The thing that bothers me most about your code is your adding some component from within the paintComponent method, a method that you cannot directly control when or if it will be called, a method that you want to happen as fast as possible. You shouldn't be doing this there.
-
Thanks for the advice, that way makes more sense. I'm really new to java thats why I made so many mistakes. Gonna start changing it now.