Java Bullet-screen pinball multi-threading

Source: Internet
Author: User
Tags date now


Package applet; Import Java.awt.borderlayout;public class Tantan extends JFrame implements Actionlistener{private JPanel Contentpane;private JTextField Danmu; JButton button; tanqiu1 t; Tablerowsorter<tablemodel> Sorter;D efaulttablemodel model1;private JTable l1;private JTable ta1; String[][] s={}; String[] name1 ={"Time", "content"};/** * Launch the application. */public static void Main (string[] args) {Eventqueue.invokelater (New Runnable () {public void run () {try {Tantan frame = new Tantan (); frame.setvisible (true);} catch (Exception e) {E.print StackTrace ();}});} /** * Create the frame. */public Tantan () {settitle ("\u5f39\u5e55\u7a0b\u5e8f\u52a0\u4e0a\u5c0f\u5f39\u7403"); SetResizable (false); Setdefaultcloseoperation (Jframe.exit_on_close); SetBounds (+, 560, 476); contentpane = new JPanel (); Contentpane.setborder (New Emptyborder (5, 5, 5, 5)); Setcontentpane (ContentPane); contentpane.setlayout (null); t = new TANQIU1 (t.setsize); t.setlocation (0, 0); Contentpane.add (t);d Anmu = new JtextfiEld ();d anmu.setbounds (), 407, 321, Contentpane.add (DANMU);d Anmu.addactionlistener (This);d Anmu.setcolumns (10)    ; button = new JButton ("\u53d1\u9001"); Button.setforeground (Color.orange); Button.setbackground (Color.cyan); Button.setbounds (358, 406, ",") Button.addactionlistener (this); Contentpane.add (button); model1 = new DefaultTableModel (); Model1.setdatavector (null,name1); ta1 = new JTable (MODEL1); ta1.setenabled (false); JScrollPane j = new JScrollPane (TA1); J.setbounds (309,, 235, 371); Contentpane.add (j); JLabel label = new JLabel ("\u5f39\u5e55\u5185\u5bb9"); Label.setfont (New Font ("Microsoft Jas Black", Font.plain,)); label.setbounds (399, 5, (); Contentpane.add (label);} @Overridepublic void actionperformed (ActionEvent e) {//TODO auto-generated method Stubif (E.getsource () ==button| | E.getsource () ==danmu) {Date now = new Date (); String []SS = new string[2]; String str = Danmu.gettext (); System.out.println ("str" +str+ "str"); if (!str.equals (")") {t.set_s (str); ss[0]= now.gethours () + ":" +now.getminutes () +":"+now.getseconds (); Ss[1]=str;model1.addrow (ss);d Anmu.settext (" ");} Else{joptionpane.showmessagedialog (NULL, "Input cannot be empty");}}}    Class TANQIU1 extends jpanel{private int numbertanmu;    private int width;    private int height;    Private Thread []looper=new thread[10];    Private Thread []looper1=new thread[1000];    Private Tanmu []tanmu1=new tanmu[1000];        Private ball []ball1=new ball[10];    Tanqiu1 () {numbertanmu=1;    SetBounds (0,0,400,400);    width=300;               height=400;        Looper = new THREAD[10];        Looper1=new thread[1000];        Tanmu1=new tanmu[1000];         Tanmu1[0] = new Tanmu ("", 20);    looper1[numbertanmu-1]= new Thread (Tanmu1[numbertanmu-1]);        Looper1[numbertanmu-1].start ();            for (int i=0;i<10;i++) {int SX = (int) ((Math.random ())) *10);            int sy = (int) ((Math.random ()) *10);            if (sx==0&&sy==0) {sx=1;            Sy=1; } ball1[i]= new Ball (Sx,sy,10+i*10,30+i*10,width,height);            Looper[i] = new Thread (Ball1[i]);         Looper[i].start ();        }} public void Paint (Graphics g) {G.setcolor (Color.gray);        G.fillrect (0,0,300,400);        G.setcolor (Color.cyan);                   G.fillrect (0,0,300,20);        for (int i=0;i<10;i++) {g.setcolor (color.orange);    G.filloval (Ball1[i].get_x (), ball1[i].get_y (), 20,20);    } for (int i=0;i<numbertanmu;i++) {int x1 = (int) ((Math.random ()) *255);    int x2 = (int) ((Math.random ()) *255);    int x3 = (int) ((Math.random ()) *255);    G.setcolor (New Color (X1,X2,X3));    G.setfont (New Font ("Blackbody", font.bold,20));    g.DrawString (tanmu1[i].get_s (), TANMU1[I].GET_SX (), Tanmu1[i].get_sy ());    } repaint ();    } public void set_s (String s) {tanmu1[numbertanmu]=new tanmu (S, (numbertanmu%17+2) *20);    looper1[numbertanmu]= new Thread (Tanmu1[numbertanmu]);    Looper1[numbertanmu].start ();    numbertanmu++; }}class Ball Implements RUNNABLE{PRivate int spx;private int spy;private int x;private int y;private int width;private int height;ball (int a,int b,int C,int D,int E,int f) {spx=a;spy=b;x=c;y=d;width = E;height =f;} public void Run () {//TODO auto-generated method Stubwhile (True) {if (x<0| |    x>280-spx) {spx=-spx; } if (Y<20+math.abs (Spy) | |    Y>380-math.abs (Spy)) {spy=-spy;    } x+=spx;    Y+=spy;   try {thread.sleep;} catch (Interruptedexception e) {e.printstacktrace ();} }}public int get_x () {return X;} public int get_y () {return Y;}} Class Tanmu implements runnable{private string s;private int sx;private int SY;TANMU (String ss,int h) {s=ss;sx=10;sy=h;} Public String get_s () {return S;} public int get_sx () {return Sx;} public int Get_sy () {return Sy;} public void set_s (String ss) {s=ss;sx=0;}    public void Run () {Boolean flag = True;while (flag) {sx+=5;     if (sx>=300) Flag=false;   try {thread.sleep;} catch (Interruptedexception e) {e.printstacktrace ();} }}}


Java Bullet-screen pinball multi-threading

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.