📄 j_flower.java
字号:
// ////////////////////////////////////////////////////////
//
// J_Flower.java
//
// ////////////////////////////////////////////////////////
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.awt.image.*;
import javax.swing.*;
public class J_Flower extends JApplet implements ActionListener
{
int m_frame = -1; // The number of the current frame in the animation.
Timer m_timer; // The timer controlling the animation.
boolean m_frozen = false; // Status of stopping animation.
boolean m_ready = true; // Status of the next frame is ready.
BufferedImage m_image= new BufferedImage(320, 320, BufferedImage.TYPE_INT_RGB );
public void init( )
{
int delay = 25;
mb_draw( );
m_timer = new Timer(delay, this);
m_timer.setInitialDelay(0);
m_timer.setCoalesce(true);
getContentPane( ).addMouseListener(new MouseAdapter( )
{
public void mousePressed(MouseEvent e)
{
m_frozen = !m_frozen;
if (m_frozen)
mb_stopAnimation( );
else mb_startAnimation( );
}
});
} // End of method: init
public void start( )
{
mb_startAnimation( );
} // End of method: start
public void stop( )
{
mb_stopAnimation( );
} // End of method: stop
public void actionPerformed(ActionEvent e)
{
m_frame++; //Advance the animation frame.
//Request that the frame be painted.
repaint( );
} // End of method: actionPerformed
public synchronized void mb_startAnimation( )
{
if (!m_frozen && !m_timer.isRunning( ))
m_timer.start( );
} // End of method: mb_startAnimation
public synchronized void mb_stopAnimation( )
{
if (m_timer.isRunning( ))
m_timer.stop( );
} // End of method: mb_stopAnimation
public void mb_draw( )
{
if (!m_ready)
return;
m_ready=false;
Graphics2D g2d = m_image.createGraphics( );
int i= (m_frame>0 ? m_frame%600 : (-m_frame)%600);
double a= (i>300 ? 600-i : i);
double b= a*6/16;
double a_2= a/2;
double b_2= b/2;
// Draw grass
g2d.setPaint(new GradientPaint(0, 0, new Color(187,255,204), 0,300, Color.green, true));
g2d.fill( new Rectangle2D.Double( 0, 0, 320, 300));
// Draw flower
g2d.setColor( Color.magenta );
g2d.fill( new Ellipse2D.Double( 160-b_2, 150-a_2, b, a));
g2d.fill( new Ellipse2D.Double( 160-a_2, 150-b_2, a, b ));
g2d.setColor( Color.orange );
g2d.fill( new Ellipse2D.Double( 160-b_2, 150-b_2, b, b ));
// Draw Frame number
g2d.setPaint(Color.white);
g2d.fill( new Rectangle2D.Double( 0, 300, 320, 20 ) );
g2d.setColor(Color.black);
g2d.drawString(""+m_frame, 150, 315);
m_ready=true;
} // End of method: mb_draw
public void paint(Graphics g)
{
if (m_ready)
g.drawImage(m_image, 0, 0, 320, 320, this);
mb_draw( );
} // End of method: paint
} // End of class: J_Flower
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -