Member Avatar for Ralphael

I have a project I am working on and it involves java graphics which I am a novice at, I have uploaded the description of the project and the image of how it should look like, now i am having problems implementing the part of introducing the virus and drug into the running program. Below is the code

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package malariademonstration;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import malariademonstration.SuperClasses.*;

public class MalariaDemonstration extends JFrame{


    JButton close;
    JButton introDrug;
    JButton introVirus;
    JPanel panel;
    AnimationPanel panel1;
    JLabel timer;

    public MalariaDemonstration()
    {
        super("Malaria Demonstration");
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(600, 400);
        setLocationRelativeTo(null);
        //setResizable(false);
        close = new JButton("Close");
        introDrug = new JButton("Introduce Drug");
        introVirus = new JButton("Introduce Virus");
        timer = new JLabel("00:00:00");
        panel = new JPanel();
        panel1 = new AnimationPanel(this);
        panel.setLayout(new FlowLayout());
        panel.add(timer);
        panel.add(introVirus);
        panel.add(introDrug);
        panel.add(close);
        add(panel,BorderLayout.NORTH);
        add(panel1,BorderLayout.CENTER);
        panel1.runAnimation();

        close.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){System.exit(0);}});
        introVirus.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){panel1.addVirus();}});
    }

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



/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package malariademonstration.SuperClasses;

import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.TimerTask;

public class AnimationPanel extends JPanel{

    int width;
    int height;
    private Cells [] cell;
    private int cellNumber = 30;
    private BufferedImage buffer;

    public AnimationPanel(JFrame frame)
    {
        width = frame.getWidth()-10;
        height = frame.getHeight()-70;
        System.out.println(""+width+" "+height);
        setSize(new Dimension((width),(height)));
        cell = new Cells[cellNumber];
        for(int i=0; i<30;i++)
        {
            if(i<25)
            {
                cell[i] = new RedCells(width,height);
                //System.out.println(""+cell[i].getPx()+" "+cell[i].getPy());
            }
            else
            {
                cell[i] = new WhiteCells(width,height);
                //System.out.println(""+cell[i].getPx()+" "+cell[i].getPy());
            }
        }
        buffer = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    }


    private void draw(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;
        g2d.setBackground(Color.WHITE);
        g2d.clearRect(0, 0, this.getWidth(), this.getHeight());
        if (cell != null) {
            for (Cells cells : cell) {
                g2d.setColor(Color.BLACK);
                g2d.drawOval((int) cells.getPx(), (int) cells.getPy(), (int) cells.getRadius() * 2, (int) cells.getRadius() * 2);
                g2d.setColor(cells.getColor());
                g2d.fillOval((int) cells.getPx(), (int) cells.getPy(), (int) cells.getRadius() * 2, (int) cells.getRadius() * 2);
            }
        }
    }

    public void paint(Graphics g) {
        if (buffer != null) {
            draw(buffer.getGraphics());
            g.drawImage(buffer, 0, 0, null);
        }
    }

    public void checkCollision() {
        for (int i = 0; i < cell.length; i++) {
            Cells cell1 = cell[i];

            //check for collision between each cell and wall
            if (cell1.getPx() <= 0) {
                cell1.setPx(0);
                cell1.reverseVx();
            }
            if (cell1.getPx() + (cell1.getRadius() * 2) >= getWidth()) {
                cell1.setPx(getWidth() - cell1.getRadius() * 2);
                cell1.reverseVx();
            }
            if (cell1.getPy() <= 0) {
                cell1.setPy(0);
                cell1.reverseVy();
            }
            if (cell1.getPy() + (cell1.getRadius() * 2) >= getHeight()) {
                cell1.setPy(getHeight() - cell1.getRadius() * 2);
                cell1.reverseVy();
            }

            //move each cell 1 step
            cell1.move();

            //test for collision between cell
            for (int j = i + 1; j < cell.length; j++) {
                //this is so that 2 cell combination that have been checked for are not rechecked
                Cells cell2 = cell[j];

                //minimum distance betweeb cell for collision to happen
                double collisionDistance = cell1.getRadius() + cell2.getRadius();

                //current distance between cell
                double xDistance = cell1.getCenter()[0] - cell2.getCenter()[0];
                double yDistance = cell1.getCenter()[1] - cell2.getCenter()[1];
                double distance = Math.sqrt(Math.pow(xDistance, 2) + Math.pow(yDistance, 2));

                if (distance <= collisionDistance) {

                    //collision of the two cell has happened
                    double collisionAngle = Math.atan2(yDistance, xDistance);
                    double magCells1 = cell1.getSpeed();
                    double magCells2 = cell2.getSpeed();
                    double angleCells1 = cell1.getAngle();
                    double angleCells2 = cell2.getAngle();

                    double sxCells1 = magCells1 * Math.cos(angleCells1 - collisionAngle);
                    double syCells1 = magCells1 * Math.sin(angleCells1 - collisionAngle);
                    double sxCells2 = magCells2 * Math.cos(angleCells2 - collisionAngle);
                    double syCells2 = magCells2 * Math.sin(angleCells2 - collisionAngle);

                    double fsxCells1 = ((cell1.getMass() - cell2.getMass()) * sxCells1 + (cell2.getMass() + cell2.getMass()) * sxCells2) / (cell1.getMass() + cell2.getMass());
                    double fsxCells2 = ((cell1.getMass() + cell1.getMass()) * sxCells1 + (cell2.getMass() - cell1.getMass()) * sxCells2) / (cell1.getMass() + cell2.getMass());
                    double fsyCells1 = syCells1;
                    double fsyCells2 = syCells2;

                    cell1.setVx(Math.cos(collisionAngle) * fsxCells1 + Math.cos(collisionAngle + Math.PI / 2) * fsyCells1);
                    cell1.setVy(Math.sin(collisionAngle) * fsxCells1 + Math.sin(collisionAngle + Math.PI / 2) * fsyCells1);
                    cell2.setVx(Math.cos(collisionAngle) * fsxCells2 + Math.cos(collisionAngle + Math.PI / 2) * fsyCells2);
                    cell2.setVy(Math.sin(collisionAngle) * fsxCells2 + Math.sin(collisionAngle + Math.PI / 2) * fsyCells2);

                    if (distance < collisionDistance) {
                        //intersecting collisions happened
                        cell1.move(collisionDistance - distance);
                        //cell2.move((collisionDistance - distance) / 2);
                    }


                } else {
                    //distance between cell in the next step
                    double nxDistance = (cell1.getCenter()[0] + cell1.getVx()) - (cell2.getCenter()[0] + cell2.getVx());
                    double nyDistance = (cell1.getCenter()[1] + cell1.getVy()) - (cell2.getCenter()[1] + cell2.getVy());
                    double predictedDistance = Math.sqrt(Math.pow(nxDistance, 2) + Math.pow(nyDistance, 2));

                    if (predictedDistance < collisionDistance) {
                        //intersecting collision probably will happen in next step
                        //take preventive measures
                    }
                }
            }
        }
    }

    private class Task extends TimerTask {
        public void run()
        {
            checkCollision();
            repaint();
        }
    }

    public void runAnimation()
    {
        java.util.Timer t = new java.util.Timer("main");
        t.schedule(new Task(), 0, 60);
    }
}




package malariademonstration.SuperClasses;

/**
 *
 * @author Adetola-Ralph
 */
import java.awt.Color;
import java.util.Random;

/**
 * Ball class
 * @author Adetola-Ralph
 */
public abstract class Cells {

    //<editor-fold defaultstate="collapsed" desc="declaration of variables">
    private double px, py; //position of ball on x and y axis respectively
    private double vx, vy; //velocity of the ball in terms of change in position of x and y
    private Color color;//color of the circle
    private double radius;//radius of the circle
    /**
     * default ball radius 
     */
    public static final int DEFAULT_RADIUS = 20;
    /**
     * default ball speed on both axis
     */
    public static final int DEFAULT_SPDXY = 10;
    //</editor-fold>
    /**
     * Default constructor for the class
     */
    public Cells() {
        this.color = Color.WHITE;
        this.px = 0;
        this.py = 0;
        this.vx = new Random().nextInt(DEFAULT_SPDXY);
        this.vy = new Random().nextInt(DEFAULT_SPDXY);
        this.radius = DEFAULT_RADIUS;
    }

    /**
     * Constructor for the class, accepts the canvas dimensions
     * @param frameWidth of the canvas
     * @param frameHeight of the canvas 
     */
    public Cells(int frameWidth, int frameHeight,Color color) {
        this.color = color;
        this.px = new Random().nextInt(frameWidth);
        this.py = new Random().nextInt(frameHeight);
        this.vx = new Random().nextInt(DEFAULT_SPDXY);
        this.vy = new Random().nextInt(DEFAULT_SPDXY);
        this.radius = DEFAULT_RADIUS;
    }


    /**
     * @param color the color to set
     */
    public void setColor(Color color) {
        this.color = color;
    }

    /**
     * Returns the color of the ball
     * @return integer
     */
    public Color getColor() {
        return color;
    }

    /**
     * Returns the radius of the ball
     * @return integer
     */
    public double getRadius() {
        return radius;
    }

    /**
     * @param radius the radius to set
     */
    public void setRadius(double radius) {
        this.radius = radius;
    }

    /**
     * Returns the position of the ball in respect to the x-axis
     * @return integer
     */
    public double getPx() {
        return px;
    }

    /**
     * Returns the position of the ball in respect to the y-axis
     * @return integer
     */
    public double getPy() {
        return py;
    }

    /**
     * Returns the velocity of the ball in respect to the increment on the x-axis
     * @return integer
     */
    public double getVx() {
        return vx;
    }

    /**
     * Returns the velocity of the ball in respect to the increment on the y-axis
     * @return integer
     */
    public double getVy() {
        return vy;
    }

    /**
     * sets the x-value of the ball on the x-axis
     * @param x the position on the x axis
     */
    public void setPx(double x) {
        px = x;
    }

    /**
     * sets the y-value of the ball on the y-axis
     * @param y the position on the y axis
     */
    public void setPy(double y) {
        py = y;
    }

    /**
     * sets the x-value of the ball velocity
     * @param x the x axis velocity
     */
    public void setVx(double x) {
        vx = x;
    }

    /**
     * sets the y-value of the ball velocity
     * @param y the y axis velocity
     */
    public void setVy(double y) {
        vy = y;
    }

    /**
     * inverse horizontal velocity
     */
    public void reverseVx() {
        vx = -vx;
        //setColor();
        //System.out.println("reverseVx");
    }

    /**
     * inverse vertical velocity
     */
    public void reverseVy() {
        vy = -vy;
        //setColor();
        //System.out.println("reverseVy");
    }

    /**
     * Returns the coordinate of the center for the ball
     * @return an array of the ball center
     */
    public double[] getCenter() {
        double position[] = new double[2];
        position[0] = px + radius;
        position[1] = py + radius;

        return position;
    }

    /**
     * Uses color to determine density
     * @return density of ball
     */
    public double getDensity() {
        return 5.0 + color.getBlue()/255.0 + color.getGreen()/255.0 + color.getRed()/255.0;
    }

    /**
     * Uses area as volume and density to calculate mass
     * @return mass of ball
     */
    public double getMass() {
        return Math.PI * Math.pow(radius, 2) * getDensity();
    }

    /**
     * Calculates the just the magnitude of the balls velocity
     * @return speed of ball
     */
    public double getSpeed() {
        return Math.sqrt(Math.pow(vx, 2) + Math.pow(vy, 2));
    }

    /**
     * Calculates the just the angle of the balls velocity
     * @return angle of balls movement
     */
    public double getAngle() {
        return Math.atan2(vy, vx);
    }

    /**
     * move the ball one step
     */
    public void move() {
        px += vx;
        py += vy;
    }

    /**
     * move the ball one step by magnitude
     * @param mag the magnitude
     */
    public void move(double mag) {
        px += (mag * vx)/getSpeed();
        py += (mag * vy)/getSpeed();
    }
}



/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package malariademonstration.SuperClasses;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 *
 * @author Adetola-Ralph
 */
public class RedCells extends Cells{

    public RedCells(int frameWidth, int frameHeight)
    {
        super(frameWidth, frameHeight,Color.RED);
    }

}

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package malariademonstration.SuperClasses;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 *
 * @author Adetola-Ralph
 */
public class WhiteCells extends Cells{

    public WhiteCells(int frameWidth, int frameHeight)
    {
        super(frameWidth, frameHeight,Color.WHITE);
    }

}


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package malariademonstration.SuperClasses;

import java.awt.*;
import java.awt.event.*;
import java.util.TimerTask;
import javax.swing.*;

/**
 *
 * @author Adetola-Ralph
 */
public class Malaria extends Cells{

    public Malaria(int frameWidth, int frameHeight)
    {
        super(frameWidth, frameHeight,Color.BLACK);
    }


}


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package malariademonstration.SuperClasses;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 *
 * @author Adetola-Ralph
 */
public class Drug extends Cells{

    public Drug(int frameWidth, int frameHeight)
    {
        super(frameWidth, frameHeight,Color.BLUE);
    }

}

Thanks in advance for any help

What exactly is you problem/question?

Member Avatar for Ralphael

writing a function to that will introduce the malaria virus if a button is clicked

Yes, you already said that.
last chance: What exactly is your problem/question? We have no idea what you already know/do not know. Do you need help defining a method, writing an action listener, creating a Virus instance? Did you write the code you posted?

Member Avatar for Ralphael

yes i wrote the code, and i have asked the question, did u read the file i uploaded

OK, I've got other things to do. Maybe someone else will help you.

You need to tell us what the problem with the code is! And where is the uploaded file with question you talk about?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.