Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6386919
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T03:05:15+00:00 2026-05-25T03:05:15+00:00

I want to creat a circle in on a panel which appears and dissapears

  • 0

I want to creat a circle in on a panel which appears and dissapears every 2 seconds.
Here is that i have:

public class Board extends JPanel implements ActionListener {

    private final int DELAY = 2000;

    private Timer timer;

    /*
     * constructor
     */
    public Board() {

        setFocusable(true);
        initGame();
    }

    /*
     * initialize board
     */
    public void initGame() {

        timer = new Timer(DELAY, this);
        timer.start();

    }

    public void paint(Graphics g) {

        super.paint(g);
        g.setColor(Color.gray);
        // draw an oval starting at 20,20 with a width and height of 100 and
        // fill it
        g.drawOval(20, 20, 100, 100);
        g.fillOval(20, 20, 100, 100);

        g.dispose();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        repaint();
    }
}

thank you guys. Now I wanna control my circle. But again something is wrong and it is not moving as I want.

here are new methods:

    private boolean left = false;
private boolean right = true;
private boolean up = false;
private boolean down = false;

private Timer timer;

public int x = 20;
public int y = 20;
public int x2 = 100;
public int y2 = 100;

…

    public void paint(Graphics g) {

    super.paint(g);
    if (drawCircle) {
        g.setColor(Color.gray);
        // draw an oval starting at 20,20 with a width and height of 100 and
        // fill it
        g.drawOval(x, y, x2, y2);
        g.fillOval(x, y, x2, y2);
    }
    // removes native non-Java recourses
    g.dispose();
}

public void move() {

    if (left) {
        x -= 5;
    }
    if (right) {
        x += 5;
    }
    if (up) {
        y -= 5;
    }
    if (down) {
        y += 5;
    }
}

@Override
public void actionPerformed(ActionEvent e) {

    move();
    repaint();
}

private class TAdapter extends KeyAdapter {

    public void keyPressed(KeyEvent e) {

        int key = e.getKeyCode();

        if ((key == KeyEvent.VK_LEFT) && (!right)) {
            left = true;
            up = false;
            down = false;
        }

        if ((key == KeyEvent.VK_RIGHT) && (!left)) {
            right = true;
            up = false;
            down = false;
        }

        if ((key == KeyEvent.VK_UP) && (!down)) {
            up = true;
            right = false;
            left = false;
        }

        if ((key == KeyEvent.VK_DOWN) && (!up)) {
            down = true;
            right = false;
            left = false;
        }
    }
}

/****/
Solved , i just added

addKeyListener(new TAdapter());

to my Board constructor!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T03:05:16+00:00Added an answer on May 25, 2026 at 3:05 am

    You just need to have your Timer modify some state. Try something like this:

    private boolean drawCircle = false;
    
    public void actionPerformed(ActionEvent e) {
        drawCircle = !drawCircle;
        repaint();
    }
    
    public void paintComponent(Graphics g) {
        //...
        if ( drawCircle ) {
           g.setColor(Color.gray);
           //...
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Contact class that contains a PortalAccount object. When I want to
I have a thread which drops a circle in the y direction. I want
I'm programming an Android game using AndEngine. I want to create a circle which
I want to get a list of all available datasets (that I'd creat in
I want to create a circle that is sectioned into 4 quarters, and each
Hey guys i have a question here, How do i create a body that
so I have some code that draws a path and a circle. the circle
I want to create custom button and I need it to be circle. How
I am working on a Wordpress-Design and i want to creat a Custom Menu.
I'm trying to creat special event for jQuery. I want to make controled delay,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.