Results 1 to 4 of 4
  1. #1
    jamesfrize is offline Member
    Join Date
    Mar 2008
    Posts
    19
    Rep Power
    0

    Default images, panels and applets

    I'm working on a applet based GUI (piano keys) for JSyn (Phil Burks Java synthesis engine).
    I have made a working version of the program that is an extension of JFrame.
    I then adapted it to be used as a JApplet. I can't get the main Image to load, It wont recognize the getImage() or getCodeBase() methods. I think this is because the Image is created inside a JPanel and not in the JApplet.
    I don't want to just add it to the main JApplet because I want to keep it as a separate class that can be added as a component to other JApplets.
    Some help/advice would be great, here's the code- Cheers:

    (I have attached a copy of the Image File I use for this program- piano.jpg)

    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.applet.*;

    public class pianoGUI extends JApplet
    {
    private JPiano piano;

    public void init()
    {
    getContentPane().setLayout(new GridLayout(1,1));
    JPiano piano = new JPiano();
    getContentPane().add(piano);
    setBounds (0,0,480,140);
    }

    public void destroy()
    {
    piano.stop();
    }
    }

    ///////////////////////////////////////////////////////////

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

    public class JPiano extends JPanel
    {
    private Image piano;
    private int state = 0, x, y;
    private note2 note;
    private MediaTracker mt;


    public JPiano()
    {
    setLayout(new GridLayout(1,1));
    mt = new MediaTracker(this);
    piano = getImage(getCodeBase(), "piano.jpg" );
    mt.addImage(piano,1);

    try
    {
    mt.waitForAll();
    }

    catch (InterruptedException e) {}

    note = new note2();

    addMouseListener
    (
    new MouseAdapter()
    {
    public void mousePressed(MouseEvent me)
    {
    mPress(me.getX(), me.getY());
    }
    }
    );

    addMouseListener
    (
    new MouseAdapter()
    {
    public void mouseReleased(MouseEvent me)
    {
    mRelease();
    }
    }
    );

    }

    public void stop()
    {
    note.stop();
    }

    private void mRelease()
    {
    state = 0;
    repaint();
    note.release();
    }

    private void mPress(int X1, int Y1)
    {
    x = X1;
    y = Y1;
    state = 1;
    repaint();
    if (x >= 2 && x <= 22 && y >= 2 && y <= 114)
    {
    System.out.println("C1");
    note.change(32.70);
    }

    else if (x >= 22 && x <= 34 && y >= 70 && y <= 114)
    {
    System.out.println("C1");
    note.change(32.70);
    }


    else if (x >= 22 && x <= 48 && y >= 2 && y <= 70)
    {
    System.out.println("C#1");
    note.change(34.65);
    }

    else if (x >= 34 && x <= 48 && y > 70 && y <= 114)
    {
    System.out.println("D1");
    note.change(36.71);
    }

    else if (x >= 48 && x <= 56 && y >= 2 && y <= 114)
    {
    System.out.println("D1");
    note.change(36.71);
    }

    else if (x >= 56 && x <= 68 && y >= 70 && y <= 114)
    {
    System.out.println("D1");
    note.change(36.71);
    }

    else if (x >= 56 && x <= 80 && y >= 2 && y <= 70)
    {
    System.out.println("D#1");
    note.change(38.89);
    }

    else if (x >= 68 && x <= 80 && y >= 70 && y <= 114)
    {
    System.out.println("E1");
    note.change(41.20);
    }


    else if (x >= 68 && x <= 102 && y >= 2 && y <= 114)
    {
    System.out.println("E1");
    note.change(41.20);
    }

    else if (x >= 102 && x <= 124 && y > 2 && y <= 114)
    {
    System.out.println("F1");
    note.change(43.65);
    }

    else if (x >= 124 && x <= 136 && y >= 70 && y <= 114)
    {
    System.out.println("F1");
    note.change(43.65);
    }

    else if (x >= 124 && x <= 150 && y >= 2 && y <= 70)
    {
    System.out.println("F#1");
    note.change(46.25);
    }

    else if (x >= 136 && x <= 150 && y >= 70 && y <= 114)
    {
    System.out.println("G1");
    note.change(49.00);
    }


    else if (x >= 150 && x <= 158 && y >= 2 && y <= 114)
    {
    System.out.println("G1");
    note.change(49.00);
    }

    else if (x >= 158 && x <= 170 && y > 70 && y <= 114)
    {
    System.out.println("G1");
    note.change(49.00);
    }

    else if (x >= 158 && x <= 184 && y >= 2 && y <= 70)
    {
    System.out.println("G#1");
    note.change(51.91);
    }

    else if (x >= 170 && x <= 184 && y >= 2 && y <= 114)
    {
    System.out.println("A1");
    note.change(55.00);
    }

    else if (x >= 184 && x <= 192 && y >= 2 && y <= 114)
    {
    System.out.println("A1");
    note.change(55.00);
    }


    else if (x >= 192 && x <= 204 && y >= 70 && y <= 114)
    {
    System.out.println("A1");
    note.change(55.00);
    }

    else if (x > 192 && x <= 218 && y > 0 && y <= 70)
    {
    System.out.println("A#1");
    note.change(58.27);
    }

    else if (x >= 204 && x <= 218 && y >= 2 && y <= 114)
    {
    System.out.println("B1");
    note.change(61.74);
    }

    else if (x >= 218 && x <= 238 && y >= 2 && y <= 114)
    {
    System.out.println("B1");
    note.change(61.74);
    }

    else if (x >= 238 && x <= 260 && y >= 2 && y <= 114)
    {
    System.out.println("C2");
    note.change(65.41);
    }


    else if (x >= 260 && x <= 272 && y >= 70 && y <= 114)
    {
    System.out.println("C2");
    note.change(65.41);
    }

    else if (x >= 260 && x <= 286 && y > 2 && y <= 70)
    {
    System.out.println("C#2");
    note.change(69.30);
    }

    else if (x >= 272 && x <= 286 && y >= 70 && y <= 114)
    {
    System.out.println("D2");
    note.change(73.42);
    }

    else if (x >= 286 && x <= 296 && y >= 2 && y <= 114)
    {
    System.out.println("D2");
    note.change(73.42);
    }

    else if (x >= 296 && x <= 306 && y >= 70 && y <= 114)
    {
    System.out.println("D2");
    note.change(73.42);
    }


    else if (x >= 296 && x <= 318 && y >= 2 && y <= 70)
    {
    System.out.println("D#2");
    note.change(77.78);
    }

    else if (x >= 296 && x <= 318 && y > 70 && y <= 114)
    {
    System.out.println("E2");
    note.change(82.41);
    }

    else if (x >= 318 && x <= 340 && y >= 2 && y <= 114)
    {
    System.out.println("E2");
    note.change(82.41);
    }

    else if (x >= 340 && x <= 362 && y >= 2 && y <= 114)
    {
    System.out.println("F2");
    note.change(87.31);
    }

    else if (x >= 362 && x <= 374 && y >= 70 && y <= 114)
    {
    System.out.println("F2");
    note.change(87.31);
    }


    else if (x >= 362 && x <= 388 && y >= 2 && y <= 70)
    {
    System.out.println("F#2");
    note.change(92.50);
    }

    else if (x >= 374 && x <= 388 && y > 70 && y <= 114)
    {
    System.out.println("G2");
    note.change(98.00);
    }

    else if (x >= 388 && x <= 396 && y >= 2 && y <= 114)
    {
    System.out.println("G2");
    note.change(98.00);
    }

    else if (x >= 396 && x <= 408 && y >= 70 && y <= 114)
    {
    System.out.println("G2");
    note.change(98.00);
    }

    else if (x >= 396 && x <= 422 && y >= 2 && y <= 70)
    {
    System.out.println("G#2");
    note.change(103.83);
    }


    else if (x >= 408 && x <= 422 && y >= 70 && y <= 114)
    {
    System.out.println("A2");
    note.change(110.00);
    }

    else if (x >= 422 && x <= 430 && y > 2 && y <= 114)
    {
    System.out.println("A2");
    note.change(110.00);
    }

    else if (x >= 430 && x <= 442 && y >= 70 && y <= 114)
    {
    System.out.println("A2");
    note.change(110.00);
    }

    else if (x >= 430 && x <= 456 && y >= 2 && y <= 70)
    {
    System.out.println("A#2");
    note.change(116.54);
    }

    else if (x >= 442 && x <= 456 && y >= 70 && y <= 114)
    {
    System.out.println("B2");
    note.change(123.47);
    }


    else if (x >= 456 && x <= 476 && y >= 2 && y <= 114)
    {
    System.out.println("B2");
    note.change(123.47);
    note.attack();
    }

    note.attack();
    }

    public void paintComponent(Graphics g)
    {
    if (state == 1)
    {
    Color c = new Color(100,100,100);
    Color c2 = new Color(100,100,100);

    if (x >= 2 && x <= 22 && y >= 2 && y <= 114)
    {
    g.setColor(c);
    g.fillRect(2,2,20,111);
    g.fillRect(22,71,12,42);
    }

    else if (x >= 22 && x <= 34 && y >= 70 && y <= 114)
    {
    g.setColor(c);
    g.fillRect(2,2,20,111);
    g.fillRect(22,71,12,42);
    }


    else if (x >= 22 && x <= 48 && y >= 2 && y <= 70)
    {
    g.setColor(c2);
    g.fillRect(24,2,22,66);
    }

    else if (x >= 34 && x <= 48 && y > 70 && y <= 114)
    {
    g.setColor(c);
    g.fillRect(36,71,12,42);
    g.fillRect(48,2,8,111);
    g.fillRect(56,71,12,42);
    }

    else if (x >= 48 && x <= 56 && y >= 2 && y <= 114)
    {
    g.setColor(c);
    g.fillRect(36,71,12,42);
    g.fillRect(48,2,8,111);
    g.fillRect(56,71,12,42);
    }

    else if (x >= 56 && x <= 68 && y >= 70 && y <= 114)
    {
    g.setColor(c);
    g.fillRect(36,71,12,42);
    g.fillRect(48,2,8,111);
    g.fillRect(56,71,12,42);
    }

    else if (x >= 56 && x <= 80 && y >= 2 && y <= 70)
    {
    g.setColor(c2);
    g.fillRect(58,2,22,66);
    }

    else if (x >= 68 && x <= 80 && y >= 70 && y <= 114)
    {
    g.setColor(c);
    g.fillRect(70,71,12,42);
    g.fillRect(82,2,20,111);
    }


    else if (x >= 68 && x <= 102 && y >= 2 && y <= 114)
    {
    g.setColor(c);
    g.fillRect(70,71,12,42);
    g.fillRect(82,2,20,111);
    }

    else if (x >= 102 && x <= 124 && y > 2 && y <= 114)
    {
    g.setColor(c);
    g.fillRect(104,2,20,111);
    g.fillRect(124,71,12,42);
    }

    else if (x >= 124 && x <= 136 && y >= 70 && y <= 114)
    {
    g.setColor(c);
    g.fillRect(104,2,20,111);
    g.fillRect(124,71,12,42);
    }

    else if (x >= 124 && x <= 150 && y >= 2 && y <= 70)
    {
    g.setColor(c2);
    g.fillRect(126,2,22,66);
    }

    else if (x >= 136 && x <= 150 && y >= 70 && y <= 114)
    {
    g.setColor(c);
    g.fillRect(138,71,12,42);
    g.fillRect(150,2,8,111);
    g.fillRect(158,71,12,42);
    }


    else if (x >= 150 && x <= 158 && y >= 2 && y <= 114)
    {
    g.setColor(c);
    g.fillRect(138,71,12,42);
    g.fillRect(150,2,8,111);
    g.fillRect(158,71,12,42);
    }

    else if (x >= 158 && x <= 170 && y > 70 && y <= 114)
    {
    g.setColor(c);
    g.fillRect(138,71,12,42);
    g.fillRect(150,2,8,111);
    g.fillRect(158,71,12,42);
    }

    else if (x >= 158 && x <= 184 && y >= 2 && y <= 70)
    {
    g.setColor(c2);
    g.fillRect(160,2,22,66);
    }

    else if (x >= 170 && x <= 184 && y >= 2 && y <= 114)
    {
    g.setColor(c);
    g.fillRect(172,71,12,42);
    g.fillRect(184,2,8,111);
    g.fillRect(192,71,12,42);
    }

    else if (x >= 184 && x <= 192 && y >= 2 && y <= 114)
    {
    g.setColor(c);
    g.fillRect(172,71,12,42);
    g.fillRect(184,2,8,111);
    g.fillRect(192,71,12,42);
    }


    else if (x >= 192 && x <= 204 && y >= 70 && y <= 114)
    {
    g.setColor(c);
    g.fillRect(172,71,12,42);
    g.fillRect(184,2,8,111);
    g.fillRect(192,71,12,42);
    }

    else if (x > 192 && x <= 218 && y > 0 && y <= 70)
    {
    g.setColor(c2);
    g.fillRect(194,2,22,66);
    }

    else if (x >= 204 && x <= 218 && y >= 2 && y <= 114)
    {
    g.setColor(c);
    g.fillRect(206,71,12,42);
    g.fillRect(218,2,20,111);
    }

    else if (x >= 218 && x <= 238 && y >= 2 && y <= 114)
    {
    g.setColor(c);
    g.fillRect(206,71,12,42);
    g.fillRect(218,2,20,111);
    }

    else if (x >= 238 && x <= 260 && y >= 2 && y <= 114)
    {
    g.setColor(c);
    g.fillRect(240,2,20,111);
    g.fillRect(260,71,12,42);
    }


    else if (x >= 260 && x <= 272 && y >= 70 && y <= 114)
    {
    g.setColor(c);
    g.fillRect(240,2,20,111);
    g.fillRect(260,71,12,42);
    }

    else if (x >= 260 && x <= 286 && y > 2 && y <= 70)
    {
    g.setColor(c2);
    g.fillRect(262,2,22,66);
    }

    else if (x >= 272 && x <= 286 && y >= 70 && y <= 114)
    {
    g.setColor(c);
    g.fillRect(274,71,12,42);
    g.fillRect(286,2,8,111);
    g.fillRect(294,71,12,42);
    }

    else if (x >= 286 && x <= 296 && y >= 2 && y <= 114)
    {
    g.setColor(c);
    g.fillRect(274,71,12,42);
    g.fillRect(286,2,8,111);
    g.fillRect(294,71,12,42);
    }

    else if (x >= 296 && x <= 306 && y >= 70 && y <= 114)
    {
    g.setColor(c);
    g.fillRect(274,71,12,42);
    g.fillRect(286,2,8,111);
    g.fillRect(294,71,12,42);
    }


    else if (x >= 296 && x <= 318 && y >= 2 && y <= 70)
    {
    g.setColor(c2);
    g.fillRect(296,2,22,66);
    }

    else if (x >= 296 && x <= 318 && y > 70 && y <= 114)
    {
    g.setColor(c);
    g.fillRect(308,71,12,42);
    g.fillRect(320,2,20,111);
    }

    else if (x >= 318 && x <= 340 && y >= 2 && y <= 114)
    {
    g.setColor(c);
    g.fillRect(308,71,12,42);
    g.fillRect(320,2,20,111);
    }

    else if (x >= 340 && x <= 362 && y >= 2 && y <= 114)
    {
    g.setColor(c);
    g.fillRect(342,2,20,111);
    g.fillRect(362,71,12,42);
    }

    else if (x >= 362 && x <= 374 && y >= 70 && y <= 114)
    {
    g.setColor(c);
    g.fillRect(342,2,20,111);
    g.fillRect(362,71,12,42);
    }


    else if (x >= 362 && x <= 388 && y >= 2 && y <= 70)
    {
    g.setColor(c2);
    g.fillRect(364,2,22,66);
    }

    else if (x >= 374 && x <= 388 && y > 70 && y <= 114)
    {
    g.setColor(c);
    g.fillRect(376,71,12,42);
    g.fillRect(388,2,8,111);
    g.fillRect(396,71,12,42);
    }

    else if (x >= 388 && x <= 396 && y >= 2 && y <= 114)
    {
    g.setColor(c);
    g.fillRect(376,71,12,42);
    g.fillRect(388,2,8,111);
    g.fillRect(396,71,12,42);
    }

    else if (x >= 396 && x <= 408 && y >= 70 && y <= 114)
    {
    g.setColor(c);
    g.fillRect(376,71,12,42);
    g.fillRect(388,2,8,111);
    g.fillRect(396,71,12,42);
    }

    else if (x >= 396 && x <= 422 && y >= 2 && y <= 70)
    {
    g.setColor(c2);
    g.fillRect(398,2,22,66);
    }


    else if (x >= 408 && x <= 422 && y >= 70 && y <= 114)
    {
    g.setColor(c);
    g.fillRect(410,71,12,42);
    g.fillRect(422,2,8,111);
    g.fillRect(430,71,12,42);
    }

    else if (x >= 422 && x <= 430 && y > 2 && y <= 114)
    {
    g.setColor(c);
    g.fillRect(410,71,12,42);
    g.fillRect(422,2,8,111);
    g.fillRect(430,71,12,42);
    }

    else if (x >= 430 && x <= 442 && y >= 70 && y <= 114)
    {
    g.setColor(c);
    g.fillRect(410,71,12,42);
    g.fillRect(422,2,8,111);
    g.fillRect(430,71,12,42);
    }

    else if (x >= 430 && x <= 456 && y >= 2 && y <= 70)
    {
    g.setColor(c2);
    g.fillRect(432,2,22,66);
    }

    else if (x >= 442 && x <= 456 && y >= 70 && y <= 114)
    {
    g.setColor(c);
    g.fillRect(444,71,12,42);
    g.fillRect(456,2,20,111);
    }


    else if (x >= 456 && x <= 476 && y >= 2 && y <= 114)
    {
    g.setColor(c);
    g.fillRect(444,71,12,42);
    g.fillRect(456,2,20,111);
    }
    }

    else if(state == 0)
    {
    g.drawImage(piano,0,0,this);
    }
    }
    }

    //////////////////////////////////////////////////////////////////////////////

    import javax.swing.*;
    import com.softsynth.jsyn.*;

    public class note2
    {
    SquareOscillator s1;
    LineOut out;
    SynthEnvelope ampEnv;
    SynthEnvelope ampEnv2;
    EnvelopePlayer envPlayer;
    MultiplyUnit mult;
    AddUnit add;
    AddUnit add2;
    DelayUnit delay;


    public note2()
    {
    Synth.startEngine (0);

    double data[] =
    {
    0.05, 0.1
    };

    double data2[] =
    {
    0.05,0.0
    };

    s1 = new SquareOscillator();
    envPlayer = new EnvelopePlayer();
    mult = new MultiplyUnit();
    add = new AddUnit();
    add2 = new AddUnit();
    out = new LineOut();
    delay = new DelayUnit(0.2);
    ampEnv = new SynthEnvelope(data);
    ampEnv2 = new SynthEnvelope(data2);

    envPlayer.output.connect(s1.amplitude);
    s1.output.connect (add.inputA);
    add.output.connect (delay.input);
    delay.output.connect (mult.inputA);
    mult.output.connect (add.inputB);
    s1.output.connect (add2.inputA);
    delay.output.connect (add2.inputB);
    add2.output.connect (0, out.input, 0);
    add2.output.connect (0, out.input, 1);

    mult.inputB.set (0.8);

    envPlayer.start();
    s1.start();
    mult.start();
    add.start();
    add2.start();
    delay.start();
    out.start();
    }

    public void change(double freq)
    {
    s1.frequency.set(freq*2);
    }

    public void release()
    {
    envPlayer.envelopePort.queue(ampEnv2);
    }

    public void attack()
    {
    envPlayer.envelopePort.queue(ampEnv);
    }

    public void stop()
    {
    envPlayer.stop();
    s1.stop();
    mult.stop();
    add.stop();
    add2.stop();
    delay.stop();
    out.stop();
    Synth.stopEngine();
    }

    }
    Attached Thumbnails Attached Thumbnails images, panels and applets-piano.jpg  

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Java Code:
    mt = new MediaTracker(this);
    piano = getImage(getCodeBase(), "piano.jpg" );
    mt.addImage(piano,1);
    
    try
    { 
        mt.waitForAll(); 
    } 
    catch (InterruptedException e) {}
    The getCodeBase method only works inside a class that extends Applet/JApplet.
    Using the Toolkit and getImage methods is the oldest way of loading images.
    The newest way of loading images in java is with the ImageIO class which has several read methods.
    Using a URL instead of trying to create a File seems to work well in this situation.
    Java Code:
    import java.awt.*;
    import java.awt.image.BufferedImage;
    import java.io.*;
    import java.net.URL;
    import javax.imageio.ImageIO;
    import javax.swing.*;
    
    public class PianoImage extends JPanel {
        BufferedImage image;
        Dimension size = new Dimension();
    
        public PianoImage() {
            loadImage();
            size.setSize(image.getWidth(), image.getHeight());
        }
    
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.drawImage(image,0,0,this);
        }
    
        public Dimension getPreferredSize() {
            return size;
        }
    
        private void loadImage() {
            String path = "piano.jpg";
            try {
                // ClassLoader looks for resource in classpath.
                URL url = getClass().getResource(path);
                image = ImageIO.read(url);
            } catch(IOException e) {
                System.out.println("Read error: " + e.getMessage());
            }
        }
    
        public static void main(String[] args) {
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(new PianoImage());
            f.pack();
            f.setLocation(200,200);
            f.setVisible(true);
        }
    }

  3. #3
    jamesfrize is offline Member
    Join Date
    Mar 2008
    Posts
    19
    Rep Power
    0

    Default Still Confused by Applet Images

    Wow thats a lot of code for displaying an image! Someone should tell Java to simplify how the Applets work!

    Thanks for the response, It kind of helped. I got a bit confused as to what parts of the program were in answer to my question, and what parts where extras. I think I've managed to take the bits I need. Now the GUI works fine but the part that calls the JSyn methods is busted. I now get a whole heap of garbage in the console-

    Exception in thread "AWT-EventQueue-4" java.lang.NullPointerException
    at JPiano.mPress(JPiano.java:68)
    at JPiano.access$000(JPiano.java:9)
    at JPiano$1.mousePressed(JPiano.java:29)
    at java.awt.AWTEventMulticaster.mousePressed(AWTEvent Multicaster.java:221)
    at java.awt.Component.processMouseEvent(Component.jav a:5580)
    at javax.swing.JComponent.processMouseEvent(JComponen t.java:3135)
    at java.awt.Component.processEvent(Component.java:534 8)
    at java.awt.Container.processEvent(Container.java:201 0)
    at java.awt.Component.dispatchEventImpl(Component.jav a:4050)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2068)
    at java.awt.Component.dispatchEvent(Component.java:38 85)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4256)
    at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:3933)
    at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:3866)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2054)
    at java.awt.Component.dispatchEvent(Component.java:38 85)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 463)
    at java.awt.EventDispatchThread.pumpOneEventForHierar chy(EventDispatchThread.java:269)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:190)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:184)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:176)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:110)
    Exception in thread "AWT-EventQueue-4" java.lang.NullPointerException
    at JPiano.mRelease(JPiano.java:56)
    at JPiano.access$100(JPiano.java:9)
    at JPiano$2.mouseReleased(JPiano.java:40)
    at java.awt.AWTEventMulticaster.mouseReleased(AWTEven tMulticaster.java:232)
    at java.awt.Component.processMouseEvent(Component.jav a:5583)
    at javax.swing.JComponent.processMouseEvent(JComponen t.java:3135)
    at java.awt.Component.processEvent(Component.java:534 8)
    at java.awt.Container.processEvent(Container.java:201 0)
    at java.awt.Component.dispatchEventImpl(Component.jav a:4050)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2068)
    at java.awt.Component.dispatchEvent(Component.java:38 85)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4256)
    at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:3936)
    at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:3866)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2054)
    at java.awt.Component.dispatchEvent(Component.java:38 85)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 463)
    at java.awt.EventDispatchThread.pumpOneEventForHierar chy(EventDispatchThread.java:269)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:190)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:184)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:176)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:110)

    JPiano.java now looks like this:

    import java.awt.*;
    import java.awt.image.BufferedImage;
    import java.io.*;
    import java.net.URL;
    import javax.imageio.ImageIO;
    import javax.swing.*;
    import java.awt.event.*;

    public class JPiano extends JPanel
    {
    private BufferedImage image;
    private int state = 0, x, y;
    private note2 note;

    public JPiano()
    {
    loadImage();

    addMouseListener
    (
    new MouseAdapter()
    {
    public void mousePressed(MouseEvent me)
    {
    mPress(me.getX(), me.getY());
    }
    }
    );

    addMouseListener
    (
    new MouseAdapter()
    {
    public void mouseReleased(MouseEvent me)
    {
    mRelease();
    }
    }
    );

    }

    public void stop()
    {
    note.stop();
    }

    public void mRelease()
    {
    state = 0;
    repaint();
    note.release();
    }

    public void mPress(int X1, int Y1)
    {
    x = X1;
    y = Y1;
    state = 1;
    repaint();
    if (x >= 2 && x <= 22 && y >= 2 && y <= 114)
    {
    System.out.println("C1");
    note.change(32.70);
    }

    else if (x >= 22 && x <= 34 && y >= 70 && y <= 114)
    {
    System.out.println("C1");
    note.change(32.70);
    }


    else if (x >= 22 && x <= 48 && y >= 2 && y <= 70)
    {
    System.out.println("C#1");
    note.change(34.65);
    }

    else if (x >= 34 && x <= 48 && y > 70 && y <= 114)
    {
    System.out.println("D1");
    note.change(36.71);
    }

    else if (x >= 48 && x <= 56 && y >= 2 && y <= 114)
    {
    System.out.println("D1");
    note.change(36.71);
    }

    else if (x >= 56 && x <= 68 && y >= 70 && y <= 114)
    {
    System.out.println("D1");
    note.change(36.71);
    }

    else if (x >= 56 && x <= 80 && y >= 2 && y <= 70)
    {
    System.out.println("D#1");
    note.change(38.89);
    }

    else if (x >= 68 && x <= 80 && y >= 70 && y <= 114)
    {
    System.out.println("E1");
    note.change(41.20);
    }


    else if (x >= 68 && x <= 102 && y >= 2 && y <= 114)
    {
    System.out.println("E1");
    note.change(41.20);
    }

    else if (x >= 102 && x <= 124 && y > 2 && y <= 114)
    {
    System.out.println("F1");
    note.change(43.65);
    }

    else if (x >= 124 && x <= 136 && y >= 70 && y <= 114)
    {
    System.out.println("F1");
    note.change(43.65);
    }

    else if (x >= 124 && x <= 150 && y >= 2 && y <= 70)
    {
    System.out.println("F#1");
    note.change(46.25);
    }

    else if (x >= 136 && x <= 150 && y >= 70 && y <= 114)
    {
    System.out.println("G1");
    note.change(49.00);
    }


    else if (x >= 150 && x <= 158 && y >= 2 && y <= 114)
    {
    System.out.println("G1");
    note.change(49.00);
    }

    else if (x >= 158 && x <= 170 && y > 70 && y <= 114)
    {
    System.out.println("G1");
    note.change(49.00);
    }

    else if (x >= 158 && x <= 184 && y >= 2 && y <= 70)
    {
    System.out.println("G#1");
    note.change(51.91);
    }

    else if (x >= 170 && x <= 184 && y >= 2 && y <= 114)
    {
    System.out.println("A1");
    note.change(55.00);
    }

    else if (x >= 184 && x <= 192 && y >= 2 && y <= 114)
    {
    System.out.println("A1");
    note.change(55.00);
    }


    else if (x >= 192 && x <= 204 && y >= 70 && y <= 114)
    {
    System.out.println("A1");
    note.change(55.00);
    }

    else if (x > 192 && x <= 218 && y > 0 && y <= 70)
    {
    System.out.println("A#1");
    note.change(58.27);
    }

    else if (x >= 204 && x <= 218 && y >= 2 && y <= 114)
    {
    System.out.println("B1");
    note.change(61.74);
    }

    else if (x >= 218 && x <= 238 && y >= 2 && y <= 114)
    {
    System.out.println("B1");
    note.change(61.74);
    }

    else if (x >= 238 && x <= 260 && y >= 2 && y <= 114)
    {
    System.out.println("C2");
    note.change(65.41);
    }


    else if (x >= 260 && x <= 272 && y >= 70 && y <= 114)
    {
    System.out.println("C2");
    note.change(65.41);
    }

    else if (x >= 260 && x <= 286 && y > 2 && y <= 70)
    {
    System.out.println("C#2");
    note.change(69.30);
    }

    else if (x >= 272 && x <= 286 && y >= 70 && y <= 114)
    {
    System.out.println("D2");
    note.change(73.42);
    }

    else if (x >= 286 && x <= 296 && y >= 2 && y <= 114)
    {
    System.out.println("D2");
    note.change(73.42);
    }

    else if (x >= 296 && x <= 306 && y >= 70 && y <= 114)
    {
    System.out.println("D2");
    note.change(73.42);
    }


    else if (x >= 296 && x <= 318 && y >= 2 && y <= 70)
    {
    System.out.println("D#2");
    note.change(77.78);
    }

    else if (x >= 296 && x <= 318 && y > 70 && y <= 114)
    {
    System.out.println("E2");
    note.change(82.41);
    }

    else if (x >= 318 && x <= 340 && y >= 2 && y <= 114)
    {
    System.out.println("E2");
    note.change(82.41);
    }

    else if (x >= 340 && x <= 362 && y >= 2 && y <= 114)
    {
    System.out.println("F2");
    note.change(87.31);
    }

    else if (x >= 362 && x <= 374 && y >= 70 && y <= 114)
    {
    System.out.println("F2");
    note.change(87.31);
    }


    else if (x >= 362 && x <= 388 && y >= 2 && y <= 70)
    {
    System.out.println("F#2");
    note.change(92.50);
    }

    else if (x >= 374 && x <= 388 && y > 70 && y <= 114)
    {
    System.out.println("G2");
    note.change(98.00);
    }

    else if (x >= 388 && x <= 396 && y >= 2 && y <= 114)
    {
    System.out.println("G2");
    note.change(98.00);
    }

    else if (x >= 396 && x <= 408 && y >= 70 && y <= 114)
    {
    System.out.println("G2");
    note.change(98.00);
    }

    else if (x >= 396 && x <= 422 && y >= 2 && y <= 70)
    {
    System.out.println("G#2");
    note.change(103.83);
    }


    else if (x >= 408 && x <= 422 && y >= 70 && y <= 114)
    {
    System.out.println("A2");
    note.change(110.00);
    }

    else if (x >= 422 && x <= 430 && y > 2 && y <= 114)
    {
    System.out.println("A2");
    note.change(110.00);
    }

    else if (x >= 430 && x <= 442 && y >= 70 && y <= 114)
    {
    System.out.println("A2");
    note.change(110.00);
    }

    else if (x >= 430 && x <= 456 && y >= 2 && y <= 70)
    {
    System.out.println("A#2");
    note.change(116.54);
    }

    else if (x >= 442 && x <= 456 && y >= 70 && y <= 114)
    {
    System.out.println("B2");
    note.change(123.47);
    }


    else if (x >= 456 && x <= 476 && y >= 2 && y <= 114)
    {
    System.out.println("B2");
    note.change(123.47);
    note.attack();
    }

    note.attack();
    }

    public void paintComponent(Graphics g)
    {
    if (state == 1)
    {
    Color c = new Color(100,100,100);
    Color c2 = new Color(100,100,100);

    if (x >= 2 && x <= 22 && y >= 2 && y <= 114)
    {
    g.setColor(c);
    g.fillRect(2,2,20,111);
    g.fillRect(22,71,12,42);
    }

    else if (x >= 22 && x <= 34 && y >= 70 && y <= 114)
    {
    g.setColor(c);
    g.fillRect(2,2,20,111);
    g.fillRect(22,71,12,42);
    }


    else if (x >= 22 && x <= 48 && y >= 2 && y <= 70)
    {
    g.setColor(c2);
    g.fillRect(24,2,22,66);
    }

    else if (x >= 34 && x <= 48 && y > 70 && y <= 114)
    {
    g.setColor(c);
    g.fillRect(36,71,12,42);
    g.fillRect(48,2,8,111);
    g.fillRect(56,71,12,42);
    }

    else if (x >= 48 && x <= 56 && y >= 2 && y <= 114)
    {
    g.setColor(c);
    g.fillRect(36,71,12,42);
    g.fillRect(48,2,8,111);
    g.fillRect(56,71,12,42);
    }

    else if (x >= 56 && x <= 68 && y >= 70 && y <= 114)
    {
    g.setColor(c);
    g.fillRect(36,71,12,42);
    g.fillRect(48,2,8,111);
    g.fillRect(56,71,12,42);
    }

    else if (x >= 56 && x <= 80 && y >= 2 && y <= 70)
    {
    g.setColor(c2);
    g.fillRect(58,2,22,66);
    }

    else if (x >= 68 && x <= 80 && y >= 70 && y <= 114)
    {
    g.setColor(c);
    g.fillRect(70,71,12,42);
    g.fillRect(82,2,20,111);
    }


    else if (x >= 68 && x <= 102 && y >= 2 && y <= 114)
    {
    g.setColor(c);
    g.fillRect(70,71,12,42);
    g.fillRect(82,2,20,111);
    }

    else if (x >= 102 && x <= 124 && y > 2 && y <= 114)
    {
    g.setColor(c);
    g.fillRect(104,2,20,111);
    g.fillRect(124,71,12,42);
    }

    else if (x >= 124 && x <= 136 && y >= 70 && y <= 114)
    {
    g.setColor(c);
    g.fillRect(104,2,20,111);
    g.fillRect(124,71,12,42);
    }

    else if (x >= 124 && x <= 150 && y >= 2 && y <= 70)
    {
    g.setColor(c2);
    g.fillRect(126,2,22,66);
    }

    else if (x >= 136 && x <= 150 && y >= 70 && y <= 114)
    {
    g.setColor(c);
    g.fillRect(138,71,12,42);
    g.fillRect(150,2,8,111);
    g.fillRect(158,71,12,42);
    }


    else if (x >= 150 && x <= 158 && y >= 2 && y <= 114)
    {
    g.setColor(c);
    g.fillRect(138,71,12,42);
    g.fillRect(150,2,8,111);
    g.fillRect(158,71,12,42);
    }

    else if (x >= 158 && x <= 170 && y > 70 && y <= 114)
    {
    g.setColor(c);
    g.fillRect(138,71,12,42);
    g.fillRect(150,2,8,111);
    g.fillRect(158,71,12,42);
    }

    else if (x >= 158 && x <= 184 && y >= 2 && y <= 70)
    {
    g.setColor(c2);
    g.fillRect(160,2,22,66);
    }

    else if (x >= 170 && x <= 184 && y >= 2 && y <= 114)
    {
    g.setColor(c);
    g.fillRect(172,71,12,42);
    g.fillRect(184,2,8,111);
    g.fillRect(192,71,12,42);
    }

    else if (x >= 184 && x <= 192 && y >= 2 && y <= 114)
    {
    g.setColor(c);
    g.fillRect(172,71,12,42);
    g.fillRect(184,2,8,111);
    g.fillRect(192,71,12,42);
    }


    else if (x >= 192 && x <= 204 && y >= 70 && y <= 114)
    {
    g.setColor(c);
    g.fillRect(172,71,12,42);
    g.fillRect(184,2,8,111);
    g.fillRect(192,71,12,42);
    }

    else if (x > 192 && x <= 218 && y > 0 && y <= 70)
    {
    g.setColor(c2);
    g.fillRect(194,2,22,66);
    }

    else if (x >= 204 && x <= 218 && y >= 2 && y <= 114)
    {
    g.setColor(c);
    g.fillRect(206,71,12,42);
    g.fillRect(218,2,20,111);
    }

    else if (x >= 218 && x <= 238 && y >= 2 && y <= 114)
    {
    g.setColor(c);
    g.fillRect(206,71,12,42);
    g.fillRect(218,2,20,111);
    }

    else if (x >= 238 && x <= 260 && y >= 2 && y <= 114)
    {
    g.setColor(c);
    g.fillRect(240,2,20,111);
    g.fillRect(260,71,12,42);
    }


    else if (x >= 260 && x <= 272 && y >= 70 && y <= 114)
    {
    g.setColor(c);
    g.fillRect(240,2,20,111);
    g.fillRect(260,71,12,42);
    }

    else if (x >= 260 && x <= 286 && y > 2 && y <= 70)
    {
    g.setColor(c2);
    g.fillRect(262,2,22,66);
    }

    else if (x >= 272 && x <= 286 && y >= 70 && y <= 114)
    {
    g.setColor(c);
    g.fillRect(274,71,12,42);
    g.fillRect(286,2,8,111);
    g.fillRect(294,71,12,42);
    }

    else if (x >= 286 && x <= 296 && y >= 2 && y <= 114)
    {
    g.setColor(c);
    g.fillRect(274,71,12,42);
    g.fillRect(286,2,8,111);
    g.fillRect(294,71,12,42);
    }

    else if (x >= 296 && x <= 306 && y >= 70 && y <= 114)
    {
    g.setColor(c);
    g.fillRect(274,71,12,42);
    g.fillRect(286,2,8,111);
    g.fillRect(294,71,12,42);
    }


    else if (x >= 296 && x <= 318 && y >= 2 && y <= 70)
    {
    g.setColor(c2);
    g.fillRect(296,2,22,66);
    }

    else if (x >= 296 && x <= 318 && y > 70 && y <= 114)
    {
    g.setColor(c);
    g.fillRect(308,71,12,42);
    g.fillRect(320,2,20,111);
    }

    else if (x >= 318 && x <= 340 && y >= 2 && y <= 114)
    {
    g.setColor(c);
    g.fillRect(308,71,12,42);
    g.fillRect(320,2,20,111);
    }

    else if (x >= 340 && x <= 362 && y >= 2 && y <= 114)
    {
    g.setColor(c);
    g.fillRect(342,2,20,111);
    g.fillRect(362,71,12,42);
    }

    else if (x >= 362 && x <= 374 && y >= 70 && y <= 114)
    {
    g.setColor(c);
    g.fillRect(342,2,20,111);
    g.fillRect(362,71,12,42);
    }


    else if (x >= 362 && x <= 388 && y >= 2 && y <= 70)
    {
    g.setColor(c2);
    g.fillRect(364,2,22,66);
    }

    else if (x >= 374 && x <= 388 && y > 70 && y <= 114)
    {
    g.setColor(c);
    g.fillRect(376,71,12,42);
    g.fillRect(388,2,8,111);
    g.fillRect(396,71,12,42);
    }

    else if (x >= 388 && x <= 396 && y >= 2 && y <= 114)
    {
    g.setColor(c);
    g.fillRect(376,71,12,42);
    g.fillRect(388,2,8,111);
    g.fillRect(396,71,12,42);
    }

    else if (x >= 396 && x <= 408 && y >= 70 && y <= 114)
    {
    g.setColor(c);
    g.fillRect(376,71,12,42);
    g.fillRect(388,2,8,111);
    g.fillRect(396,71,12,42);
    }

    else if (x >= 396 && x <= 422 && y >= 2 && y <= 70)
    {
    g.setColor(c2);
    g.fillRect(398,2,22,66);
    }


    else if (x >= 408 && x <= 422 && y >= 70 && y <= 114)
    {
    g.setColor(c);
    g.fillRect(410,71,12,42);
    g.fillRect(422,2,8,111);
    g.fillRect(430,71,12,42);
    }

    else if (x >= 422 && x <= 430 && y > 2 && y <= 114)
    {
    g.setColor(c);
    g.fillRect(410,71,12,42);
    g.fillRect(422,2,8,111);
    g.fillRect(430,71,12,42);
    }

    else if (x >= 430 && x <= 442 && y >= 70 && y <= 114)
    {
    g.setColor(c);
    g.fillRect(410,71,12,42);
    g.fillRect(422,2,8,111);
    g.fillRect(430,71,12,42);
    }

    else if (x >= 430 && x <= 456 && y >= 2 && y <= 70)
    {
    g.setColor(c2);
    g.fillRect(432,2,22,66);
    }

    else if (x >= 442 && x <= 456 && y >= 70 && y <= 114)
    {
    g.setColor(c);
    g.fillRect(444,71,12,42);
    g.fillRect(456,2,20,111);
    }


    else if (x >= 456 && x <= 476 && y >= 2 && y <= 114)
    {
    g.setColor(c);
    g.fillRect(444,71,12,42);
    g.fillRect(456,2,20,111);
    }
    }


    else if(state == 0)
    {
    super.paintComponent(g);
    g.drawImage(image,0,0,this);
    }
    }


    public void loadImage()
    {
    String path = "piano.jpg";
    try
    {
    URL url = getClass().getResource(path);
    image = ImageIO.read(url);
    }
    catch(IOException e)
    {
    System.out.println("Read error: " + e.getMessage());
    }
    }
    }

  4. #4
    jamesfrize is offline Member
    Join Date
    Mar 2008
    Posts
    19
    Rep Power
    0

    Default

    I've managed to sort it out! It's not quite how I intended it, would have been nice to keep the JPiano as a separate component. I've just put all the code in the main class that extends JApplet and the whole thing works now.
    Have a look if ya want Sonodrome.
    Thanks for the advice, this Java stuff is impenetrable! :{p

Similar Threads

  1. Adding and removing panels dynamically
    By kbyrne in forum AWT / Swing
    Replies: 1
    Last Post: 04-12-2008, 08:28 PM
  2. Buttons to show new panels
    By Lehane_9 in forum AWT / Swing
    Replies: 1
    Last Post: 03-06-2008, 04:22 PM
  3. Working with Labels on Panels.
    By vargihate in forum AWT / Swing
    Replies: 2
    Last Post: 01-04-2008, 04:09 AM
  4. Need a tutorial for Studying about Panels
    By ramachandran in forum New To Java
    Replies: 1
    Last Post: 10-25-2007, 09:05 AM
  5. Problems to show images in applets
    By Felissa in forum Java Applets
    Replies: 1
    Last Post: 07-06-2007, 09:12 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
  •