User Tools

Site Tools


resources_for_dotclicker

This is an old revision of the document!


Dot Clicker

Here is a dot that has a “is hit” method

import java.awt.Graphics;
import java.awt.Color;
public class Dot
{
    private int x,y,size;
    /**
     * Constructor for objects of class Dot
     */
    public Dot()
    {
        x = 50+(int)(500*Math.random());
        y = 50+(int)(300*Math.random());
        size = 10 +(int)(20*Math.random());
    }

    public Dot(int x,int y,int size)
    {
        this.x = x;
        this.y=y;
        this.size = size;
    }

    public void draw(Graphics g){
        g.setColor(Color.BLACK);
        g.fillOval(x,y,size,size);
    }

    /**
     * returns true if (x0,y0) is inside the dot
     */
    public boolean isHit(int x0, int y0)
    {
        double x2 = x0 - (x+size/2.0);
        double y2 = y0 - (y+size/2.0);
        return x2*x2 + y2*y2 - size*size <0;// Pythagorean theorem
    }
}

Here is a basic Timer with spawning of dots and expiration of dots

import java.awt.Graphics;
import java.awt.Color;
public class Dot
{
    private int x,y,size;
    /**
     * Constructor for objects of class Dot
     */
    public Dot()
    {
        x = 50+(int)(500*Math.random());
        y = 50+(int)(300*Math.random());
        size = 10 +(int)(20*Math.random());
    }

    public Dot(int x,int y,int size)
    {
        this.x = x;
        this.y=y;
        this.size = size;
    }

    public void draw(Graphics g){
        g.setColor(Color.BLACK);
        g.fillOval(x,y,size,size);
    }

    /**
     * returns true if (x0,y0) is inside the dot
     */
    public boolean isHit(int x0, int y0)
    {
        double x2 = x0 - (x+size/2.0);
        double y2 = y0 - (y+size/2.0);
        return x2*x2 + y2*y2 - size*size <0;// pythaorean theorem
    }
}
resources_for_dotclicker.1652125175.txt.gz · Last modified: 2022/05/09 15:39 by frchris

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki