gpt4 book ai didi

java - Swing - drawString 移动动画失败

转载 作者:太空宇宙 更新时间:2023-11-04 06:11:40 25 4
gpt4 key购买 nike

我正在尝试在Java swing上制作一个小动画,这应该将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 方法。

此外,您还需要在 animate 方法中调用 super.paintComponent(page); (如 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();

}
}

简介面板:

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);
}
}

关于java - Swing - drawString 移动动画失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28652742/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com