• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

About images in applet

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can someone help me to find how I cannot see the pictures in my applet. I found that the paint is not working in the Canvas, even I have repainted it in the actionlistener.
Thx in advance
//======================================
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
public class Images extends JApplet
{
private int curr = 0;
private final int SIZE = 8;
private final int WIDTH = 640;
private final int LENGTH = 480;
private Image[] imgs = new Image[SIZE];
private Panel northPanel = new Panel();
private Panel southPanel = new Panel();
private Panel centerPanel = new Panel();
private JButton previousButton = new JButton("PREVIOUS");
private JButton nextButton = new JButton("NEXT");
private ShowPicture currPic;
public class ShowPicture extends Canvas
{
public void paint(Graphics g)
{
g.drawImage(imgs[curr], 0, 0, this);
}
public ShowPicture()
{
}
}
public void init()
{
for (int i = 0; i < SIZE ;i++ )
{
imgs[i] = getImage(getDocumentBase(),
"image00" + (i + 1) + ".jpg").getScaledInstance(
WIDTH,LENGTH,Image.SCALE_SMOOTH);
}
curr = 0;
getContentPane().setLayout(new BorderLayout());
currPic = new ShowPicture();
centerPanel.setBackground(Color.lightGray);
centerPanel.add(currPic);
nextButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
curr = curr + 1;
curr = curr % SIZE;
showStatus("Current image is " + "image00" + (curr + 1) + ".jpg");
currPic.repaint();
}
});
previousButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
curr = curr - 1;
if (curr < 0)
{
curr += SIZE;
}
curr = curr % SIZE;
showStatus("Current image is " + "image00" + (curr + 1) + ".jpg");
currPic.repaint();
}
});
southPanel.add(previousButton);
southPanel.add(nextButton);
getContentPane().add(southPanel, "South");
getContentPane().add(centerPanel, "Center");
setVisible(true);
}
}
 
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Works okay now. I added a MediaTracker to block program execution until your images are completely loaded (see api for an example) and an override of the getPreferredSize method in the ShowPicture class.
 
Richard Jee
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much!!!
reply
    Bookmark Topic Watch Topic
  • New Topic