java - How to make a scrolling effect for a background image -


i need make scrolling background platformer. needs scroll 2400x500 image while frame size 1440x900. if gradually change on time great. code:

import java.awt.*; import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.util.*;   import javax.swing.*;  public class main extends jframe {     public main() {          //creates title image          jlabel title = new jlabel(" ");         imageicon tl = new imageicon("title.gif");         title.seticon(tl);          //creates start image         final jbutton start = new jbutton("");         imageicon st = new imageicon("start.gif");         start.seticon(st);          //creates options image         jbutton options = new jbutton("");         imageicon opt = new imageicon("options.gif");         options.seticon(opt);         options.setbackground(color.black);          //creates label level 0 background image         jlabel background = new jlabel(" ");         imageicon = new imageicon("level0.gif");         background.seticon(back);          //creates panel level 0         final jpanel p5 = new jpanel();         p5.setlayout (new borderlayout(1, 1));         p5.add(background);          //create first frame "start" button         final jpanel p1 = new jpanel();         p1.setlayout(new gridlayout(1, 1));         p1.add(start, borderlayout.center);          //create second panel title label         final jpanel p2 = new jpanel(new borderlayout());         p2.setlayout(new gridlayout(1, 3));         p2.add(title, borderlayout.west);          //create third panel "options" button         final jpanel p3 = new jpanel(new borderlayout());         p3.setlayout(new gridlayout(1, 1));         p3.add(options, borderlayout.south);          //creates fourth panel organize other primary         final jpanel p4 = new jpanel(new borderlayout());         p4.setlayout(new gridlayout(1, 3));         p4.add(p1, borderlayout.west);         p4.add(p2, borderlayout.center);         p4.add(p3, borderlayout.east);          //when button clicked, changes level         start.addactionlistener(new actionlistener() {             @override             public void actionperformed(actionevent e) {                 if(start.isenabled()) {                     remove(p4);                     add(p5, borderlayout.center);                     invalidate();                     validate();                 }                 else {                     return;                 }             }         });          //adds fourth panel frame         add(p4, borderlayout.center);     }      public static void main(string args[]) {         main frame = new main();          //finds screen size of monitor         dimension screensize = toolkit.getdefaulttoolkit().getscreensize();          //creates frame         frame.settitle("cockadoodle duty: awakening");         frame.setsize(screensize);         frame.setlocale(null);          frame.setdefaultcloseoperation(jframe.exit_on_close);         frame.setvisible(true);         string background = "#000000";         frame.setbackground(color.decode(background));     } } 

  1. add label containing icon scroll panel
  2. use swing timer schedule scrolling
  3. when timer fires can scroll background.

the scrolling code might like:

jviewport viewport = scrollpane.getviewport(); point position = viewport.getviewposition(); position.x += 2;  viewport.setviiewposition( position ); 

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -