Question

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();
            }
        }
    }
Was it helpful?

Solution

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();
}

OTHER TIPS

Your code is recursive. In inc(), you call repaint, which calls VirusGamePanel.paint(), which calls inc() ...

Each call to repaint() will reinvoke the paint() method. So instead of looping in inc() do this:

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

Your computer cannot repaint as fast as your cpu increases sizeX and sizeY. Add some waiting time in milliseconds(especially if you draw a big area like 800x600)

 for(int i = 0; i<25000; i++)
    {
        sizeX++;
        sizeY++;
        repaint();
        try{ sleep(100);} catch(InterruptedException e){}
    }

But, do this from another thread! So, your app. will not freeze.

This is 10 FPS. But you tried 2G FPS. Good day.

Here is an example of a thread.

class TEN_FPS extends Thread
{
    public void run()
    {
        while(working)
        {
             //calculations here
             repaint();
             try{sleep(100);}catch(InterruptedException e){}
        }
     }
 }

Then in the main method:

working=true;
TEN_FPS.start();

When you are done with this program:

working=false;

will release your daemon thread.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top