Java Strength Pinball Ball implementation code _java

Source: Internet
Author: User
Tags static class

First look at the effect chart:

directly on the code.
Adjust the button and draw a few circles on the canvas, then realize the monitor ...

Package cn.hncu.threadDemo.thread2;
Import Java.awt.Canvas;
Import Java.awt.Color;
Import Java.awt.Graphics;
Import java.awt.event.ActionEvent;
Import Java.awt.event.ActionListener;
Import java.awt.event.FocusEvent;

Import Java.awt.event.FocusListener;
Import Javax.swing.JFrame;
Import Javax.swing.JLabel;
Import Javax.swing.JPanel;
Import Javax.swing.JSpinner;
Import Javax.swing.Timer;
Import javax.swing.event.ChangeEvent;

Import Javax.swing.event.ChangeListener;
  public class Ballsjframe extends JFrame implements changelistener{private Ballscanvas ball;

  Private JSpinner spinner;
    Public Ballsjframe () {super ("Bouncing Ball");
    This.setbounds (300, 200, 400, 300);
    This.setdefaultcloseoperation (Exit_on_close);

    Color colors[] = {Color.red,color.green,color.blue,color.magenta,color.cyan};
    Ball = new Ballscanvas (colors,100);
    This.getcontentpane (). Add (ball);/default is center location JPanel Panel = new JPanel ();
    This.getcontentpane (). Add (Panel, "South"); Panel.add (New JLabel ("Delay"));
    Spinner = new JSpinner ();

    Spinner.setvalue (100);

    Panel.add (spinner);

    Spinner.addchangelistener (this);
  This.setvisible (TRUE);
    @Override public void statechanged (ChangeEvent e) {int value = Integer.parseint ("" +spinner.getvalue ());
  Ball.setdelay (value);
  public static void Main (string[] args) {new ballsjframe ();  } class Ballscanvas extends Canvas implements ActionListener, focuslistener{private Ball balls[];//store all the balls private Timer Timer;//javax.swing.timer public Ballscanvas (Color colors[], int delay) {this.balls = new ball[colors.length
    ];

    for (int i=0,x=40;i<colors.length;i++,x+=20) {This.balls[i] = new Ball (x,x,colors[i));


    //Let the current canvas monitor the Focus event This.addfocuslistener (this);
    Timer = new timer (delay,this);

  Timer.start ();
  public void Setdelay (int delay) {timer.setdelay (delay); @Override public void Paint (Graphics g) {for (int I=0;i<this.balls.length;i++) {g.setcolor (balls[i].color); Let's change the coordinates of each ball---(x coordinates) balls[i].x = Balls[i].left?
      balls[i].x-10:balls[i].x+10; When the ball hits the wall, change the direction of the ball if (balls[i].x<=0| |
      Balls[i].x>=this.getwidth () -24) {balls[i].left =!balls[i].left;//Toggle Direction}//Let the coordinates of each ball change---(Y-coordinate) Balls[i].y = balls[i].up?
      balls[i].y-10:balls[i].y+10; When the ball hits the wall, change the direction of the ball if (balls[i].y<=0| | Balls[i].y>=this.getheight () -22) {balls[i].up =!balls[i].up;//Toggle Direction} g.filloval (balls[i].x, Bal
    LS[I].Y, 20, 20);
    @Override public void actionperformed (ActionEvent e) {//system.out.println ("AAA");
  Repaint (),//Refresh canvas. Call Paint (Graphics g)} @Override public void focusgained (FocusEvent e) {timer.stop ();

  @Override public void Focuslost (FocusEvent e) {Timer.restart ();
    private static class ball{int x,y;
    Boolean up,left;
    Color color; public Ball (int x, int y, color color) {This. x = x;
      This.y = y;
      This.color = color;
    Up = left = false;
 }

  }




}

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.