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

