问题描述
模拟绘制一转动中的小风扇在窗口中绘制具有4个叶片的风扇;用Timer或多线程程序设计实现叶片的转动;设置启动、停止按钮,定义一个滚动条控制叶片的转速。各位大侠帮我完善和优化一下下面的代码,实现一下加速的真实模拟和浮动文字的多线程方法!请大家多多指教~packageACM;importjavax.swing.*;importjava.awt.*;importjava.awt.event.*;publicclassDrawArcs22extendsJFrameimplementsActionListener{publicArcsPanelarc;publicJButtonstart,end;publicJSliderslider;publicJPanelbtnPanel;publicvoidInitBtnPanel(){start=newJButton("开始");start.addActionListener(this);end=newJButton("暂停");end.addActionListener(this);slider=newJSlider(0,100,10);btnPanel=newJPanel();btnPanel.setBorder(BorderFactory.createTitledBorder("控制操作:"));btnPanel.setLayout(newGridLayout(1,3));btnPanel.add(start);btnPanel.add(end);btnPanel.add(slider);}publicDrawArcs22(){setTitle("DrawArcs");//Timert=newTimer(1000,arc);arc=newArcsPanel(20);this.InitBtnPanel();//arc.Start();getContentPane().add(arc);this.add(btnPanel,BorderLayout.NORTH);}publicstaticvoidmain(String[]args){DrawArcs22frame=newDrawArcs22();frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//frame.arc.Start();frame.setSize(250,300);frame.setVisible(true);}publicvoidactionPerformed(ActionEvente){//TODOAuto-generatedmethodstubif(e.getSource()==start){this.arc.rotate=true;Threadt=newThread(this.arc);t.start();}else{//if(t!=null)this.arc.rotate=false;}}}classArcsPanelextendsJPanelimplementsRunnable{publicintclockwise=-1;publicintstart=0;publicbooleanrotate=true;publicArcsPanel(intstart){this.start=start;//t=newTimer(1000,this);//this.t=t;}protectedvoidpaintComponent(Graphicsg){super.paintComponent(g);g.setColor(Color.green);intxCenter=getWidth()/2;intyCenter=getHeight()/2;intradius=(int)(Math.min(getWidth(),getHeight())*0.4);intx=xCenter-radius;inty=yCenter-radius;g.setColor(Color.BLUE);g.fillArc(x,y,2*radius,2*radius,this.start%360,30*this.clockwise);g.setColor(Color.ORANGE);g.fillArc(x,y,2*radius,2*radius,(this.start+90)%360,30*this.clockwise);g.setColor(Color.RED);g.fillArc(x,y,2*radius,2*radius,(this.start+180)%360,30*this.clockwise);g.setColor(Color.GREEN);g.fillArc(x,y,2*radius,2*radius,(this.start+270)%360,30*this.clockwise);}publicvoidrun(){//TODOAuto-generatedmethodstubwhile(rotate){try{Thread.sleep(59);}catch(Exceptione){e.printStackTrace();}this.start+=30;this.start%=360;this.repaint();}}}
解决方案
解决方案二:
各位大侠帮我完善和优化一下下面的代码,实现一下加速的真实模拟和浮动文字的多线程方法!请大家多多指教~importjavax.swing.*;importjava.awt.*;importjava.awt.event.*;publicclassDrawArcs22extendsJFrameimplementsActionListener{publicArcsPanelarc;publicJButtonstart,end;publicJSliderslider;publicJPanelbtnPanel;publicvoidInitBtnPanel(){start=newJButton("开始");start.addActionListener(this);end=newJButton("暂停");end.addActionListener(this);slider=newJSlider(0,100,10);btnPanel=newJPanel();btnPanel.setBorder(BorderFactory.createTitledBorder("控制操作:"));btnPanel.setLayout(newGridLayout(2,7));btnPanel.add(start);btnPanel.add(end);btnPanel.add(slider);}publicDrawArcs22(){//Timert=newTimer(1000,arc);arc=newArcsPanel(20);this.InitBtnPanel();//arc.Start();getContentPane().add(arc);this.add(btnPanel,BorderLayout.NORTH);}publicstaticvoidmain(String[]args){DrawArcs22frame=newDrawArcs22();frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//frame.arc.Start();frame.setSize(350,400);frame.setLocation(550,150);frame.setTitle("模拟加速小风扇");frame.setVisible(true);}publicvoidactionPerformed(ActionEvente){//TODOAuto-generatedmethodstubif(e.getSource()==start){this.arc.rotate=true;Threadt=newThread(this.arc);t.start();}else{//if(t!=null)this.arc.rotate=false;}}}classArcsPanelextendsJPanelimplementsRunnable{publicintclockwise=-1;publicintstart=0;publicbooleanrotate=true;publicArcsPanel(intstart){this.start=start;//t=newTimer(1000,this);//this.t=t;}protectedvoidpaintComponent(Graphicsg){super.paintComponent(g);g.setColor(Color.green);intxCenter=getWidth()/2;intyCenter=getHeight()/2;intradius=(int)(Math.min(getWidth(),getHeight())*0.4);intx=xCenter-radius;inty=yCenter-radius;g.setColor(Color.BLUE);g.fillArc(x,y,2*radius,2*radius,this.start%360,30*this.clockwise);g.setColor(Color.ORANGE);g.fillArc(x,y,2*radius,2*radius,(this.start+90)%360,30*this.clockwise);g.setColor(Color.RED);g.fillArc(x,y,2*radius,2*radius,(this.start+180)%360,30*this.clockwise);g.setColor(Color.GREEN);g.fillArc(x,y,2*radius,2*radius,(this.start+270)%360,30*this.clockwise);}publicvoidrun(){//TODOAuto-generatedmethodstubwhile(rotate){try{Thread.sleep(59);}catch(Exceptione){e.printStackTrace();}this.start+=30;this.start%=360;this.repaint();}}}