Posted by : Unknown
Thursday, 18 June 2015
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class InfiniteMotion extends JPanel implements ActionListener
import java.awt.event.*;
import javax.swing.*;
public class InfiniteMotion extends JPanel implements ActionListener
{
public void drawFrame(Graphics g, int frameNumber, int width, int height)
public void drawFrame(Graphics g, int frameNumber, int width, int height)
{
int inset;
int rectWidth, rectHeight;
g.setColor(Color.LIGHT_GRAY);
g.fillRect(0,0,width,height);
g.setColor(Color.BLACK);
inset = frameNumber % 15;
rectWidth = width - 2*inset;
rectHeight = height - 2*inset;
while (rectWidth >= 0 && rectHeight >= 0)
int inset;
int rectWidth, rectHeight;
g.setColor(Color.LIGHT_GRAY);
g.fillRect(0,0,width,height);
g.setColor(Color.BLACK);
inset = frameNumber % 15;
rectWidth = width - 2*inset;
rectHeight = height - 2*inset;
while (rectWidth >= 0 && rectHeight >= 0)
{
g.drawRect(inset, inset, rectWidth, rectHeight);
inset += 15;
rectWidth -= 30;
rectHeight -= 30;
}
}
public static void main(String[] args)
g.drawRect(inset, inset, rectWidth, rectHeight);
inset += 15;
rectWidth -= 30;
rectHeight -= 30;
}
}
public static void main(String[] args)
{
JFrame window = new JFrame("Infinite motion");
InfiniteMotion drawingArea = new InfiniteMotion();
drawingArea.setBackground(Color.WHITE);
window.setContentPane(drawingArea);
drawingArea.setPreferredSize(new Dimension(600,450));
window.pack();
window.setLocation(100,50);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setResizable(true);
Timer frameTimer = new Timer(20,drawingArea);
window.setVisible(true);
frameTimer.start();
}
private int frameNum;
public void actionPerformed(ActionEvent evt)
JFrame window = new JFrame("Infinite motion");
InfiniteMotion drawingArea = new InfiniteMotion();
drawingArea.setBackground(Color.WHITE);
window.setContentPane(drawingArea);
drawingArea.setPreferredSize(new Dimension(600,450));
window.pack();
window.setLocation(100,50);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setResizable(true);
Timer frameTimer = new Timer(20,drawingArea);
window.setVisible(true);
frameTimer.start();
}
private int frameNum;
public void actionPerformed(ActionEvent evt)
{
frameNum++;
repaint();
}
protected void paintComponent(Graphics g)
frameNum++;
repaint();
}
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
drawFrame(g, frameNum, getWidth(), getHeight());
}
}
Output:
Related Posts :
- Back to Home »
- Simple Application Program »
- Infinite Motion