Results 1 to 4 of 4

Thread: JApplet problem

  1. #1
    fubared is offline Member
    Join Date
    Dec 2011
    Posts
    2
    Rep Power
    0

    Default JApplet problem

    the red dot for this program is not showing for some reason. any help will be greatly appreciated

    Java Code:
    import java.awt.*;
    import java.applet.*;
    import javax.sound.midi.*;
    import javax.sound.midi.Synthesizer;
    import javax.swing.text.*;
    import javax.swing.*;
    import java.awt.image.BufferedImage;
    import javax.imageio.ImageIO;
    import java.io.*;
    
    public class Piano extends JApplet
    {	
    	boolean hit = false;
    	int soundnumber;
    	int x;
    	int y;
    	int[] keyEdge = {54, 74, 90, 102, 118, 138, 158, 174, 186, 202, 214, 230, 250, 270, 
    	286, 298, 314, 334, 354, 370, 382, 398, 410, 426, 447};
    	int soundNumber;
    	int[] whiteKey = {0, 2, 4, 5, 7, 9, 11, 12, 14, 16, 17, 19, 21, 23};
    	int redotx;
    	boolean isWhite;
    	boolean record = false;
    	int a[] = new int[100];
    	int z = 0;
    	
    	Keyboard kBoard;
    	
    	Synthesizer synthesizer;
        	MidiChannel channel;
    	ClickMouseListenerClass mouseHandler;
    	ClickKeyListenerClass keyHandler;
    	RecordButtonListenerClass recordListener;
            PlayRecordedButtonListenerClass playRecordedListener;
    	JButton recordButton;
    	JButton playButton;
    	BufferedImage myPicture;
    	Dot rdot;	
    
    	public void init()
    	{
    		try{
    			myPicture = ImageIO.read(new File("piano.gif"));
    		}
    		catch(IOException ie){}
    		
    		JLabel picLabel = new JLabel(new ImageIcon(myPicture));
    
    		Container c = getContentPane();
    
    		Box all = Box.createVerticalBox();
    		Box top = Box.createHorizontalBox();
    		Box bottom = Box.createHorizontalBox();
    		recordButton = new JButton("Record");
    		playButton = new JButton("Play");
    		
    		c.setLayout(new FlowLayout());
    	
    		this.rdot = new Dot();
    		top.add(this.rdot);
    
    		top.add(picLabel);
    		all.add(top);
    		all.add(Box.createVerticalStrut(25));
    		bottom.add(recordButton);
    		bottom.add(playButton);
    		all.add(bottom);
    
    		c.add(all);
    		resize(500, 450);	    	
    
    		kBoard = new Keyboard();
    		kBoard.setKeys();
            	kBoard.setSound();
    		
    		try
    		{
    		  synthesizer = MidiSystem.getSynthesizer();
    		  synthesizer.open();
    		  channel = synthesizer.getChannels()[0];
    		}
    		catch(MidiUnavailableException mue) {}
    		
    		
    		mouseHandler = new ClickMouseListenerClass(this);
    		this.addMouseListener(mouseHandler); 
    		
    		keyHandler = new ClickKeyListenerClass(this);
    		c.addKeyListener(keyHandler);
    		c.setFocusable(true); 
    		
    		recordListener = new RecordButtonListenerClass(this);		
    		//recordButton = new JButton("Record");
    		recordButton.addActionListener(recordListener);
    
                    playRecordedListener = new PlayRecordedButtonListenerClass(this);
                    //playButton = new JButton("Play");
                    playButton.addActionListener(playRecordedListener);
    	}
    
    	public void paintComponent(Graphics g){
    		g.drawImage(myPicture, 0, 0, myPicture.getWidth(this), myPicture.getHeight(this), this);
    	}
    
    	public class Dot extends JPanel{
    	public void paintComponent(Graphics g){
    		paintdot(g);
    		System.out.println("y");
    	}
    		
    	public void paintdot(Graphics g){
    	if(hit)
    		{
    			isWhite=false;
                            for(int i = 0; i < 14; i++){
                                    if(whiteKey[i] == soundNumber){
                                            isWhite=true;
    				}
    			}
    			if(isWhite){
    				g.setColor(Color.red);
                                    redotx = 54;
                                    for(int i = 0; i < 14; i++){
                                            if(whiteKey[i] == soundNumber){
                                                    redotx = i*28+54+14;
                                            }
                                    }
                                    g.fillOval(redotx - 4, 220, 10, 10);
    			}
    			else{
    				g.setColor(Color.red);
                                    redotx = ((keyEdge[soundNumber + 1] + keyEdge[soundNumber])/2)+2;
                                    g.fillOval(redotx - 4, 180, 10, 10);
                            }
    		}
    		System.out.println("dot"+redotx);
    	}
    	}
    	   public void checkKey(char c)
    	   {
    		String myKey[] = {"a","h","c","d","e","f","g"};
                    int mySound[] = {9,11,0,2,4,5,7};
    		String thePressedKey = "" + c;
    		for(int i = 0; i < 7; i++)
    		{
    			 if(thePressedKey.equalsIgnoreCase(myKey[i]))
    			 {
    				soundNumber=mySound[i];
    				hit = true;
    				int kt = kBoard.getSound(soundNumber);	  
    		        play(kt);
    				
    				if(record)
    		         {
    			       storeSong(kt);
    			     }
    				repaint();
    			 }
    		  
    		}
    	  
    	   }
    	
    	   public void checkMouse(int x, int y)
    	   {
    		int white;
    		hit = false;
    		if((x >= 54) && (y >= 116) && (x <= 447) && (y <= 235))
    		{
    			hit=true;
    			if(y > 208)
    			 {
    				white = (x - 54)/28;
    				soundNumber = whiteKey[white];
    			 }
    			else
    			  {
    				for(int i = 0; i < 24; i++)
    				{
    					if((x > keyEdge[i]) && (x < keyEdge[i + 1]))
    					soundNumber = i;
    				}
                  }
    			  
    		   int mt = kBoard.getSound(soundNumber);	  
    		   play(mt);
    		   
    		   if(record)
    		    {
    			  storeSong(mt);
    			}
    		 }
    	   	repaint();
    	   }
    	   
    	public void recordButtonOn()
    	{
    	 record = true;
    	}
    	
    	public void storeSong(int s)
    	{
    	 a[z] = s;
    	 System.out.println(a[z]);
    	 z++;
    	}
    	
    	public void playRecordedSong()
    	{
             System.out.println("In play class");
    	     //int length = a.length;
             System.out.println("After play class" + z);
    	 for(int i=0; i<z; i++)
    	  {
    	    int prs = a[i];
    		System.out.println("After play class in for loop" + prs);
    		play(prs);
    		repaint();
    	  }
    	}
    	
    	public void play(int note)
    	{
                channel.noteOn(note, 50);
    			System.out.println("In play" + note);
    			try
    			{
                  Thread.sleep(200);
    			}
                catch(InterruptedException ie) {}		
                  
                channel.noteOff(note);
    	  }
    	
    }
    Last edited by Fubarable; 12-11-2011 at 10:50 PM. Reason: code tags added

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: JApplet problem

    Quote Originally Posted by fubared View Post
    the red dot for this program is not showing for some reason.
    Welcome to the java-forums.org!

    Can you tell us more about your problem? What does the red dot represent? When and where is it supposed to appear?

    Since you have a somewhat large bit of code posted, most of it unrelated to your current problem, you should strongly consider creating and posting a small applet that does nothing more than try to display a red dot in a similar fashion to your main program above. This is called creating an SSCCE, and the process of extracting out and simplifying the problem often will lead you to a solution without our help (the best outcome possible here), or failing that, will allow you to post concise runnable code that we can more easily understand, modify, and help you with.

    Luck!

  3. #3
    fubared is offline Member
    Join Date
    Dec 2011
    Posts
    2
    Rep Power
    0

    Default Re: JApplet problem

    public class Dot extends JPanel{
    public void paintComponent(Graphics g){
    paintdot(g);
    System.out.println("y");
    }

    public void paintdot(Graphics g){
    if(hit)
    {
    isWhite=false;
    for(int i = 0; i < 14; i++){
    if(whiteKey[i] == soundNumber){
    isWhite=true;
    }
    }
    if(isWhite){
    g.setColor(Color.red);
    redotx = 54;
    for(int i = 0; i < 14; i++){
    if(whiteKey[i] == soundNumber){
    redotx = i*28+54+14;
    }
    }
    g.fillOval(redotx - 4, 220, 10, 10);
    }
    else{
    g.setColor(Color.red);
    redotx = ((keyEdge[soundNumber + 1] + keyEdge[soundNumber])/2)+2;
    g.fillOval(redotx - 4, 180, 10, 10);
    }
    }
    System.out.println("dot"+redotx);
    }
    }

    public void init()
    {
    try{
    myPicture = ImageIO.read(new File("piano.gif"));
    }
    catch(IOException ie){}

    JLabel picLabel = new JLabel(new ImageIcon(myPicture));

    Container c = getContentPane();

    Box all = Box.createVerticalBox();
    Box top = Box.createHorizontalBox();
    Box bottom = Box.createHorizontalBox();
    recordButton = new JButton("Record");
    playButton = new JButton("Play");

    c.setLayout(new FlowLayout());

    this.rdot = new Dot();
    top.add(this.rdot);

    top.add(picLabel);
    all.add(top);
    all.add(Box.createVerticalStrut(25));
    bottom.add(recordButton);
    bottom.add(playButton);
    all.add(bottom);

    c.add(all);
    resize(500, 450);
    }

    But some how the dot (oval) do not appear in appletviewer

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: JApplet problem

    You'll want to re-read the link on how to create an SSCCE, because I can't run the code from either of your posts above, and so cannot reproduce your problem or help you.

    Best of luck!

Similar Threads

  1. JApplet GUI problem
    By mitra in forum Java Applets
    Replies: 1
    Last Post: 08-29-2011, 02:25 PM
  2. Problem with JApplet painting
    By Fuzzier in forum Java Applets
    Replies: 5
    Last Post: 07-29-2010, 08:54 PM
  3. JApplet display problem
    By Darrarski in forum New To Java
    Replies: 10
    Last Post: 06-05-2010, 07:02 AM
  4. JApplet Problem
    By tanmoy.b81 in forum AWT / Swing
    Replies: 2
    Last Post: 07-28-2008, 06:56 AM
  5. Another problem in JApplet :S
    By juju in forum Java Applets
    Replies: 2
    Last Post: 12-30-2007, 07:46 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •