Aircraft of World War II - Background scroll

We set the background really MyPanel class, this class has paintComponent () method, we rewrite the method of the parent class () set the background position in g.drawImage, so in order to achieve the background rolling, we need background the position coordinates from the constant becomes variable.

First, the definition of a variable timer plastic, the equivalent program in time, we can facilitate this time variable to modify the position variables. So we also need to define a variable position.

	public int time = 0;
	public int top = 0;

In the paintComponent () to add the code, the position of the control variables

public void paintComponent(Graphics g)
	{
		super.paintComponent(g);
		g.drawImage(this.bgImage, 0,
				top-this.bgImage.getHeight(this), 
				this.bgImage.getWidth(this),
				this.bgImage.getHeight(this),
				null);
		g.drawImage(this.bgImage, 0,top, 
				this.bgImage.getWidth(this),
				this.bgImage.getHeight(this),
				null);
		
		time ++;
		
		if(time == 10000)
			time = 0;
		
	
		if(time % 10 == 0)
		{
			top ++;
			
			if(top >= this.bgImage.getHeight(this))
			{
				top = 0;
			}
			
		}
		
		
	}

At regular intervals of time we need to redraw our myPanel, so we put this to the thread, a thread of a new package. Package named thread
class named DrawbleThread, this class inherits the Thread

package thread;


public class DrawbleThread extends Thread{

	public DrawbleThread()
	{
		
		
	}
}

Since we want to call repaint () method MyPane in, so in the constructor of the thread class in need of MyPanel types of arguments. Remember import package.

package thread;

import view.MyPanel;

public class DrawbleThread extends Thread{
	
	public MyPanel myPanel;

	public DrawbleThread(MyPanel myPanel)
	{
		this.myPanel = myPanel;
		
	}
	
}


Additional:
When the program calls start () method creates a new thread, and then perform the run () method.

So rewrite the run () method in the thread. We redraw panel myPanel in the run method

package thread;

import view.MyPanel;

public class DrawbleThread extends Thread{
	
	public MyPanel myPanel;

	public DrawbleThread(MyPanel myPanel)
	{
		this.myPanel = myPanel;
		
	}
	
	public void run()
	{
		while(true)
		{
			this.myPanel.repaint();
			
		}
		
	}
	
}

Then we went to the constructor MyPanel class and started adding our thread.

package view;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;

import javax.swing.JPanel;

import thread.DrawbleThread;

public class MyPanel extends JPanel{
	
	public Image bgImage;
	
	public int time = 0;
	public int top = 0;
	
	public DrawbleThread drawbleThread;
	
	public MyPanel()
	{
		//设置背景
		this.bgImage = Toolkit.getDefaultToolkit().getImage("images/bg01.jpg");
		//创建线程
		this.drawbleThread = new DrawbleThread(this);
		//启动线程
		this.drawbleThread.start();
		
	}
	
	public void paintComponent(Graphics g)
	{
		super.paintComponent(g);
		g.drawImage(this.bgImage, 0,
				top-this.bgImage.getHeight(this), 
				this.bgImage.getWidth(this),
				this.bgImage.getHeight(this),
				null);
		g.drawImage(this.bgImage, 0,top, 
				this.bgImage.getWidth(this),
				this.bgImage.getHeight(this),
				null);
		
		time ++;
		
		if(time == 10000)
			time = 0;
		
	
		if(time % 10 == 0)
		{
			top ++;
			
			if(top >= this.bgImage.getHeight(this))
			{
				top = 0;
			}
			
		}
		
		
	}
	
	
}

Published 24 original articles · won praise 1 · views 1459

Guess you like

Origin blog.csdn.net/qq_43077318/article/details/104644016