以前提的问题已经解决,现在就是在sin上animate一个球,怎么样可以让这个球与frame window向切? 请大家帮忙~~
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
public class SineCurve extends JFrame
{
public void lauchFrame()
{
int Row,Col;
Toolkit MyKit = Toolkit.getDefaultToolkit();
Dimension XY = MyKit.getScreenSize();
Row = XY.width;
Col = XY.height;
Image MyImage = MyKit.getImage(":");
this.setIconImage(MyImage);
this.setTitle("JButton-Sun-Rise Color Test");
this.setSize(7*Row/8, 7*Col/8);
this.setLocation(Row/32,Col/32);
this.setTitle("Sine");
this.setResizable(false);
MyPanel Panel01 = new MyPanel();
Container ContentPanel01 = getContentPane();
ContentPanel01.add(Panel01);
this.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
setVisible(true);
}
private class MyPanel extends JPanel
{
public void paint(Graphics g)
{
super.paint;
Graphics2D gr = (Graphics2D) g;
int X, Y, x1, x2, y1, y2;
int Row, Col;
double Frequency;
Dimension RowCol;
RowCol = getSize();
Row = RowCol.width;
Col = RowCol.height;
g.setColor(Color.green);
g.drawRect(1,1,Row-2,Col-2);
g.setColor(Color.blue);
g.drawLine(0,Col/2,Row,Col/2);
g.drawLine(Row/2,0,Row/2,Col);
g.setColor(Color.red);
Frequency = 1;
Radius = 20;
x1 = Action;
y1 = Col/2 - (int)(Col/2*Math.sin(Frequency*2.0*Math.PI/(Row)*x1));
/*if (Action < Row/2)
{
x1 = Action+Radius;
y1 = Col/2+Radius - (int)(Col/2*Math.sin(Frequency*2.0*Math.PI/(Row)*x1));
}*/
g.setColor(new Color(0,150,150));
Ellipse2D C1 = new Ellipse2D.Double();
C1.setFrameFromCenter(x1,y1,x1+Radius,y1+Radius);
gr.fill(C1);
}
public MyPanel()
{
Action = 0;
JButton StartButton = new JButton("Start Running");
add(StartButton);
ClickStart Start = new ClickStart();
StartButton.addActionListener(Start);
}
private class ClickStart implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
ActionListener WakeUp = new TimeStamp();
Timer T = new Timer(10,WakeUp);
T.start();
}
private class TimeStamp implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
Action ++;
repaint();
if (Action == Row-40)
{
Action = 0;
}
}
}
}
private int Action,Frequency,y2,Row,Radius;
}
}