All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.barrybecker4.simulation.fluid.ui.InteractionHandler Maven / Gradle / Ivy

There is a newer version: 1.6.2
Show newest version
/** Copyright by Barry G. Becker, 2000-2011. Licensed under MIT License: http://www.opensource.org/licenses/MIT  */
package com.barrybecker4.simulation.fluid.ui;

import com.barrybecker4.simulation.fluid.model.Grid;

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

/**
 * Handle mouse interactions - converting them in to physical manifestations.
 *
 * @author Barry Becker
 */
public class InteractionHandler implements MouseListener, MouseMotionListener {

    public static final double DEFAULT_FORCE = 3.0f;
    public static final double DEFAULT_SOURCE_DENSITY = 1.0f;

    private double force_ = DEFAULT_FORCE;
    private double sourceDensity_ = DEFAULT_SOURCE_DENSITY;

    Grid grid_;

    double scale_;

    private int currentX, currentY;
    private int lastX, lastY;
    private boolean mouse1Down, mouse3Down;

    /**
     * Constructor
     */
    public InteractionHandler(Grid grid,  double scale) {
        scale_ = scale;
        grid_ = grid;
    }

    public void setForce(double force) {
        force_ = force;
    }

    public void setSourceDensity(double sourceDensity) {
        sourceDensity_ = sourceDensity;
    }


    /**
     * Make waves or adds ink when dragging depending on the mouse key held down.
     */
    public void mouseDragged(MouseEvent e) {

        currentX = e.getX();
        currentY = e.getY();
        int i = (int) (currentX / scale_);
        int j = (int) (currentY / scale_);

        // apply the change to a convolution kernel area
        int startX = Math.max(1, i - 1);
        int stopX = Math.min(grid_.getWidth(), i + 1);
        int startY = Math.max(1, j - 1);
        int stopY = Math.min(grid_.getHeight(), j + 1);


        for (int ii=startX; ii




© 2015 - 2025 Weber Informatics LLC | Privacy Policy