java 彈幕 彈球 多線程

來源:互聯網
上載者:User

標籤:


package 小程式;import java.awt.BorderLayout;public class tantan extends JFrame implements ActionListener{private JPanel contentPane;private JTextField danmu;JButton button ;tanqiu1 t;TableRowSorter<TableModel> sorter ;DefaultTableModel model1;private JTable l1;private JTable ta1;String[][] s={};String[] name1 ={"時間","內容"};/** * 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.printStackTrace();}}});}/** * Create the frame. */public tantan() {setTitle("\u5F39\u5E55\u7A0B\u5E8F\u52A0\u4E0A\u5C0F\u5F39\u7403 ");setResizable(false);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setBounds(100, 100, 560, 476);contentPane = new JPanel();contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));setContentPane(contentPane);contentPane.setLayout(null);t = new tanqiu1();t.setSize(300, 400);t.setLocation(0, 0);contentPane.add(t);danmu = new JTextField();danmu.setBounds(10, 407, 321, 21);contentPane.add(danmu);danmu.addActionListener(this);danmu.setColumns(10);    button = new JButton("\u53D1\u9001");button.setForeground(Color.ORANGE);button.setBackground(Color.CYAN);button.setBounds(358, 406, 93, 23);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, 27, 235, 371);contentPane.add(j);JLabel label = new JLabel("\u5F39\u5E55\u5185\u5BB9");label.setFont(new Font("微軟雅黑", Font.PLAIN, 13));label.setBounds(399, 5, 52, 17);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);danmu.setText("");}else{JOptionPane.showMessageDialog(null, "輸入不可為空");}}}}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("黑體",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(50);} 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(50);} catch (InterruptedException e) {e.printStackTrace();}   }}}


java 彈幕 彈球 多線程

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.