JAVA Frame form background image with the first scroll code instance attached

  • 2020-06-23 00:21:33
  • OfStack

Background image scrolling, the program has run. The premise! The width of the background image is longer than that of the window. The code is as follows:


import Java.awt.Graphics;
import java.awt.Image;
import javax.swing.ImageIcon;
import mine.game.util.PropertiesUtil;
@SuppressWarnings("serial")
public class GameFrame extends MyFrame{
private Image img=ImageUtil.imageLoad("image/bk.jpg");
double movs,speed=1,headmovs;
double pWidth,pHeight,bgWidth;
@Override
public void paint(Graphics g) {
//g.drawImage(img, 0, 0, null);
//===================================================
pWidth=PropertiesUtil.getValue("Width", "game.properties");
pHeight=PropertiesUtil.getValue("Height", "game.properties");
bgWidth=new ImageIcon(img).getIconWidth();
//movs+=speed;
if(bgWidth>pWidth+movs){
g.drawImage(img, 0, 0, (int)pWidth,(int)pHeight, (int)movs, 0, (int)(pWidth+movs), (int)pHeight, null);
}
if(bgWidth<=pWidth+movs){
headmovs=pWidth+movs-bgWidth;
g.drawImage(img, 0, 0, (int)(pWidth-headmovs),(int)pHeight, (int)movs, 0, (int)(bgWidth), (int)pHeight, null);
g.drawImage(img,(int)(pWidth-headmovs), 0, (int)pWidth,(int)pHeight, 0, 0, (int)(headmovs), (int)pHeight, null);
if(headmovs>=pWidth){
// Reinitialize all variable data, loop 
movs=headmovs-pWidth;
}
}
movs+=speed;
//===================================================
}
public static void main(String[] args) {
GameFrame gf=new GameFrame();
gf.launchFrame();
}
}
//=================================
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import mine.game.util.PropertiesUtil;
@SuppressWarnings("serial")
public class MyFrame extends Frame{
private BufferedImage imgBuffer;
private Graphics gBuffer;
public void launchFrame(){
int wd=800;//PropertiesUtil.getValue("Width", "game.properties");
int ht=600;//PropertiesUtil.getValue("Height", "game.properties");
setSize(wd,ht);
 setLocation(0, 0);
 setVisible(true); 
 new PaintThread().start(); 
 addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
} 
});
}
// Redraw window thread, inner class 
class PaintThread extends Thread{
public void run(){
while(true){
repaint();
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
/**
 *  Double buffering solution, screen flash . This method is inherited Frame the AWT It works in programming. JFram Don't work , It has its own advanced way of implementation (guess, have time to learn) 
 */
@Override
public void update(Graphics g) {
if(imgBuffer==null){
imgBuffer=(BufferedImage)createImage(this.getWidth(),this.getSize().height);// Creating a graphics buffer 
//imgBuffer=new BufferedImage((int)this.getSize().getWidth(),(int)this.getSize().getHeight(),BufferedImage.TYPE_4BYTE_ABGR);// Creating a graphics buffer 
}
gBuffer=imgBuffer.getGraphics();// Gets the graphics context of the graphics buffer 
 gBuffer.fillRect(0, 0, this.getWidth(), this.getHeight());
this.paint(gBuffer);// with paint Method to write the drawing process to draw the graph buffer 
gBuffer.dispose();// Free up graphics context resources 
g.drawImage(imgBuffer, 0, 0, null);// Draws the graphics buffer to the screen 
}
}
//====================
import java.awt.Image;
import java.awt.Toolkit;
import java.NET.URL;
public class ImageUtil {
public static Image imageLoad(String path){
URL u=ImageUtil.class.getClassLoader().getResource(path);
return Toolkit.getDefaultToolkit().getImage(u);
}
}

I hope you found the above code helpful


Related articles: