Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

scroll an image

  Asked By: Don    Date: Mar 04    Category: Java    Views: 710
  

Desprately trying to get some help with trying to write an
application that will scroll an image. Any help will be much
appreciated.

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

public class DrawImage extends JFrame implements ActionListener {

private Image image;

private JButton jbtLeft, jbtRight;

}
public static void main(String []args) {

DrawImage frame = new DrawImage();
setTitle("Draw Image To Move Left And Right");
frame.pack();
frame.setVisible(true);

public DrawImage()
{


image = getImage(getDocumentBase(), "image1.gif");

JPanel jpButtons = new JPanel();
jpButtons.setLayout(new FlowLayout());
jpButtons.add(jptLeft = new JButton());
jpButtons.add(jbtRight = new JButton());

jbtLeft.setText("<=");
jbtRight.setText("=>");

jbtLeft.setMnemonic('L');
jbtRight.setMnemonic('R');

jbtLeft.setToolTipText("Move image to the left");
jbtRight.setToolTipText("Move image to the right");

getContentPane().setLayout(newBorderLayout());
getContentPane().add(image, BorderLayout.CENTER);
getContentPane().add(jpButtons, BorderLayout. SOUTH);
}

public void actionPerformed(ActionEvent e)
{
if (e.getSource() == jbtLeft)
{
left();

else if (e.getSource() == jbtRight)
{
right();
}
}
public void left()
{
int x = image.getXCoordinate();

if (x > 10)
{
image.setXCoordinate(x-10);
image.repaint;
}
}

public void right()
{
int x = image.getXCoordinate();

if (x < getSize().width - 20)
{
image.setXCoordinate(x+10);
image.repaint();
}
}
}

Share: 

 

No Answers Found. Be the First, To Post Answer.

 
Didn't find what you were looking for? Find more on scroll an image Or get search suggestion and latest updates.




Tagged: