package four.work.course;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
// import java.util.Random;
public class MovingShapes {
protected JFrame frameCW4 = new JFrame("CourseWork Four");
private FirstDrawPanel firstDrawPanel = new FirstDrawPanel();
private SecondDrawPanel secondDrawPanel = new SecondDrawPanel();
private int xCoord = 0, yCoord = 0;
private int counter;
final private byte LOOPBACK = 0;
protected boolean status = true;
// private Random generator = new Random();
protected void setUpComponents()
{
frameCW4.setVisible(true);
frameCW4.setSize(500,500);
frameCW4.getContentPane().add(BorderLayout.CENTER, firstDrawPanel);
frameCW4.getContentPane().add(BorderLayout.NORTH, secondDrawPanel);
frameCW4.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
protected void animationManager(String direction)
{
String buffer;
buffer = direction.toString().trim();
if (buffer.equalsIgnoreCase("PartA"))
{
for ( counter = 0; counter < frameCW4.getWidth(); counter++ )
{
for ( counter = 0; counter < frameCW4.getHeight(); counter ++)
{
{
// xCoord = generator.nextInt(frameCW4.getWidth());
// yCoord = generator.nextInt(frameCW4.getHeight());
xCoord++;
yCoord++;
try
{
Thread.sleep(5);
firstDrawPanel.repaint();
secondDrawPanel.repaint();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
}
}
}
protected void retrace(boolean check)
{
while (status == true)
{
for (counter = LOOPBACK; counter <= frameCW4.getWidth(); counter++ )
{
for (counter = LOOPBACK; counter <= frameCW4.getHeight(); counter++)
{
{
// System.out.println(counter);
// xCoord = generator.nextInt(frameCW4.getWidth());
// yCoord = generator.nextInt(frameCW4.getHeight());
xCoord++;
yCoord++;
try
{
Thread.sleep(5);
firstDrawPanel.repaint();
secondDrawPanel.repaint();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
}
for ( counter = frameCW4.getWidth(); counter >= LOOPBACK; counter--)
{
for (counter = frameCW4.getHeight(); counter >= LOOPBACK; counter --)
{
xCoord--;
yCoord--;
try
{
Thread.sleep(5);
firstDrawPanel.repaint();
secondDrawPanel.repaint();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
}
}
class FirstDrawPanel extends JPanel
{
public void paintComponent(Graphics g)
{
g.setColor(Color.WHITE);
g.fillRect(0, 0, this.getWidth(), this.getHeight());
g.setColor(Color.BLUE);
g.fillOval(xCoord, yCoord, 50, 50);
}
}
class SecondDrawPanel extends JPanel
{
public void paintComponent(Graphics g)
{
g.setColor(Color.WHITE);
g.fillRect(0, 0, this.getWidth(), this.getHeight());
g.setColor(Color.ORANGE);
g.fillRect(xCoord, 0, 50, 50);
}
}
public static void main(String[] args)
{
MovingShapes ms = new MovingShapes();
ms.setUpComponents();
ms.animationManager("PartA");
}
}