Easy updating of picture with threads should finish today

Hi!

I am really thankful if you can help me.

My program issupposted to show a clickable picture of number one and if you click thepicture it should change to number two.

If you run this program clicking will not work.

I try to reference to the old picture by

JComponent sourceEvent = (JComponent)e.getSource();

sourceEvent.add(panel2);

Perhaps this is not the most elegant way to do it but since I am very slow in programming I would like use at least something similar approach.

So I have createdpicture by using labels and panels and I would like to update their contents.

Later I would like to add parallel playing of midi notes in to my program.

I am happy if you can also give some advice how to add this midi playing in synchronous way to these threads.

Thank you very much

Partly functional source code:

import java.awt.*;

import java.awt.event.*;

import java.awt.datatransfer.*;

import javax.swing.*;

import java.io.*;

import java.util.Scanner;

import java.lang.Math.*;

publicclass Saiekokeiluextends JPanelimplements MouseListener{

JPanel panel;

publicstaticvoid main(String[] args)throws IOException

{

javax.swing.SwingUtilities.invokeLater(new Runnable(){

publicvoid run(){

createAndShowMyGUI();

}

});

}

privatestaticvoid createAndShowMyGUI(){

JFrame frame =new JFrame("Saiekokeilu");

frame.setExtendedState(Frame.MAXIMIZED_BOTH);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JComponent newContentPane =new Saiekokeilu();

newContentPane.setOpaque(true);

frame.setContentPane(newContentPane);

frame.pack();

frame.setVisible(true);

}

public Saiekokeilu(){

super(new GridLayout(0,1));

ImageIcon icon =new ImageIcon("num_1.gif");// huom. icon

JLabel label =new JLabel();

label.setIcon(icon);

JPanel panel =new JPanel();

panel.add(label);

add(panel);

label.addMouseListener(this);

}

void eventOutput(String eventDescription, MouseEvent e){

System.out.println("testing" + e.getSource());

}

publicvoid mousePressed(MouseEvent e){

ImageIcon icon2 =new ImageIcon("num_2.gif");

JLabel label2 =new JLabel();

label2.setIcon(icon2);

JPanel panel2 =new JPanel();

panel2.add(label2);

JComponent sourceEvent = (JComponent)e.getSource();

sourceEvent.add(panel2);

panel2.updateUI();

/*

e.getComponent().getClass().getName().add(label2);

*/

eventOutput("Mouse pressed (# of clicks: "

+ e.getClickCount() +")", e);

}

publicvoid mouseReleased(MouseEvent e){

eventOutput("Mouse released (# of clicks: "

+ e.getClickCount() +")", e);

}

publicvoid mouseEntered(MouseEvent e){

eventOutput("Mouse entered", e);

}

publicvoid mouseExited(MouseEvent e){

eventOutput("Mouse exited", e);

}

publicvoid mouseClicked(MouseEvent e){

eventOutput("Mouse clicked (# of clicks: "

+ e.getClickCount() +")", e);

}

}// end class Saiekokeilu

[5911 byte] By [wonderful123a] at [2007-11-27 10:42:04]
# 1

It's not enough to create a label or panel, you have to actually put it somewhere for display.

May I suggest having a mouse listener that simply sets a new image for the old label and updates (repaints? revalidates?) it?

CeciNEstPasUnProgrammeura at 2007-7-28 19:16:16 > top of Java-index,Java Essentials,Java Programming...
# 2

thanks for fast comments!!

i would really appreciate if you can give some more details.

For ex. a scetch how you would build the methods and classes.

What i should change..

Thank you very much!

wonderful123a at 2007-7-28 19:16:17 > top of Java-index,Java Essentials,Java Programming...
# 3

Just the implementation of your listener (for which you will need to keep a reference to the JLabel to alter as a member attribute in your class).

CeciNEstPasUnProgrammeura at 2007-7-28 19:16:17 > top of Java-index,Java Essentials,Java Programming...
# 4

thanks. sorry to bother but I am so newbie that i cannot yet completly manage.

could you still kindly explain what are the most critical lines that i should change in my program?

thank you very much!!

wonderful123a at 2007-7-28 19:16:17 > top of Java-index,Java Essentials,Java Programming...
# 5

please, help needed

wonderful123a at 2007-7-28 19:16:17 > top of Java-index,Java Essentials,Java Programming...
# 6

do what he says.

1) make your label variable a class variable so that your mouse events can "see" it. That way you can update the label's icon inside the mouse click event.

2) I'd do the same with the JFrame so you can repeat a "pack()" command after updating the icon. I assume the pictures are different sized and pack will adjust the size of the frame accordingly.

3) I'd make an array of Strings of path-file names to your pictures. (I called mine imageStrings). I'd also have an int var called imageCount that would be used as an index to this array. Every time you click on the panel, imageCount increments (mod it by the array.length to allow it to loop back to zero) and create a new icon from the imageStrings[imageCount].

Good luck

petes1234a at 2007-7-28 19:16:17 > top of Java-index,Java Essentials,Java Programming...
# 7

thank you very much! your help was very valuable for me!!!

-wonderful-

wonderful123a at 2007-7-28 19:16:17 > top of Java-index,Java Essentials,Java Programming...