repainting

I have some code which paints a guitar fretboard onto the screen and then the notes inside thier frets. All these are jpg images which are set using the setIcon method of JLabel. This has been working fine.

I now have a JComboBox which has different tuning notes for the guitar which is attached to an actionListener that changes the tuning, here it should repaint the screen to show the adapted note sequence but I'm having a problem repainting the screen, I'm not using any layout manager here, here's the code segments:

/*Method for drawing the Note images onto the screen*/

publicvoid paintTuningNotes(){

Iterator iter=fretboardMap.iterator();

int stringCount=0;

while(iter.hasNext()){

GuitarString guitarString=(GuitarString)iter.next();

int i=0;

Iterator stringIter=(guitarString.getStringNotes()).iterator();

while(stringIter.hasNext() && i < 12){

ImageIcon note=new ImageIcon();

Note theNote=(Note)stringIter.next();

note=createImageIcon(theNote.getImage());

JLabel noteLabel=new JLabel();

noteLabel.setIcon(note);

fretboardPanel.add(noteLabel);

int offset=((104-((i+2)*3))/2)+(10-(i*2)+(i/2));

System.out.println(offset);

int fretwidth=104-(i*3);

int x=((i+1)*(fretwidth)+50)-offset;

int y=(stringCount*21)+40;

noteLabel.setBounds(x,y,21,21);

i++;

}

stringCount++;

}

}

/*Listener attached to the ComboBox*/

publicclass TuningListenerimplements ActionListener{

publicvoid actionPerformed(ActionEvent e){

Tuning selectedTuning=(Tuning)tunings.get(tuning.getSelectedIndex());

System.out.println(selectedTuning.getTuningName());

fbl.setTuning(selectedTuning);

fretboardPanel.invalidate();

fretboardPanel.repaint();

}

}

/*Overridden paint method (Not too sure on this!)*/

publicvoid paint(Graphics g){

super.paint(g);

paintTuningNotes();

}

I appreciate any help. Cheers

Craig

[3110 byte] By [shok_411a] at [2007-10-2 16:45:08]
# 1

What exactly is failing?

My first suggestion is to move paintTuningNotes() into your action listener. As it is now, every time the paint method is called, paintTuningNotes() will get called, which adds more and more Labels to the fretBoardPanel. Only override the paint method when you want to do your own custom drawing, which you are not, you are using JLabels. Since you don't even use Graphics g in paint (except super), then you aren't doing any custom painting.

You could use your current design, but instead of putting a label on the fretboardPanel, use g.drawImage to paint the ImageIcon. That would also require passing Graphics g into paintTuningNotes().

robtafta at 2007-7-13 17:55:12 > top of Java-index,Java Essentials,Java Programming...
# 2

Thanks for your response. After a little playing I think I've discovered the problems. One was a program error you would not have been able to see from the code fragment I gave. The other is to do with the order of the images i.e. they are painted underneath the previous image.

Instead I've placed all my JLabels into an ArrayList and will be working back through it and updating the JLabel icons when the images need to change. I think that will work best, if not then I may take your suggestion and use the Graphics drawImage method.

Cheers

Craig

shok_411a at 2007-7-13 17:55:12 > top of Java-index,Java Essentials,Java Programming...