我刚刚开始学习GUI和Swing,并决定编写一个我在教科书中找到的程序来显示一个彩色矩形,并使其看起来像是在缩小尺寸。屏幕。
下面是我编写的代码,我的问题是矩形显示在屏幕上,但不会每隔50ms重新绘制一个较小的尺寸。任何人都可以指出我在哪里出错吗?
非常感谢
import javax.swing.*;
import java.awt.*;
public class Animate {
int x = 1;
int y = 1;
public static void main(String[] args){
Animate gui = new Animate();
gui.start();
}
public void start(){
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Rectangles rectangles = new Rectangles();
frame.getContentPane().add(rectangles);
frame.setSize(500,270);
frame.setVisible(true);
for(int i = 0; i < 100; i++,y++,x++){
x++;
rectangles.repaint();
try {
Thread.sleep(50);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
class Rectangles extends JPanel {
public void paintComponent(Graphics g){
g.setColor(Color.blue);
g.fillRect(x, y, 500-x*2, 250-y*2);
}
}
}
答案 0 :(得分:0)
// Cities Seeding
foreach (var uf in Enum.GetValues(typeof(Uf)))
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("http://servicos.cptec.inpe.br/~rserv/estados/cidade-" + uf.ToString() + ".xml");
int cityId = Convert.ToInt32(xmlDoc.SelectSingleNode("/climas/clima/estados/estado/cidades/cidade").Attributes["id"].Value);
string cityName = xmlDoc.SelectSingleNode("/climas/clima/estados/estado/cidades/cidade").Attributes["nome"].Value.ToString();
bool capital = Convert.ToBoolean(xmlDoc.SelectSingleNode("/climas/clima/estados/estado/cidades/cidade").Attributes["capital"].Value);
int stateId = 1;
//if (!capital)
//{
context.Cities.AddOrUpdate(
c => c.CityId,
new City
{
CityId = cityId,
CityName = cityName,
StateId = stateId
});
//}
//else
//{
//context.Capitals.AddOrUpdate(
// cp => cp.CityId,
// new Capital
// {
// CityId = cityId,
// CityName = cityName,
// StateId = stateId
// });
//}
stateId++;
}
(或你的图形所在的JPanel)。JFrame
两次,而是x++
将您的代码更改为此(有效,我还修复了缩进):
x+=2