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

examples.linear.line.application.ElasticLineApplication Maven / Gradle / Ivy

There is a newer version: 6.0.0
Show newest version
package examples.linear.line.application;

import java.awt.Color;

import br.com.etyllica.commons.context.Application;
import br.com.etyllica.commons.context.UpdateIntervalListener;
import br.com.etyllica.commons.event.MouseEvent;
import br.com.etyllica.commons.event.PointerEvent;
import br.com.etyllica.core.graphics.Graphics;
import br.com.etyllica.linear.Point2D;
import br.com.etyllica.util.PointUtils;

public class ElasticLineApplication extends Application implements UpdateIntervalListener {

	private Point2D p;
	private Point2D q;

	private boolean pressed = false;
	private boolean back = false;
	
	private int currentArea = 0;
	private int divisions = 6;

	public ElasticLineApplication(int w, int h) {
		super(w,h);
	}

	public void load() {

		p = new Point2D(100, 200);
		q = new Point2D(200, 300);

		updateAtFixedRate(100, this);
		loading = 100;
	}

	@Override
	public void timeUpdate(long now) {

		if(pressed) {
			if(!back) {
				currentArea++;	
			} else {
				currentArea--;
				if(currentArea < 0) {
					pressed = false;
				}
			}
		}

		if(currentArea >= divisions) {
			back = true;
		}
	}

	public void draw(Graphics g) {
		
		if(pressed) {
			g.setColor(Color.BLUE);
			double distance = p.distance(q);
			double index = distance/divisions*currentArea;
			
			Point2D distantPoint = PointUtils.distantPoint(p, q, index);
			g.drawLine(p, distantPoint);
		} else {
			g.setColor(Color.BLACK);
			g.drawLine(p, q);	
		}
	}

	@Override
	public void updateMouse(PointerEvent event) {
		q.setLocation(event.getX(), event.getY());

		if(event.isButtonDown(MouseEvent.MOUSE_BUTTON_LEFT)) {
			pressed = true;
			back = false;
			currentArea = 0;
		}
	}
}





© 2015 - 2025 Weber Informatics LLC | Privacy Policy