[英]Swing - drawString shifting animation fails
我正在嘗試在Java擺動上制作一些動畫,這應該將drawString的字符串稍微向左移動(居中),但是,每當我嘗試執行該操作時,程序都會緩慢打開,並跳至該位置最終應該降落,所以看似沒有動畫發生。
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.border.Border;
public class IntroPanel extends JPanel {
private int x = 300;
private JButton exitButton, startButton;
private JPanel buttonsPanel;
public IntroPanel()
{
setPreferredSize( new Dimension( 300, 150));
setLayout( new BorderLayout());
buttonsPanel = new JPanel();
exitButton = new JButton( "Exit" );
startButton = new JButton( "Start" );
exitButton.addActionListener( new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
startButton.addActionListener( new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
/**
* TODO GOES TO MAIN PANEL
*/
}
});
setBackground( new Color( 250, 250, 250) );
startButton.setPreferredSize( new Dimension(70, 40) );
exitButton.setPreferredSize( new Dimension(70, 40) );
Border padding = BorderFactory.createEmptyBorder(0, 0, 25, 0);
setBorder(padding);
buttonsPanel.setBackground( new Color( 250, 250, 250));
buttonsPanel.add(startButton);
buttonsPanel.add(exitButton);
add(buttonsPanel, BorderLayout.SOUTH);
animate();
}
public void animate()
{
for( int i = 1; i < 211; i++ )
{
x--;
repaint();
try
{
Thread.sleep(10);
}
catch(InterruptedException ex) { }
}
}
public void paintComponent( Graphics page )
{
Graphics2D gra = (Graphics2D) page;
gra.setFont( new Font( "Philosopher-BoldItalic", Font.ITALIC | Font.BOLD, 50 ) );
Color start = new Color( 50, 50, 50 );
Color end = new Color( 250, 250, 250 );
GradientPaint gradient =
new GradientPaint( 120, 100, start, 270, 270, end);
gra.setRenderingHint( RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
gra.setPaint( gradient );
gra.drawString("Geometrica", x, 100);
}
}
主要方法:
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame( "Geometrica" );
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(420, 250);
frame.setBackground( new Color(250, 250, 250) );
frame.setLocationRelativeTo(null);
frame.getContentPane().add( new IntroPanel() );
frame.setVisible(true);
}
}
謝謝!
使框架從對象外部可見后,調用animate方法。
您還需要調用super.paintComponent(page);
在您的animate
方法中(如Javadoc中所述),您將獲得所需的結果。 主要:
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame( "Geometrica" );
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(420, 250);
frame.setBackground( new Color(250, 250, 250) );
frame.setLocationRelativeTo(null);
IntroPanel p=new IntroPanel();
frame.getContentPane().add(p);
frame.setVisible(true);
p.animate();
}
}
IntroPanel:
public class IntroPanel extends JPanel {
private int x = 300;
private JButton exitButton, startButton;
private JPanel buttonsPanel;
public IntroPanel()
{
setPreferredSize( new Dimension( 300, 150));
setLayout( new BorderLayout());
buttonsPanel = new JPanel();
exitButton = new JButton( "Exit" );
startButton = new JButton( "Start" );
exitButton.addActionListener( new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
startButton.addActionListener( new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
/**
* TODO GOES TO MAIN PANEL
*/
}
});
setBackground( new Color( 250, 250, 250) );
startButton.setPreferredSize( new Dimension(70, 40) );
exitButton.setPreferredSize( new Dimension(70, 40) );
Border padding = BorderFactory.createEmptyBorder(0, 0, 25, 0);
setBorder(padding);
buttonsPanel.setBackground( new Color( 250, 250, 250));
buttonsPanel.add(startButton);
buttonsPanel.add(exitButton);
add(buttonsPanel, BorderLayout.SOUTH);
}
public void animate()
{
super.paintComponent(page);
for( int i = 1; i < 211; i++ )
{
x--;
repaint();
try
{
Thread.sleep(10);
}
catch(InterruptedException ex) { }
}
}
public void paintComponent( Graphics page )
{
Graphics2D gra = (Graphics2D) page;
gra.setFont( new Font( "Philosopher-BoldItalic", Font.ITALIC | Font.BOLD, 50 ) );
Color start = new Color( 50, 50, 50 );
Color end = new Color( 250, 250, 250 );
GradientPaint gradient =
new GradientPaint( 120, 100, start, 270, 270, end);
gra.setRenderingHint( RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
gra.setPaint( gradient );
gra.drawString("Geometrica", x, 100);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.