#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    3
    Rep Power
    0

    Post Photo album applet


    Hello, I was wondering if anyone could help me with some Java code. I have an applet setup but my photos will not show up. The buttons work just not the photos. Any help would be great, thank you!

    Code:
    import java.awt.*;
    import java.applet.*;
    import java.applet.Applet;
    import java.awt.event.*;
    import java.awt.image.*;
    
    public class VirtualPhotoAlbum_ extends Applet implements ActionListener{
    
    // declare all the components
    		
    	Button
    			previous = new Button("Previous"),
    			first = new Button("First"),
    			last = new Button("Last"),
    			next = new Button("Next");
    		
    	Canvas
    			imageCanvas = new Canvas();
    		
    	final int total = 5;			// total # of images 
    	int i = 0;						// this is value of currently displayed image
    
    	Image Pictures[] = new Image[total];
    		
    	
    public void init() {
    
    	makeGui();
    	
    		// get all the images 
    	
    		for (int p=0; p < total; p++) {
    			Pictures[p] = getImage(getCodeBase(), "../../images/grump1.gif"+p+"../../images/grump2.gif"
                                    +p+"../../images/grump2.gif"+p+"../../images/grump3.gif"+p+"../../images/grump4.gif"
                                    +p+"../../images/grump5.gif");
    			prepareImage(Pictures[p], this);
    		}
    
    	} // init
    	
    	private void displayImage(int n) {
    		// Display the nth image on the canvas
    		Graphics g = imageCanvas.getGraphics();
    		g.clearRect(10, 10, 700, 700);
    		g.drawImage(Pictures[n], 30, 10, this);
    		g.drawString("Image: "+(n+1)+"/"+total, 30, 500);
    	} // displayImage
    	
    	public void drawFirst(){
    
    		displayImage(0);
    	
    	} // drawFirst
    	
    	public void drawLast(){
    
    		displayImage(total-1);
    		
    	} // drawLast
    	
    	public void drawPrevious(){
    
    		i = i - 1;
    		if (i <= -1){
    			i = total-1;
    		}
    		displayImage(i);
    	} // drawPrevious
    
    	public void drawNext(){
    
    		i = i + 1;
    		if (i == total){
    			i = 0;
    		}
    		
    		displayImage(i);
    	} // drawNext
    
        @Override
    	public void actionPerformed(ActionEvent e){
    
    		if (e.getSource() == previous){
    			drawPrevious();}
    		else if (e.getSource() == first){
    			drawFirst();}
    		else if (e.getSource() == last){
    			drawLast();}
    		else if (e.getSource() == next){
    			drawNext();}
    	} // actionPerformed
    
        @Override
    	public void paint(Graphics g) {
    		drawFirst();
    		i = 0;
    	}
    	
    	private void makeGui() {
    	
    		setBackground(Color.blue);
    		setForeground(Color.black);
    		setLayout(new BorderLayout());
    
    		Panel p1 = new Panel();
    		
    		p1.add(previous);  
    		p1.add(first);
    	
    		p1.add(next);
    		p1.add(last);
    	
    		add(BorderLayout.NORTH, p1);
    		add(BorderLayout.CENTER, imageCanvas);
    
    		previous.addActionListener(this);
    		first.addActionListener(this);
    		last.addActionListener(this);
    		next.addActionListener(this);
    	
    	} // makeGui
    } // PhotoAlbum
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,696
    Rep Power
    347
    Are there any error messages?

    The code does not use the recommended painting technique that overrides a paint method. See:http://docs.oracle.com/javase/tutori...ing/index.html

    The code that puts images into the pictures array is unusual looking.
    Why the long String of concatenated names?
    Do any of the images load?

    for testing try loading just one image (no array) and display that image in a method that overrides the paint method as shown in the link.
    Last edited by NormR; March 19th, 2013 at 06:59 AM.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    3
    Rep Power
    0
    Currently there are no errors however I was getting errors worked that out at the moment though. I've tried a few different types of arrays but, none work with my current code so i'd have to change just about all of it. I did have this working in a browser with url photos so that leads me to believe it is something with the images not loading like you referred. I also added sound, which is working fine just those images. Perhaps if you or anyone can spot an error of compatibility with the image code. I did try new ImagaIcon ... to add the photos and it took them in but then the g.drawImage handle does not work anymore.
    ill update the code...


    I have two bits highlighted below where i think the problem may be. Any help much appreciated. Thanks again!

    Code:
    import java.awt.*;
    import java.applet.*;
    import java.applet.Applet;
    import java.awt.event.*;
    import java.awt.image.*;
    
    public class VirtualPhotoAlbum extends Applet implements ActionListener{
    
    // declare all the components
    		
    	Button
    			previous = new Button("Previous"),
    			first = new Button("First"),
    			last = new Button("Last"),
    			next = new Button("Next");
    		
    	Canvas
    			imageCanvas = new Canvas();
    		
    	final int total = 5;			// total # of images 
    	int i = 1;						// this is value of currently displayed image
    
    	Image Pictures[] = new Image[total];
    		
    	private String file = "../../sound/Maid.wav";
            
        @Override
        public void init() {
    AudioClip ac = getAudioClip(getCodeBase(), file);
    ac.loop(); 
    	makeGui();
    	
    		
    Code:
    // get all the images 
     
    		for (int p=0; p < total; p++) {
    			Pictures[p] = getImage(getCodeBase(), "../../images/grump1.GIF"+p+"../../images/grump2.GIF"
                                    +p+"../../images/grump2.GIF"+p+"../../images/grump3.GIF"+p+"../../images/grump4.GIF"
                                    +p+"../../images/grump5.GIF");
    			prepareImage(Pictures[p], this);
    } } // init
    Code:
    private void displayImage(int n) {
    		// Display the nth image on the canvas
     
    		Graphics g = imageCanvas.getGraphics();
    		g.clearRect(10, 10, 700, 700);
    		g.drawImage(Pictures[n], 30, 10, this);
    		g.drawString("Image: "+(n+1)+"/"+total, 30, 500);
    	} // displayImage
    public void drawFirst(){ displayImage(1); } public void drawLast(){ displayImage(total - 1); } // drawLast public void drawPrevious(){ i = i - 1; if (i <= -1){ i = total-1; } displayImage(i); } // drawPrevious public void drawNext(){ i = i + 1; if (i == total){ i = 0; } displayImage(i); } // drawNext @Override public void actionPerformed(ActionEvent e){ if (e.getSource() == previous){ drawPrevious();} else if (e.getSource() == first){ drawFirst();} else if (e.getSource() == last){ drawLast();} else if (e.getSource() == next){ drawNext();} } // actionPerformed @Override public void paint(Graphics g) { drawFirst(); i = 1; } private void makeGui() { setBackground(Color.blue); setForeground(Color.black); setLayout(new BorderLayout()); Panel p1 = new Panel(); p1.add(first); p1.add(previous); p1.add(next); p1.add(last); add(BorderLayout.NORTH, p1); add(BorderLayout.CENTER, imageCanvas); previous.addActionListener(this); first.addActionListener(this); last.addActionListener(this); next.addActionListener(this); setSize (400,400); } // makeGui } // PhotoAlbum
  6. #4
  7. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,696
    Rep Power
    347
    Did you try it with a single image to see if the code would load and display that?

    When that works, then work on loading multiple images and displaying them.

IMN logo majestic logo threadwatch logo seochat tools logo