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 8429313
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T05:18:04+00:00 2026-06-10T05:18:04+00:00

I am writing a simple game in java using Ovals and the Graphics objects.

  • 0

I am writing a simple game in java using Ovals and the Graphics objects. It is called virus and works like this: There is an oval in the middle, and six ovals around the outside. These outside ovals are supposed to increase in size until clicked on, when they will disappear and the player scores ten points. If an oval touches the central oval, the health of the centre oval goes down. When it hits zero, the game ends. The problem im having is that the outside ovals will not increase in size. Why is this happening?

Here is my code:

package virus;


import java.awt.*;
import java.util.Random;
import javax.swing.JPanel;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class VirusGamePanel extends JPanel implements MouseListener{
    private static final long serialVersionUID = 1L;//serialVersionUID field
    Random colour = new Random();//the outside ovals will always be a random colour
    private int sizeX = 1;//the x size of the outside ovals 
    private int sizeY = 1;//the y size of the outside ovals
    int score = 0;
    static String scorestring = "Score: ";
    Color rand = new Color(colour.nextInt(255),colour.nextInt(255),colour.nextInt(255));//generate the random colour

    public void paint(Graphics g)
    {
        super.paint(g);
        g.setColor(Color.magenta);
        g.drawString(scorestring+score,275,250);
        g.setColor(Color.orange);
        g.drawOval(200,150,200,200);
        g.setColor(rand);
        g.drawOval(270,50,50,50);
        g.drawOval(100,100,50,50);
        g.drawOval(450,100,50,50);
        g.drawOval(100,400,50,50);
        g.drawOval(450,400,50,50);
        g.drawOval(275,450,50,50);
        g.fillOval(270,50,sizeX,sizeY);//these six ovals are supposed to increase in size
        g.fillOval(100,100,sizeX,sizeY);
        g.fillOval(450,100,sizeX,sizeY);
        g.fillOval(100,400,sizeX,sizeY);
        g.fillOval(450,400,sizeX,sizeY);
        g.fillOval(275,450,sizeX,sizeY);
        inc();
    }

    public static void main(String[] args) {
      JPanel panel = new VirusGamePanel();

      JFrame frame = new JFrame("Virus");
      frame.setSize(700, 700);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(panel);
      frame.setVisible(true);
    }

    private void inc()//increase the size of the ovals
    {
        for(int i = 0; i<25000; i++)
        {
            sizeX++;
            sizeY++;
            repaint();
        }
    }

New code with thread:

package virus;


import java.awt.*;
import java.util.Random;
import javax.swing.JPanel;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class VirusGamePanel extends JPanel implements MouseListener,Runnable{
    private static final long serialVersionUID = 1L;
    Random colour = new Random();
    private int sizeX = 1;
    private int sizeY = 1;
    int score = 0;
    Thread thr = new Thread();
    static String scorestring = "Score: ";
    Color rand = new Color(colour.nextInt(255),colour.nextInt(255),colour.nextInt(255));

    public void paint(Graphics g)
    {
        super.paint(g);
        thr.start();
        g.setColor(Color.magenta);
        g.drawString(scorestring+score,275,250);
        g.setColor(Color.orange);
        g.drawOval(200,150,200,200);
        g.setColor(rand);

        g.fillOval(270,50,sizeX,sizeY);
        g.fillOval(100,100,sizeX,sizeY);
        g.fillOval(450,100,sizeX,sizeY);
        g.fillOval(100,400,sizeX,sizeY);
        g.fillOval(450,400,sizeX,sizeY);
        g.fillOval(275,450,sizeX,sizeY);
        inc();
    }

    public static void main(String[] args) {}

    private void inc()
    {
        thr.run();
    }
    public void run(){
        for(int i = 0; i<25000; i++)
        {
            sizeX++;
            sizeY++;
            repaint();
            try
            {
                Thread.sleep(10);
            }
            catch(InterruptedException e)
            {
                e.printStackTrace();
            }
        }
    }
  • 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-10T05:18:06+00:00Added an answer on June 10, 2026 at 5:18 am

    This works for me:

    public class VirusGamePanel extends JPanel{
        private static final long serialVersionUID = 1L;//serialVersionUID field
        Random colour = new Random();//the outside ovals will always be a random colour
        private int sizeX = 0;//the x size of the outside ovals 
        private int sizeY = 0;//the y size of the outside ovals
        int score = 0;
        static String scorestring = "Score: ";
        Color rand = new Color(colour.nextInt(255), colour.nextInt(255), colour.nextInt(255)); //generate the random colour
    
        public void paint(Graphics g)
        {
            super.paint(g);
            g.setColor(Color.magenta);
            g.drawString(scorestring+score,275,250);
            g.setColor(Color.orange);
            g.drawOval(200, 150, 200, 200);
            g.setColor(rand);
            g.fillOval(270 - sizeX / 2, 50 - sizeY / 2, sizeX, sizeY);//these six ovals are supposed to increase in size
            g.fillOval(100 - sizeX / 2,100 - sizeY / 2, sizeX, sizeY);
            g.fillOval(450 - sizeX / 2,100 - sizeY / 2, sizeX, sizeY);
            g.fillOval(100 - sizeX / 2,400 - sizeY / 2, sizeX, sizeY);
            g.fillOval(450 - sizeX / 2,400 - sizeY / 2, sizeX, sizeY);
            g.fillOval(275 - sizeX / 2,450 - sizeY / 2, sizeX, sizeY);
            inc();
        }
    
        public static void main(String[] args) {
          JPanel panel = new VirusGamePanel();
    
          JFrame frame = new JFrame("Virus");
          frame.setSize(700, 700);
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.getContentPane().add(panel);
          frame.setVisible(true);
        }
    
        private void inc()//increase the size of the ovals
        {
                sizeX++;
                sizeY++;
                repaint();
        }
    }
    

    Just fixed the center points of the ovals and removed the loop in the inc method. If you want the oval boundaries drawn, just add the drawOval commands with the same parameters as the fillOval commands.

    EDIT:

    If you want to slow down the growth process, just add the following just before the inc() call of the paint method:

    try {
        Thread.sleep(100);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing simple game on flex. In the game there are 2 objects
I'm writing a very simple Java Game. Let me describe it briefly: There are
I'm writing a simple game in Java and are trying to get the coordinates
greetngs, i am trying to learn Java and Swing by writing a simple game
In a simple RMI game I'm writing (an assignment in uni), I reveice: java.rmi.MarshalException:
I'm tinkering with writing a simple text-based role-playing game. I would like to use
I am writing a simple java console game. I use the scanner to read
I'm writing a text game and I need a simple combat system, like in
I'm writing a simple card game in JavaScript, and I'd like to have a
I'm currently writing a very simple game using python and pygame. It has stuff

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.