- Joined
- Aug 11, 2007
- Messages
- 2
- Reaction score
- 0
Can someone please tell me what is wrong in this java code?
--------------------------------------------------------------------------------
/* The program is intended to start animating text at the click of a button, pause it at another click and resume at the next click. It should continue like this */
import javax.swing.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
public class TextAnime implements Runnable
{
JFrame frame;
boolean flag;
Thread animeThread;
JLabel label;
String[] textArray;
public TextAnime()
{
flag = false;
animeThread = new Thread(this);
frame = new JFrame("Animate Text");
GridBagLayout gbl = new GridBagLayout();
GridBagConstraints gbc = new GridBagConstraints();
frame.setLayout(gbl);
JButton button = new JButton("Start");
label = new JLabel("Stopped");
textArray = new String[5];
String textArray1[] = {"Programmer", "SportsMan", "Genius", "Friend", "Knowledgable"};
for(int ctr = 0; ctr<5 ; ctr++)
{
textArray[ctr] = textArray1[ctr];
}
gbc.weightx = 1;
gbc.gridx = 0;
gbl.setConstraints(button,gbc);
frame.getContentPane().add(button);
ButList bl = new ButList();
button.addActionListener(bl);
gbc.gridx = 1;
gbl.setConstraints(label,gbc);
frame.getContentPane().add(label);
frame.setSize(200,150);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
frame.setVisible(true);
}
public class ButList implements ActionListener
{
public void actionPerformed(ActionEvent evt)
{
if(flag == false)
{
flag = true;
animeStart();
}
else
{
flag = false;
//animeStop();
}
}
}
public synchronized void animeStart()
{
animeThread.start();
Thread newThread;
newThread = Thread.currentThread();
newThread.notify();
}
public void animeStop()
{
animeThread.interrupt();
}
public void run()
{
int i = 0;
try
{
while(i == i)
{
if(i==5)
{
i=0;
}
label.setText(textArray);
animeThread.sleep(1000);
i++;
if (flag == false)
{
animeThread.wait();
}
}
}
catch(InterruptedException ie)
{
label.setText("Stopped");
}
}
public static void main(String args[])
{
TextAnime ta = new TextAnime();
}
}
Please tell me if this can be written in a more simpler manner. Also please correct this code. I am tired after trying many times.
--------------------------------------------------------------------------------
/* The program is intended to start animating text at the click of a button, pause it at another click and resume at the next click. It should continue like this */
import javax.swing.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
public class TextAnime implements Runnable
{
JFrame frame;
boolean flag;
Thread animeThread;
JLabel label;
String[] textArray;
public TextAnime()
{
flag = false;
animeThread = new Thread(this);
frame = new JFrame("Animate Text");
GridBagLayout gbl = new GridBagLayout();
GridBagConstraints gbc = new GridBagConstraints();
frame.setLayout(gbl);
JButton button = new JButton("Start");
label = new JLabel("Stopped");
textArray = new String[5];
String textArray1[] = {"Programmer", "SportsMan", "Genius", "Friend", "Knowledgable"};
for(int ctr = 0; ctr<5 ; ctr++)
{
textArray[ctr] = textArray1[ctr];
}
gbc.weightx = 1;
gbc.gridx = 0;
gbl.setConstraints(button,gbc);
frame.getContentPane().add(button);
ButList bl = new ButList();
button.addActionListener(bl);
gbc.gridx = 1;
gbl.setConstraints(label,gbc);
frame.getContentPane().add(label);
frame.setSize(200,150);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
frame.setVisible(true);
}
public class ButList implements ActionListener
{
public void actionPerformed(ActionEvent evt)
{
if(flag == false)
{
flag = true;
animeStart();
}
else
{
flag = false;
//animeStop();
}
}
}
public synchronized void animeStart()
{
animeThread.start();
Thread newThread;
newThread = Thread.currentThread();
newThread.notify();
}
public void animeStop()
{
animeThread.interrupt();
}
public void run()
{
int i = 0;
try
{
while(i == i)
{
if(i==5)
{
i=0;
}
label.setText(textArray);
animeThread.sleep(1000);
i++;
if (flag == false)
{
animeThread.wait();
}
}
}
catch(InterruptedException ie)
{
label.setText("Stopped");
}
}
public static void main(String args[])
{
TextAnime ta = new TextAnime();
}
}
Please tell me if this can be written in a more simpler manner. Also please correct this code. I am tired after trying many times.