Load picture and make moving pictures

Ideas: want to let the ball first need to let him move up in the window of the constant change of position can be achieved, is positioned directly in the code to start small ball to create a variable X and Y to make him stop redrawing action

// // load the window level fly ball // Import Package

import java.awt.*;

import javax.swing.*;

The code editor window

public class BallGame extends JFrame{

   int

i=1;

   Image

ball = Toolkit.getDefaultToolkit () getImage ( "images / ball.png");. // load the code image

   Image

desk = Toolkit.getDefaultToolkit().getImage(“images/desk.jpg”);
Here Insert Picture Description

   //给小球定位在窗口的位置

   double

x = 100; // coordinate pellets

   double

y = 100;

// direction of movement of the ball

   boolean

right = true;

   //让图片显示在窗口上的方法//画窗口的方法

   public

void paint(Graphics g){

          g.drawImage(desk,

0, 0, null);

          g.drawImage(ball,

(You) work, (you) y, null);

          if

(right) {

                 x=

x+10;

          }else{

                 x=x-10;

// small ball at the edge of this process would exceed the table so be subtracted from the corresponding else seems unreal

          }if

(X> 856-40-30) {// 856 is the width of the window, the width of the table 40, the ball 30 is a small diameter

                 right=false;

          }if(x<40){

// hit side will rebound

                 right

= true;

          }}

   void

launchFrame(){

          setSize(856,500);//窗口大小;

          setLocation(800,800);//出现位置定位

          setVisible(true);

          //重画窗口

Redraw is equivalent to refresh the window in order to see the ball moving up effect

          while(true){

                 repaint();

                 try{Thread.sleep(40);//毫秒Thread.sleep(40);//毫秒

                 }catch(Exception

e) {e.printStackTrace ();}}}

   //main方法是程序执行的入口

public static void main(String[] args){

    System.out.println("666");

    BallGame game=new BallGame();

    game.launchFrame();

}}

In this way the ball will move up the cycle
Here Insert Picture Description

Guess you like

Origin blog.csdn.net/weixin_44589117/article/details/90046409