该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
import java.awt.EventQueue;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class DD implements ActionListener{
private JFrame frame;
private javax.swing.Timer timer =new javax.swing.Timer(50,this);
private MyPanel20 panel ;
int count;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
DD window = new DD();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public DD() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 419, 302);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel = new MyPanel20();
frame.getContentPane().add(panel, BorderLayout.CENTER);
timer.start();
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
panel.arc1 = panel.arc1 - 5*2*Math.PI/360;
//逆时针转
panel.repaint();
}
class MyPanel20 extends JPanel{
double arc1 = 300*2*Math.PI/360;
protected void paintComponent(Graphics g){
super.paintComponent(g);
int x1 = this.getWidth()/2;
int r1 = x1-15;
g.fillOval(x1-2, 20, 4, 4);
g.drawLine(x1, 20, (int)(x1-x1*Math.cos(arc1)), (int)(20-x1*Math.sin(arc1)));
g.fillOval((int)(r1-x1*Math.cos(arc1)), (int)(10-x1*Math.sin(arc1)), 30, 30);
}
}
}
只能逆时针转,如何才能来回摆动?求点拨