You are here:

Java/Create a window in Java with 2 circles ramdomly move in the window

Advertisement


Paul wrote at 2012-12-27 05:17:17
// I spent some time debugging your code.

// hear is what I ended up with



import javax.swing.*;

import java.awt.event.*;

import java.awt.*;



public class TimerTest extends JPanel

{

 public static final int FRAME_HEIGHT = 400;

 public static final int FRAME_WIDTH = 400;

 public static final double ROOT_2_OVER_2 = (Math.sqrt(2)/2.0);

 public static void main(String[] args)

 {

    TimerTest timerTest = new TimerTest();

    JFrame frame = new JFrame(TimerTest.class.getName());

    frame.setContentPane(timerTest);

    frame.setSize(FRAME_HEIGHT,FRAME_WIDTH);

    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    frame.setVisible(true);

    timerTest.start();

 }

 

 private final Timer timer;

 private final int circleSize = 50;

 private int circle1X = 200;

 private int circle1Y = 200;

 private int circle1Width = 20;

 private int circle1Height = 20;

 private boolean circle1Foward = true;

 private int circle2X = 100;

 private int circle2Y = 100;

 private int circle2Width = 20;

 private int circle2Height = 20;

 private boolean circle2Foward = true;

 private Color circle1Color = Color.BLACK;

 private Color circle2Color = Color.BLACK;

 private int line1X1 = 0;

 private int line1Y1 = 0;

 private int line2X1 = 0;

 private int line2Y1 = 0;

 private int line1X2;

 private int line1Y2;

 private int line2X2;

 private int line2Y2;

 public TimerTest()

 {

    timer = new Timer(100, new ActionListener()

    {

       public void actionPerformed(ActionEvent e)

       {

        if(circle1Foward)

        {

        if(circle1X > 350)

        circle1Foward = false;

        else

        circle1X += 1;

        }

        else

        {

        if(circle1X < 50)

        circle1Foward = true;

        else

        circle1X -= 1;

        }

        if(circle1X < 150 || circle1X > 250)

        circle1Color = Color.RED;

        else

        circle1Color = Color.BLACK;

        if(circle2Foward)

        {

        if(circle2Y > 350)

        circle2Foward = false;

        else

        circle2Y += 1;

        }

        else

        {

        if(circle2Y < 50)

        circle1Foward = true;

        else

        circle2Y -= 1;

        }

        if(circle2Y < 150 || circle2Y > 250)

        circle2Color = Color.RED;

        else

        circle2Color = Color.BLACK;

        updateLines();

        repaint();

       }

    });

    timer.setInitialDelay(0);

 }

 private void updateLines()

 {

    line1X2 = (int) (circle1X + circle1Width/2);

    line1Y2 = (int) (circle1Y + circle1Height/2);

    line2X2 = (int) (circle2X + circle2Width/2);

    line2Y2 = (int) (circle2Y + circle2Height/2);

 }

 protected void paintComponent(Graphics g)

 {

    g.clearRect(0, 0, FRAME_WIDTH, FRAME_HEIGHT);

    g.setColor(circle1Color);

    g.drawOval(circle1X,circle1Y,circle1Width,circle1Height);

    g.setColor(circle2Color);

    g.drawOval(circle2X,circle2Y,circle2Width,circle2Height);

    g.setColor(Color.BLACK);

    g.drawLine(line1X1,line1Y1,line1X2,line1Y2);

    g.drawLine(line2X1,line2Y1,line2X2,line2Y2);

 }

 public void start()

 {

    timer.start();

 }

}  


Java

All Answers


Answers by Expert:


Ask Experts

Volunteer


Artemus Harper

Expertise

I have a Masters in computer science. I can answer questions on core J2SE, swing and graphics. Please no questions about JSP or J2ME.

Experience

I have experience in Core Java, good background in Java swing/gui, some experience with JNI, Java reflection. Some experience in bio-informatics. Basics in c++ and c#

Organizations
Washington State University

Education/Credentials
MS in Computer Science from Washington State University and a BS in Mathematics and Computer Science from Central Washington University.

©2016 About.com. All rights reserved.