Results 1 to 5 of 5
  1. #1
    Java_n00b is offline Member
    Join Date
    Jan 2011
    Posts
    6
    Rep Power
    0

    Default 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

    Java 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();
                
        		
            
        }
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  3. #3
    Java_n00b is offline Member
    Join Date
    Jan 2011
    Posts
    6
    Rep Power
    0

    Default

    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.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  5. #5
    Java_n00b is offline Member
    Join Date
    Jan 2011
    Posts
    6
    Rep Power
    0

    Default

    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.

Similar Threads

  1. JFrame and JPanel
    By khiat in forum New To Java
    Replies: 6
    Last Post: 01-15-2011, 08:32 PM
  2. Help with JPanel and JFrame
    By weikang in forum AWT / Swing
    Replies: 3
    Last Post: 11-26-2010, 02:22 AM
  3. JPanel not showing up in JFrame
    By rlindsey in forum AWT / Swing
    Replies: 2
    Last Post: 06-25-2010, 08:21 AM
  4. JPanel/JFrame
    By Mayur in forum New To Java
    Replies: 2
    Last Post: 12-20-2009, 06:07 AM
  5. scroll a Jpanel in a JFrame
    By nidhirastogi in forum SWT / JFace
    Replies: 1
    Last Post: 09-07-2008, 04:42 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •