Results 1 to 7 of 7
- 11-27-2010, 04:43 PM #1
Member
- Join Date
- Nov 2010
- Posts
- 4
- Rep Power
- 0
Using threads in a graphical program
This is about threads but I posted here because it's really basic.
So I've read a tutorial on concurrency but I'm not sure where exactly I should use threads within the program. I'm not looking for code just an explanation.
I've been messing around with this application to try things out. It's just an extension of a JPanel that draws squares when you click, but let's say first you clicked all your points, then pressed a button that did all the drawing. How could I make that a thread, so then someone else could click points on the canvas and press the button when they wanted to get their squares (in a separate thread)?
-
- 11-27-2010, 05:07 PM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,392
- Blog Entries
- 7
- Rep Power
- 17
Note, if you're using Swing or AWT there's already a Thread running: Swing's or AWT't event dispatching thread. When your MouseListener or ActionListener receicves an event it runs in that event dispatching thread (EDT). Your 'rectangle drawing' method or class object isn't stateless: it receives points and when it's ready it dumps a representation ready for drawing in the paintComponent( ... ) method that also runs in the EDT. That method is called by Swing (or AWT) when you call the repaint() method. So careful scheduling should do the job without introducing yet another thread.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 11-27-2010, 05:10 PM #4
Member
- Join Date
- Nov 2010
- Posts
- 4
- Rep Power
- 0
Well mutlithreading isn't necessary I'm just practicing here.
So you have a JPanel, you can click and those coordinates are stored. Then you can click a button which creates a thread that performs the actual drawing of the squares. Then someone else can click some more, press the button again and new squares are created in a different thread.
This link is safe everyone it's from tech recipes.
-
As Josh notes, all code that occurs in Swing itself, such as responding to mouse clicks or button presses, and all painting in the GUI must be done on the EDT, or Event Dispatch Thread, Swing's main thread responsible for these things. You'll usually use a background thread when you want to perform a long process off of the GUI such as when you want to upload a large image file or access a database.
But if you want to modify the file in the link, go ahead and give it a try, and then please feel free to post your attempt here.
-
Here's some code I dug up that shows what happens when you try to do a long-running process on the EDT versus on a background thread:
Java Code:import java.awt.Dimension; import java.awt.Toolkit; import java.awt.Window; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; import javax.swing.*; public class TestProgressBar { private static void createAndShowUI() { JFrame frame = new JFrame("TestProgressBar"); frame.getContentPane().add(new TestPBGui().getMainPanel()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } public static void main(String[] args) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { createAndShowUI(); } }); } } class TestPBGui { private JPanel mainPanel = new JPanel(); public TestPBGui() { JButton noBackgroundThreadBtn = new JButton("Progress Bar w/out Background Thread"); JButton backgroundThreadBtn = new JButton("Progress Bar with Background Thread"); noBackgroundThreadBtn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { noBackgroundThreadActionPerformed(); } }); backgroundThreadBtn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { backgroundThreadActionPerformed(); } }); mainPanel.add(noBackgroundThreadBtn); mainPanel.add(backgroundThreadBtn); } private void noBackgroundThreadActionPerformed() { Window thisWin = SwingUtilities.getWindowAncestor(mainPanel); final JDialog progressDialog = new JDialog(thisWin, "Uploading..."); createProgressBar(progressDialog); Task task = new Task("No background thread"); task.execute(); progressDialog.setVisible(true); while (!task.isDone()) { } progressDialog.dispose(); } private void backgroundThreadActionPerformed() { Window thisWin = SwingUtilities.getWindowAncestor(mainPanel); final JDialog progressDialog = new JDialog(thisWin, "Uploading..."); final JProgressBar bar = createProgressBar(progressDialog); final Task task = new Task("Background Thread"); task.addPropertyChangeListener(new PropertyChangeListener() { public void propertyChange(PropertyChangeEvent evt) { if (evt.getPropertyName().equalsIgnoreCase("progress")) { int progress = task.getProgress(); if (progress == 0) { bar.setIndeterminate(true); } else { bar.setIndeterminate(false); progressDialog.dispose(); } } } }); task.execute(); progressDialog.setVisible(true); } private JProgressBar createProgressBar(final JDialog progressDialog) { JPanel contentPane = new JPanel(); contentPane.setPreferredSize(new Dimension(300, 100)); final JProgressBar bar = new JProgressBar(0, 100); bar.setIndeterminate(true); contentPane.add(bar); progressDialog.setContentPane(contentPane); progressDialog.pack(); progressDialog.setLocationRelativeTo(null); return bar; } public JPanel getMainPanel() { return mainPanel; } } class Task extends SwingWorker<Void, Void> { private static final long SLEEP_TIME = 4000; private String text; public Task(String text) { this.text = text; } @Override public Void doInBackground() { setProgress(0); try { // imitate a long-running task Thread.sleep(SLEEP_TIME); } catch (InterruptedException e) { } setProgress(100); return null; } @Override public void done() { System.out.println(text + " is done"); Toolkit.getDefaultToolkit().beep(); } }
- 11-28-2010, 03:52 PM #7
Member
- Join Date
- Nov 2010
- Posts
- 4
- Rep Power
- 0
Similar Threads
-
Graphical Java Developer
By ALATECjobs in forum Jobs OfferedReplies: 2Last Post: 09-28-2009, 11:24 PM -
Graphical query
By Alex j in forum IntroductionsReplies: 0Last Post: 04-07-2009, 06:48 AM -
Graphical view of an array
By ventrue in forum New To JavaReplies: 2Last Post: 02-13-2009, 07:31 PM -
Graphical Ftp client
By Java Tip in forum java.netReplies: 0Last Post: 04-07-2008, 08:10 PM -
Help about how to implement a graphical editor in java
By xiul in forum Java 2DReplies: 0Last Post: 11-29-2007, 08:19 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks