This discussion is archived
7 Replies Latest reply: Jun 10, 2010 3:08 AM by 843853 RSS

Background scrolling

843853 Newbie
Currently Being Moderated
Hi all,
I've changed this source code so I can move the character from bottom to up but scrolling background is up to bottom. How I can change this class for scrolling background from up to down and not viceversa?

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.awt.Image;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.MediaTracker;
import java.awt.Toolkit;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
public class JAniDemo {
  private BufferedImage biBackground = null;
  private Toolkit tk;
  private JFrame f;
  JAniDemo(){
    tk = Toolkit.getDefaultToolkit();
    f = new JFrame("AniDemo 1.0");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    loadBackground("C:\\image.png");
    myPanel p = new myPanel(biBackground);
    f.add(p);
    f.pack();
    f.setVisible(true);
  }
  private void loadBackground(String fn){
    Image im = tk.getImage(fn);
    MediaTracker mt = new MediaTracker(f);
    mt.addImage(im,0);
    try{
      mt.waitForID(0);
      biBackground = new BufferedImage(800, 641, BufferedImage.TYPE_INT_RGB);
      Graphics2D g2 = biBackground.createGraphics();
      g2.drawImage(im, 0, 0, null);
      g2.dispose();
    }catch(InterruptedException e){
      System.out.println(e.toString());
    }
  }
  public static void main(String[] args) {
    new JAniDemo();
  }
  class myPanel extends JPanel implements ActionListener{
    private BufferedImage background;
    private BufferedImage bi = null;
    private int upperCorner = 0;
    int width;
    int height;
    myPanel(BufferedImage bg){
      super();
      background = bg;
      width = background.getWidth()/2;
      height = background.getHeight();
      bi = background.getSubimage(0,upperCorner,width,height);
      setPreferredSize(new Dimension(width, height));
      Timer t = new Timer(25, this);
      t.start();
    }
    public void actionPerformed(ActionEvent e){  
      upperCorner -= 1;
      if(UpperCorner >= height) UpperCorner = 38;  // adjust to make rollover seemless
      bi = background.getSubimage(0,UpperCorner,width,height);
      repaint();
    }
    public void paintComponent(Graphics g){
      g.drawImage(bi, 0, 0, this);
    }
  }
}
Please see this changes of original source code:
bi = background.getSubimage(0,upperCorner,width,height);
width = background.getWidth()/2;
if(UpperCorner >= height) // for vertical
upperCorner -= 1; // for up to down scrolling

Can you help me? Thanks.
Regards.
  • 1. Re: Background scrolling
    796262 Newbie
    Currently Being Moderated
    It's important to understand what your code does instead of just hacking at somebody else's code until it does what you want.

    Better yet, I would say you should start from scratch and come up with your own scrolling background.

    I know that's not the answer you want to hear, but it's the truest one you're going to get.
  • 2. Re: Background scrolling
    843853 Newbie
    Currently Being Moderated
    It's the same of here:
    http://forums.sun.com/thread.jspa?threadID=5324388

    but now background, scrolling up to down and not viceversa.
  • 3. Re: Background scrolling
    796262 Newbie
    Currently Being Moderated
    Rapworld wrote:
    It's the same of here:
    http://forums.sun.com/thread.jspa?threadID=5324388

    but now background, scrolling up to down and not viceversa.
    Ok? What I said still stands.

    What changes did you make? What did you think those changes would do? What did they do instead? Why do you think that is?
  • 4. Re: Background scrolling
    843853 Newbie
    Currently Being Moderated
    I've changed the original source code (please see my change code) so now it scroll from UP to down but not from bottom to UP,
    How I can change it?
    thx for help me, regards.

    Edited by: Rapworld on 9-giu-2010 16.39
  • 5. Re: Background scrolling
    796262 Newbie
    Currently Being Moderated
    Rapworld wrote:
    I've changed the original source code (please see my change code) so now it scroll from UP to down but not from bottom to UP,
    You still haven't explained why you thought your changes would accomplish your goal (surely you aren't just randomly copying and pasting, right?), what it does instead, or why you think it does that.
    How I can change it?
    By first understanding how it works. And the best way to do that is to write your own code instead of copying and pasting code you found on the internet.
    thx for help me, regards.
    Honestly, based on the questions you're asking here and in your other posts, I'd say your best bet is to go back to the basic tutorials and work your way up. You're trying to learn how to swim by jumping in the deep end, which simply does not work for programming.

    Good luck.
  • 6. Re: Background scrolling
    843853 Newbie
    Currently Being Moderated
    Done...
  • 7. Re: Background scrolling
    843853 Newbie
    Currently Being Moderated
    Close topic. Thanks so much.