java 彈幕 彈球 多執行緒
阿新 • • 發佈:2018-12-27
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); } @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub if(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 stub while(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(); } } } }