问题描述
我的代码实现不了重绘,请大家帮忙看看哈!importjava.awt.*;publicclassPrintTaskextendsJPanelimplementsRunnable//,MouseListener,MouseMotionListener{intnum;intn;inttype;RandomrandomNumber;longstart,end;intcolor1,color2,color3;intrandomX,randomY;intstartX,startY;intsumX,sumY;booleanover;Colorc;JButtonbtn;Threadthread[];MouseEventevent;ExecutorServiceexecutor;//JFrameframe;//PolygonsJPanelpolygonsJPanel;publicPrintTask(){JFrameframe=newJFrame("弹球");frame.setLayout(newBorderLayout());frame.add(btn,BorderLayout.NORTH);frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);btn=newJButton("Start");//frame.add(polygonsJPanel);//addpolygonsJPaneltoframeframe.setSize(1280,650);//setframesizeframe.setVisible(true);frame.setResizable(false);randomNumber=newRandom();color1=randomNumber.nextInt(256);color2=randomNumber.nextInt(256);color3=randomNumber.nextInt(256);randomX=randomNumber.nextInt(15);randomY=randomNumber.nextInt(15);over=false;startX=600;startY=300;sumX=startX;sumY=startY;start=0;addMouseMotionListener(newMouseMotionAdapter(){publicvoidmouseMoved(MouseEvente){//TODOAuto-generatedmethodstubover=isOver(e.getX(),e.getY());}});/*Stringstr=JOptionPane.showInputDialog("Pleaseenterthenumberofballs");n=Integer.parseInt(str);btn.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEventevent){thread=newThread[n];start=System.currentTimeMillis();for(inti=0;i<n;i++){thread[i]=newThread(newPrintTask());thread[i].start();}}});*/}booleanisOver(intx,inty){if(x!=sumX&&y!=sumY&&x!=1280&&y!=650){type=0;if(sumX>1280)type=1;if(sumX<0)type=2;if(sumY>650)type=3;if(sumY<0)type=4;switch(type){case0:{sumX+=randomX;sumY+=randomY;break;}case1:{sumX-=randomX;sumY+=randomY;break;}case2:{sumX=0;sumY+=randomY;break;}case3:{sumX+=randomX;sumY-=randomY;break;}case4:{sumX+=randomX;sumY=0;break;}default:break;}returnfalse;}returntrue;}publicvoidshowDlg(){Stringstr=JOptionPane.showInputDialog("Pleaseenterthenumberofballs");n=Integer.parseInt(str);btn.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEventevent){thread=newThread[n];start=System.currentTimeMillis();for(inti=0;i<n;i++){thread[i]=newThread(newPrintTask());thread[i].start();}}});}publicvoidrun(){}{//frame=newJFrame("DrawingPolygons");//frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);PolygonsJPanelpolygonsJPanel=newPolygonsJPanel();//frame.add(polygonsJPanel);//addpolygonsJPaneltoframe//frame.setSize(1280,650);//setframesize//frame.setVisible(true);//画圆//setCursor(Toolkit.getDefaultToolkit().createCustomCursor(im,newPoint(15,15),"Slef"));executor=Executors.newFixedThreadPool(num);for(inti=0;i<num;i++){Runnableworker=newPrintTask();executor.execute(worker);}polygonsJPanel.repaint();try{Thread.sleep(200);}catch(InterruptedExceptione2){//TODOAuto-generatedcatchblocke2.printStackTrace();}executor.shutdown();//PrintTaskprintTask;n=1;while(!over){//printTask=newPrintTask();//paintComponent(newGraphics());//画圆try//休眠{Thread.sleep(50);}catch(InterruptedExceptionexception){System.out.println("Terminatedprematurelyduetointerruption");}//printTask.repaint();//重画polygonsJPanel.repaint();}//printTask.repaint();}*/classPolygonsJPanelextendsJPanel/*implementsMouseListener,MouseMotionListener*/{RandomrandomNumber;intround,color1,color2,color3;intrandomX,randomY;intstartX,startY;intsumX,sumY;Colorc;MouseEventevent;publicPolygonsJPanel(){randomNumber=newRandom();color1=randomNumber.nextInt(256);color2=randomNumber.nextInt(256);color3=randomNumber.nextInt(256);randomX=randomNumber.nextInt(15);randomY=randomNumber.nextInt(15);//startX=randomNumber.nextInt(1000);//startY=randomNumber.nextInt(600);startX=600;startY=300;sumX=startX;sumY=startY;}publicvoidpaintComponent(Graphicsg){//连续画圆super.paintComponent(g);//callsuperclass'spaintComponentc=newColor(color1,color2,color3);g.setColor(c);if(event.getX()!=sumX&&event.getY()!=sumY&&event.getX()!=1280&&event.getY()!=650){type=0;if(sumX>1280)type=1;if(sumX<0)type=2;if(sumY>650)type=3;if(sumY<0)type=4;switch(type){case0:{sumX+=randomX;sumY+=randomY;break;}case1:{sumX-=randomX;sumY+=randomY;break;}case2:{sumX=0;sumY+=randomY;break;}case3:{sumX+=randomX;sumY-=randomY;break;}case4:{sumX+=randomX;sumY=0;break;}default:break;}while(!over){g.translate(sumX,sumY);g.fillOval(randomX,randomY,30,30);n=1;}//else{end=System.currentTimeMillis();JOptionPane.showMessageDialog(null,"游戏已结束,本次游戏共持续"+(end-start)+",欢迎下次再来!","游戏结束",JOptionPane.INFORMATION_MESSAGE);n=0;}}}}importjava.awt.*;publicclassThreadCreatorextendsJFrame{publicstaticvoidmain(String[]args){PrintTaska=newPrintTask();a.showDlg();}}