0

i have this code. in this code an image is moving left to right with moveImage method and moves right to left with moveimg method in the code. what i want now is to work a button event. there is a button in code, i want when i click the button it should do its job. but it's not doing.. here the code is:

    import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class MyImage extends JFrame implements ActionListener
{
   static int xPixel = 20;
   Image myImage, offScreenImage;
   Graphics offScreenGraphics;
   JPanel p = new JPanel();
   Button btn = new Button("bun");
   JFrame f = new JFrame();

   public MyImage()
   {
      myImage = Toolkit.getDefaultToolkit().getImage("mywineshoplogo.jpg");
      setExtendedState(JFrame.MAXIMIZED_BOTH);
      this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setVisible(true);
      add(p);
      p.add(btn);
      moveImage();
      btn.addActionListener(this);
   }

   public void update(Graphics g)
   {
      paint(g);
   }

   public void paint(Graphics g)
   {
      int width = getWidth();
      int height = getHeight();
      if (offScreenImage == null)
      {
         offScreenImage = createImage(width, height);
         offScreenGraphics = offScreenImage.getGraphics();
      }
// clear the off screen image  
      offScreenGraphics.clearRect(0, 0, width + 1, height + 1);
// draw your image off screen  
      offScreenGraphics.drawImage(myImage, xPixel, 10, this);
// draw your image off screen  
// show the off screen image  
      g.drawImage(offScreenImage, 0, 0, this);
// show the off screen image  
   }

   void moveImage()   //left to right move
   {
      for (int i = 0; i < 530; i++)
      {

         xPixel += 1;
         repaint();
        // then sleep for a bit for your animation  
         try
         {
            Thread.sleep(40);
         } /* this will pause for 50 milliseconds */

         catch (InterruptedException e)
         {
            System.err.println("sleep exception");
         }
      }
   }

/*   void moveimg()   // right to left move
   {
      for (int i = 529; i > 0; i--)
      {
         if (i == 1)
         {
            moveImage();
         }
         xPixel -= 1;
         repaint();
// then sleep for a bit for your animation  
         try
         {
            Thread.sleep(40);
         } // this will pause for 50 milliseconds 

         catch (InterruptedException e)
         {
            System.err.println("sleep exception");
          }
      } 
   } */     

   public void actionPerformed(ActionEvent ae)
   {
      try
      {
         if (ae.getSource() == btn)
         {
            p.setBackground(Color.RED);
         }
      }
      catch (Exception e)
      {
         System.out.println("error");
      }
   }

   public static void main(String args[])
   {
      MyImage me = new MyImage();
   }
}

1 Answers1

1

If you want the program to quit when the close button is pressed, you need to do a few things. First, insted of extending Frame, extend JFrame. Then, in the constructor before you call setVisible() or moveImage(), add the following code:

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

This makes it so that the JFrame calls System.exit(0) when you press the close button on the frame.

If you want to get your button to work, you'll need to add several things to your code. First, you'll need to add "implements ActionListener" to your class header.

public class MyImage extends JFrame implements ActionListener {

Then, in your body of the class, add the following method:

@Override
public void actionPerformed(ActionEvent e){

}

Then, in your constructor after you create a new button, add yourButton.addActionListener(this); This makes a listener and registers it with your button. Whenever your button has an action, like being clicked, it calls the actionPerformed method.

If you want to use this to to exit the for loop, I suggest adding a boolean that starts false, but is set to true when your button is clicked. Then in your for loop, add

if(exitLoop == true) break;

This exits (breaks) from your loop if the boolean, exitLoop, is true.

dmichaelc
  • 101
  • 5