I have a class Banner that extends JFrame implements ActionListener and is responsible for thread handling via actionPerfmored.
"launch banner", "pause banner" works. "resume banner" seems to be dead alltogether
import java.awt.*;
import javax.swing.*;
public class BannerP extends JPanel implements Runnable {
int width, height, xPos=10;
boolean stopT;
public BannerP(int width, int height) {
this.setPreferredSize(new Dimension(width, height/3));
this.width = width;
this.height = height;
}
public void run() {
while(true) {
if(stopT) {
synchronized (this) {
while(stopT) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}//while
}//sync
} //if
if(xPos<=width)xPos+=5;
else xPos=0;
repaint();
try {
Thread.sleep(20);
} catch (InterruptedException e) {
e.printStackTrace();
}
}// end while
} //end run
public void paint(Graphics g) {
super.paint(g);
g.setFont(new Font("Arial", Font.BOLD, 20));
g.drawString("banner", xPos, height/3);
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.Thread;
public class Banner extends JFrame implements ActionListener {
public JPanel up, middle, bott;
public BannerP bnrP;
private JButton bnr[], tmr[];
private Thread bnrT, tmrT;
public Banner() {
this.setSize(600, 500);
up = new JPanel();
up.setLayout(new FlowLayout(FlowLayout.CENTER));
bnr = new JButton[3];
bnr[0] = new JButton("launch banner");
bnr[1] = new JButton("pause banner");
bnr[2] = new JButton("resume banner");
bnr[0].addActionListener(this);
bnr[1].addActionListener(this);
bnr[2].addActionListener(this);
up.add(bnr[0]);
up.add(bnr[1]);
up.add(bnr[2]);
this.getContentPane().add(BorderLayout.PAGE_START, up);
middle = new JPanel(new FlowLayout(FlowLayout.CENTER));
bnrP = new BannerP(this.getWidth(), this.getHeight());
bnrT = new Thread(bnrP);
middle.add(bnrP);
this.getContentPane().add(BorderLayout.CENTER, middle);
//-------------------
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
} //end Banner
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand()=="launch banner") { bnrT.start(); bnr[0].setEnabled(false); }
else if(e.getActionCommand()=="pause banner") { bnrP.stopT = true; }
else if(e.getActionCommand()=="resume banner") {
bnrP.stopT = false;
synchronized(bnrT) {bnrT.notify(); }
}
}
public static void main(String agrs[]) {
Banner banner = new Banner();
}
}