0

According to the event delegation model i have taken one handler class.

package simple;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class ButtonHandler implements ActionListener {

    MainFrame frame;

    public ButtonHandler(MainFrame frame) {
        this.frame = frame;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        MyThread thread = new MyThread(frame);
        Thread mthread = new Thread(thread);            
        mthread.start();
    }    
}

,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,

One MainFrame Class for frame creation

package simple;

import java.awt.Graphics;    
import javax.swing.JButton;
import javax.swing.JFrame;

@SuppressWarnings("serial")
public class MainFrame extends JFrame {
    JButton btnStart = new JButton("Start");
    int xPos1,xPos2;    
    public MainFrame()
    {           
        setSize(700, 600);
        setLayout(null);
        setVisible(true);           
        xPos1=10;
        xPos2=600;
        btnStart.setBounds(590, 30, 100, 30);
        add(btnStart);
        btnStart.addActionListener(new ButtonHandler(this));            
        paint(null);            
        setDefaultCloseOperation(EXIT_ON_CLOSE);            
    }

    @Override
    public void paint(Graphics g) {
        g.drawString("Seed", xPos1, 50);
        g.drawString("Infotech", xPos2, 550);
    }
}

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

one Thread Class which implements runnable interface

package simple;

public class MyThread implements Runnable {

    MainFrame frame;        

    public MyThread(MainFrame frame) {
        super();
        this.frame = frame;
    }

    @Override
    public void run() {

        while(true)
        {
            frame.repaint();
            try {
                frame.xPos1++;
                frame.xPos2--;
                Thread.sleep(10);
            } catch (InterruptedException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }               
        }
    }    
}

////////////////////////////////////////////////////////////// and one class for just main method

package simple;

public class Test {

    public static void main(String[] args) {
        new MainFrame();
    }
}

Can anyone tell what is the problem in that I am new to the java programming concepts.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
abhi3232
  • 371
  • 1
  • 4
  • 15
  • Don't block the EDT (Event Dispatch Thread) - the GUI will 'freeze' when that happens. Instead of calling `Thread.sleep(n)` implement a Swing `Timer` for repeating tasks or a `SwingWorker` for long running tasks. See [Concurrency in Swing](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/) for more details. – Andrew Thompson Oct 16 '13 at 19:38
  • See the `WorldView` class in [this answer](http://stackoverflow.com/a/18825844/418556) for how to create an animation. And for better help sooner, post an [SSCCE](http://sscce.org/) (e.g. as seen in the linked answer). – Andrew Thompson Oct 16 '13 at 19:41
  • Just as a side note, a sleep period of 10 milliseconds is 100 fps, for something so simple, that might be a little too much. Something like 40 milliseconds (about 25fps) might result in better performance, as the system as more time to flush out it's queue...IMHO – MadProgrammer Oct 16 '13 at 20:04

1 Answers1

1
  1. Overriding paint of a top level container (JFrame)
  2. Not calling super.paint(g);
  3. Calling paint(null)
  4. setLayout(null)

Star by taking a look at Performing Custom Painting and Laying out components in a container

Instead of using a Thread, you may find a javax.swing.Timer more useful. Take a look at Concurrency in Swing for more details

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366