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

  • Home
  • SEARCH
  • 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 8874497
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T18:44:14+00:00 2026-06-14T18:44:14+00:00

I wrote program to draw wheel (circle with 6 segments) and each segment with

  • 0

I wrote program to draw wheel (circle with 6 segments) and each segment with different colour. and animate the wheel..

here is the code:

    public class ExamWheel extends JFrame implements ActionListener{
        JButton b_start = new JButton("Start");
        JButton b_stop = new JButton("Stop");
        Thread th;
        Boolean doDraw = true;

        public ExamWheel(){
            setSize(400,400);
            setVisible(true);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setLocationRelativeTo(null);
            setTitle("Wheel..");
            //add(b_start);
            this.setLayout (new FlowLayout());
            this.add(b_stop);
            b_start.addActionListener(this);
        }

        public void actionPerformed(ActionEvent e) {
            if(e.getSource()==b_start)
                doDraw=true;
        }

        public void paint(Graphics graphics) {
        if (doDraw){
            super.paint(graphics);
            Graphics2D g = (Graphics2D) graphics;
            g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

            try{
            // draw the circle
            for(int i=0; ; i=i+1){
                g.setColor(Color.CYAN);
                g.fillArc(50, 50, 300, 300, i+0, 60);
                th.sleep(1);
                g.setColor(Color.red);
                g.fillArc(50, 50, 300, 300, i+60, 60);
                th.sleep(1);
                g.setColor(Color.green);
                g.fillArc(50, 50, 300, 300, i+120, 60);
                th.sleep(1);
                g.setColor(Color.blue);
                g.fillArc(50, 50, 300, 300, i+180, 60);
                th.sleep(1);
                g.setColor(Color.gray);
                g.fillArc(50, 50, 300, 300, i+240, 60);
                th.sleep(1);
                g.setColor(Color.pink);
                g.fillArc(50, 50, 300, 300, i+300, 60);
                th.sleep(1);
            }
            }
            catch(InterruptedException e){
                Thread.currentThread().interrupt();
            }
        }
        }

        public static void main(String[] args) {
            ExamWheel f = new ExamWheel();
        }

    }

Problems: it is infinite loop, and I can’t stop it or close the Frame.

so I had idea:

I created Boolean variable (doDraw) with value true, and add JButton, when click the Button the variable will change to false, and in paint() method I’ll use if condition at first of paint()

problem: I can’t add JButton to the Frame with paint(), so what can I do?

Note: I tried to use paintComponent() but the loop (for with thread) doesn’t work.

IT IS SOLVED
Thanks for Pete Kirham

I added Timer and replaced paint() by paintComponent()

public class ExamWheel extends JPanel implements ActionListener {
    int i=0;
    Timer tm = new Timer(10, this);
    public void paintComponent(Graphics graphics) {
        super.paintComponent(graphics);
        Graphics2D g = (Graphics2D) graphics;
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        g.setColor(Color.CYAN);
        g.fillArc(50, 50, 300, 300, i+0, 60);
        g.setColor(Color.red);
        g.fillArc(50, 50, 300, 300, i+60, 60);
        g.setColor(Color.green);
        g.fillArc(50, 50, 300, 300, i+120, 60);
        g.setColor(Color.blue);
        g.fillArc(50, 50, 300, 300, i+180, 60);
        g.setColor(Color.gray);
        g.fillArc(50, 50, 300, 300, i+240, 60);
        g.setColor(Color.pink);
        g.fillArc(50, 50, 300, 300, i+300, 60);
        tm.start();
    }
    public void actionPerformed(ActionEvent e) {
        i++;
        repaint();
    }
    public static void main(String[] args) {
        ExamWheel wh = new ExamWheel();
        JFrame jf = new JFrame();
        jf.setSize(500,500);
        jf.setResizable(false);
        jf.setVisible(true);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.setLocationRelativeTo(null);
        jf.setTitle("Wheel..");
        jf.add(wh);
    }
  • 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-06-14T18:44:15+00:00Added an answer on June 14, 2026 at 6:44 pm

    You need to return from paintComponent to allow the thread which runs the gui to do other stuff, like respond to button events or actually put the contents of the graphics onto the screen.

    Use a timer to invalidate the component – see http://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html and update the the animation based on the current time, for example current milliseconds modulo 5000 will give a cycle which repeats every five seconds.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote a little program to draw a pattern a cycle through different color
I wrote a program in C++/OpenGL (using Dev-C++ compiler) for my calculus 2 class.
I wrote a simple program, here's what it looks like, some details hidden: using
I am using the windows phone emulator. I wrote a very simple program: draw
i wrote program to connect oracle database 11g for my android application to store
I wrote a program for downloading an image from web using AsyncTask in service
I wrote a program called Hello.py that looks like this: import pygame, sys from
I wrote a program to multiply, divide, add, and subtract fractions..I just can't figure
I wrote this program: #include <stdio.h> /*Part B Write a program that: defines an
I wrote a program that forks some processes with fork(). I want to kill

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.