Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

About images in applet

 
Richard Jee
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • 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);
}
}
 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thank you very much!!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic