日期:2014-05-20 浏览次数:20743 次
import javax.swing.*; import java.awt.event.*; import java.awt.*; import java.lang.Math; public class TestSin extends JFrame{ private DisplayPanel dp; public void TestSin(){ setLayout(new BorderLayout()); dp=new DisplayPanel(this); getContentPane().add(dp,"Center"); setLocation(200,200); pack(); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public static void main(String []args){ new TestSin(); } class DisplayPanel extends JPanel implements Runnable,MouseListener{ private TestSin ts; Thread th; double c=0.0; double a=100.0; boolean stopIt=false; public DisplayPanel(TestSin tsi){ ts=tsi; setBackground(Color.pink); // setCursor(Cursor.WAIT_CURSOR); 这里为什么不对啊?? setPreferredSize(new Dimension(300,700)); addMouseListener(this); th=new Thread(this); th.start(); } public void paint(Graphics g){ Graphics2D g1=(Graphics2D)g; ts.setTitle("C ="+c); g1.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON); //也可以用super.paint(); g1.clearRect(0,0,getSize().width,getSize().height); g1.setColor(Color.blue); for(int i=0;i<getSize().width;i++){ g1.drawLine(i,(int)(Math.sin(i/a+c)*a+getSize().height/2), i+1,(int)(Math.sin(i+1/a+c)*a+getSize().height/2) ); } } public void run(){ while(!stopIt){ c+=0.1; repaint(); if(c>=2*Math.PI*getSize().width) {c=0.0;} } try{ th.sleep(1000); } catch(InterruptedException ex) {ex.printStackTrace();} } public void mouseClicked(MouseEvent e){} public void mousePressed(MouseEvent e){} public void mouseReleased(MouseEvent e){stopIt=!stopIt;} public void mouseEntered(MouseEvent e){} public void mouseExited(MouseEvent e){} } }
import javax.swing.*; import java.awt.event.*; import java.awt.*; import java.lang.Math; public class TestSin extends JFrame { private DisplayPanel dp; public TestSin() { setLayout(new BorderLayout()); dp = new DisplayPanel(this); getContentPane().add(dp, "Center"); setLocation(200, 200); pack(); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public static void main(String[] args) { new TestSin(); } class DisplayPanel extends JPanel implements Runnable, MouseListener { private TestSin ts; Thread th; double c = 0.0; double a = 100.0; boolean stopIt = false; public DisplayPanel(TestSin tsi) { ts = tsi; setBackground(Color.pink); setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); setPreferredSize(new Dimension(300, 700)); addMouseListener(this); th = new Thread(this); th.start(); } public void paint(Graphics g) { Graphics2D g1 = (Graphics2D) g; ts.setTitle("C =" + c); g1.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); // 也可以用super.paint(); g1.clearRect(0, 0, getSize().width, getSize().height); g1.setColor(Color.blue); for (int i = 0; i < getSize().width; i++) { g1.drawLine(i, (int) (Math.sin(i / a + c) * a + getSize().height / 2), i + 1, (int) (Math.sin(i + 1 / a + c) * a + getSize().height / 2)); } } public void run() { while (!stopIt) { c += 0.1; repaint(); if (c >= 2 * Math.PI * getSize().width) { c = 0.0; } } try { Thread.sleep(1000); } catch (InterruptedException ex) { ex.printStackTrace(); } } public void mouseClicked(MouseEvent e) { } public void mousePressed(MouseEvent e) { } public void mouseReleased(MouseEvent e) { stopIt = !stopIt; } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } } }