Sign in to follow this  
yukit0

[java] MouseDrag gestures

Recommended Posts

Hi I've been working on a HUD system for a game I'm programming in Java. The problem is that when I drag my mouse, the mouseDrag() function is called, but only the first time it is called it actually prints out that the dragging is being registered. Other than that, the HUD also doesnt change the coordinates of the panels on the HUD. Output is:
FPS counter set to true
Creating new offscreen image, W=312 H=215
Mouse pressed down.
Inside the dragbars of 0.
Mouse being dragged.
While the HUD is expecting it, moving panel 0.
Mouse being dragged.
Mouse being dragged.
Mouse being dragged.
Mouse being dragged.
Mouse being dragged.
Mouse being dragged.
Mouse being dragged.
Mouse being dragged.
Mouse being dragged.
Mouse being dragged.
Mouse being dragged.
Mouse being dragged.
Mouse being dragged.
Mouse released.




Here's the full source. I think mouseDown, mouseDragged and mouseUp should be the only ones that matter.
import java.awt.*;

public class HexaHUD {
	Font font;
	Tile t;
	LogData log;
	HUDPanel[] panels;
	boolean dragging;
	int focusOnPanel, prevX, prevY;
	
	private class HUDPanel {
		public int x, y;
		public boolean visible;
		int width, height;
		Image img;
		HexaDraw display;
		
		public Image getImage() {
			return img;
		}
		
		public Dimension getSize() {
			return new Dimension(width, height);
		}
		
		public HUDPanel(int w, int h, HexaDraw hex) throws NullPointerException {
			if(w < 32)
				w = 32;
			if(h < 32)
				h = 32;
			display = hex;
			img = display.createImage(w,h);
			width = w;
			height = h;
			if(img == null)
				throw new NullPointerException("Image could not be created");
			clear();
		}
		
		public void resize(int w, int h) throws NullPointerException {
			img = display.createImage(w,h);
			width = w;
			height = h;
			if(img == null)
				throw new NullPointerException("Image could not be created");
			clear();
		}
		
		private void clear() {
			Graphics gr = img.getGraphics();
			gr.setColor(EasyColor.CreateHTMLColor("#E6E3DE"));
			gr.fillRect(0,0,width,height); 
			gr.setColor(EasyColor.CreateHTMLColor("#3A6173"));
			gr.fillRect(1,1,width-2,14);
			display.repaint();
		}
		
		public void requestRefresh() {
			display.repaint();
		}
		
	}
	
	private class LogData {
		
	}
	
	public void mouseDown(int x, int y, boolean right) {
		System.out.println("Mouse pressed down.");
		for(int i = 0; i<panels.length; i++) {
			if(panels[i].visible && x >= panels[i].x +1 && x < panels[i].x + panels[i].getSize().width - 1 && y >= panels[i].y + 1 && y < panels[i].y + 15) {
				System.out.println("Inside the dragbars of " + i +".");
				focusOnPanel = i+1;
				prevX = x;
				prevY = y;
				dragging = true;
			}
		}
	}
	
	public void mouseUp() {
		System.out.println("Mouse released.");
		if(dragging)
			dragging = false;
	}
	
	public void mouseDrag(int x, int y, boolean right) {
		System.out.println("Mouse being dragged.");
		if(dragging == true) {
			System.out.println("While the HUD is expecting it, moving panel " + (focusOnPanel-1) + ".");
			int xchange = x - prevX;
			int ychange = y - prevY;
			panels[focusOnPanel-1].x += xchange; // Doesn't seem to change anything
			panels[focusOnPanel-1].y += ychange;
			prevX = x;
			prevY = y;
			panels[focusOnPanel-1].requestRefresh();
		}
	}
		
	public boolean processClick(int x, int y, boolean right) {
		boolean hit = false;
		int panel = 0;
		for(int i = 0; i<panels.length; i++) {
			if(panels[i].visible && x >= panels[i].x && x < panels[i].x + panels[i].getSize().width && y >= panels[i].y && y < panels[i].y + panels[i].getSize().height) {
				hit = true;
				panel = i;
			}
		}
		if(!hit) {
			focusOnPanel = 0;
			return false;
		} else {
			focusOnPanel = panel + 1;
			System.out.println("focus: " + (focusOnPanel-1));
			return true;
		}
	}

	public HexaHUD(HexaDraw h) throws NullPointerException {
		font = new Font("Serif", Font.PLAIN, 8);
		panels = new HUDPanel[2];
		for(int x = 0; x<panels.length; x++) {
			panels[x] = new HUDPanel(100,100, h);
		}
		panels[0].visible = true;
		panels[0].x =10;
	}
	
	public void draw(Graphics g) {
		for(int x = 0; x<panels.length; x++) {
			if(panels[x].visible)
				g.drawImage(panels[x].getImage(), panels[x].x, panels[x].y, null);
		}
	}
}




Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this