• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem in the JVM ??????????????

 
Sameera Abeysinghe
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have this applet that takes shows some pic
(Shown below)
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
public class Slide extends javax.swing.JApplet {
private javax.swing.JButton btnext = new javax.swing.JButton();
private javax.swing.JButton btplay = new javax.swing.JButton();
private javax.swing.JButton btprevious = new javax.swing.JButton();
private javax.swing.JButton btstop = new javax.swing.JButton();
private javax.swing.JPanel jPanel1 = new javax.swing.JPanel();
private javax.swing.JPanel jPanel2 = new javax.swing.JPanel();
private javax.swing.JLabel lbpic = new javax.swing.JLabel();
private java.awt.image.BufferedImage buim;
private javax.swing.Timer time;
private String picName[];
private int pic=0;
/** Creates a new instance of Slide */
public Slide() {
}
public void init(){
getContentPane().setLayout(null);
this.setSize(new java.awt.Dimension(450,500));
}
public void paint(java.awt.Graphics g){

btprevious.setText("Previous");
btprevious.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
//btprevious.setEnabled(false);
btprevious.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btpreviousActionPerformed(evt);
}
});
getContentPane().add(btprevious);
btprevious.setBounds(new java.awt.Rectangle(10, 440, 70, 30));

btnext.setText("Next");
btnext.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
/*btnext.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnextActionPerformed(evt);
}
});*/
btnext.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
btnextMouseClicked(evt);
}
});
getContentPane().add(btnext);
btnext.setBounds(new java.awt.Rectangle(90, 440, 70, 30));

btstop.setText("Stop");
btstop.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
//btstop.setEnabled(false);
btstop.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
//btstopActionPerformed(evt);
}
});
getContentPane().add(btstop);
btstop.setBounds(new java.awt.Rectangle(330, 440, 70, 30));

btplay.setText("Play");
btplay.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
btplay.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
//btplayActionPerformed(evt);
}
});
getContentPane().add(btplay);
btplay.setBounds(new java.awt.Rectangle(250, 440, 70, 30));

lbpic.setBackground(new java.awt.Color(255, 255, 255));
//lbpic.setIcon(new javax.swing.ImageIcon("C:\\Projects\\slide\\Testing\\testttt\\JavaApplication4\\build\\classes\\AA1_10.gif"));
getContentPane().add(lbpic);
lbpic.setBounds(10, 409, 340, 50);

time=new javax.swing.Timer(3000,new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
//playslid(evt);
}
});

getImage();
//buim = loadImage(picName[pic]);
paintImage(g);
}
public void getImage(){
picName=new String[]{"AA1_01.gif","AA1_02.gif","AA1_03.gif","AA1_04.gif","AA1_05.gif","AA1_06.gif","AA1_07.gif","AA1_08.gif","AA1_09.gif","AA1_10.gif"};
}
public void paintImage(java.awt.Graphics g){
buim = loadImage(picName[pic]);
System.out.println("pic in paintimage = "+pic);

g.drawImage(buim,10,20,400,400,null);
//pic=pic+1;
System.out.println("pic in paintimage = "+pic);
//repaint();
}
public java.awt.image.BufferedImage loadImage(String name){
java.net.URL url=null;
try{
url = getClass().getClassLoader().getResource(name);
System.out.println(url);
return javax.imageio.ImageIO.read(url);
}catch(Exception e){
System.out.println("Cannot load image " + name + " in " + url);
System.out.println(e);
System.exit(0);
return null;
}
}
private void btnextActionPerformed(java.awt.event.ActionEvent evt) {
/*if(pic==0){
btprevious.setEnabled(true);
}
if(pic == picName.length-2){
btnext.setEnabled(false);
btplay.setEnabled(false);
}*/
//repaint();
//pic=pic+1;
//paintImage(buim.getGraphics());
}
private void btnextMouseClicked(java.awt.event.MouseEvent evt) {
repaint();
pic=pic+1;
paintImage(buim.getGraphics());
}
private void btpreviousActionPerformed(java.awt.event.ActionEvent evt) {
/*if(pic==1){
btprevious.setEnabled(false);
}
if(pic==picName.length-1){
btnext.setEnabled(true);
btplay.setEnabled(true);
}*/
pic=pic-1;
paintImage(buim.getGraphics());
}
}
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
As you can see I am printing the url I the loadImage method
When I click the next button some how it loads the loadimage method random times. I found out this by printing the url.
Some time it print the url from 1st image to 6th image and the3 6th image will be shown
Some time it print the url from 1st to 3rd image and the 3rd image will be show
Each time I run the applet this happens
The loadimage gets called random time
What I have done wrong

/////////////////////////////////////////////////////////////////////////
after posting the above part i change some coding like this


btnext.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
System.out.println("hi");
btnextActionPerformed(evt);
}
});

I found out that my btnext.addActionListener event gets fired several times after I click the button only ones. Because �hi� gets printed.

Why is it doing that. Is it possible that it is because of a problem in my code of in the jvm. I tested in a another pc. Same result came.

Please help me.
[ June 30, 2005: Message edited by: Sameera Abeysinghe ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic