hi! i now implementing laptop playing guitar where my touchpad is to do strumming and keyboard keys is to change chords. when i pressed a key and moving the mouse cursor i can do the strumming, now my problem is when i released the key i still can do the strumming when i moving the mouse cursor. any ideas that i able to cut/stop the midi when i released the key? thanks in advance!

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

public class Testing extends JFrame
{	MidiChannel mc;
	boolean downA = false;
	
	void setDownA()
	{ this.downA = true;
	}
	
	void resetDownA()
	{ this.downA = false;
	}
	
	boolean getDownA()
	{ return downA;
	}
 
	public void paint(Graphics g)
	{	g.drawLine(0, 121, 1280, 121);
		g.drawLine(0, 242, 1280, 242);
		g.drawLine(0, 363, 1280, 363);
		g.drawLine(0, 484, 1280, 484);
		g.drawLine(0, 605, 1280, 605);
		g.drawLine(0, 726, 1280, 726);
	}

 	public static void main(String[  ] args) throws MidiUnavailableException
	{	Synthesizer synthesizer = MidiSystem.getSynthesizer( );
		synthesizer.open( );
	   JFrame frame = new Testing(synthesizer);
		  
		frame.setSize(1280, 800);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	   frame.setVisible(true);
	} 
 
	public Testing(Synthesizer synth)
	{
	 	mc = synth.getChannels( )[1];
  
	 	addKeyListener(new MyKeyListener(this));
		addMouseMotionListener(new MyMouseMotionListener(this));
	}
	
	private class MyKeyListener extends KeyAdapter
	{	Testing myTesting;
	
		public MyKeyListener(Testing testing)
		{	myTesting = testing;
		}
	
		public void keyPressed(KeyEvent e)
		{	if (e.getKeyCode() == KeyEvent.VK_A)
				myTesting.setDownA();
		}
		
		public void keyRelease(KeyEvent e)
		{	if (e.getKeyCode() == KeyEvent.VK_A)
				myTesting.resetDownA();
		}
	}
		
	private class MyMouseMotionListener extends MouseMotionAdapter
	{	Testing myTesting;
	
		public MyMouseMotionListener(Testing testing)
		{	myTesting = testing;
		}		 
		
		int x = -1;
		int y = -1;
						
		public void mouseMoved(MouseEvent me)
		{	if (myTesting.getDownA())
			{	int cx = me.getX();
				int cy = me.getY();
					  
		  		if( x == -1 && y == -1)
		  		{	x = cx;
					y = cy;
	   			return;	 
	   		}	 
		  					  
		  		int dx = cx - x;
		  		int dy = cy - y;
		  
		  		double d = Math.sqrt((dx*dx) + (dy*dy));
		 
		 		int n1 = 60;
		 		int n2 = 65;
		 		int n3 = 70;
		 		int n4 = 75;
		 		int n5 = 80;
		 		int n6 = 85;
					
				System.out.println(d);
						
				if ( d > 30 && d < 50)
				{	mc.noteOn(n1,100);
					mc.noteOff(n1,100);
				}									  
										
 				if ( d > 80 && d < 100)
				{	mc.noteOn(n2,100);
					mc.noteOff(n2,100);
				}
						
				if ( d > 130 && d < 150)
				{	mc.noteOn(n3,100);
					mc.noteOff(n3,100);
				}
						
				if ( d > 180 && d < 200)
				{	mc.noteOn(n4,100);
					mc.noteOff(n4,100);
				}
						
				if ( d > 230 && d < 250)
				{	mc.noteOn(n5,100);
					mc.noteOff(n5,100);
				}
						
				if ( d > 280 && d < 300)
				{	mc.noteOn(n6,100);
					mc.noteOff(n6,100);
				}
		
				try
				{	Thread.sleep(1);
				}
				catch (InterruptedException e)
				{	e.printStackTrace();
				}
			}
		}
	}
}

Recommended Answers

All 5 Replies

In the mouse moved method, say "if(downA == false)return;" as the first statement in the method.

No problem, mark solved threads as solved

by the way do you have any idea to make the strumming function becomes more sensitive? because when i move my mouse cursor very fast the program tends to skip notes..

how exactly do you run this? seems interesting i would like to try it.

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.