Here I want to make a simple program where a Ball keep bouncing forever, then I add a MouseClick Listener to stop and resume thread (I mean, to stop moving Ball and to resume again). The problem is I can stop the thread using wait(), But I can not resume it. Below is the code
public class TestResumePauseBall {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public synchronized void run() {
Field aField = new Field();
[code]....
At first I think that notify() must be called from another active thread. So I add a 'class Timing implements Runnable'. Then I call notifyAll() from class Timing. But still it was failed.
After wait() called on Ball's thread, the whole frame also freeze. I can not understand why this happen?
If this test success, I will add 1 one more ball and add keyboard keys as controller of ball's movement.
I have a JTable that is within a JScrollPane. If I try to write add a MouseListener to the JTable it never fires. However, a mouse event does fire for the JScrollPane which it is added to. Is there a way to prevent the JScrollPane from capturing the event or passing it along to the JTable?
I'm currently learning about Swing but I can't get my head round this piece of the code. Here is a simplified gui (not interested in the gui part but the execution)
public class SwingDemo implements ActionListener { SwingDemo(){ JFrame jfrm = new JFrame("Simple gui pro"); //rest of code public static void main(String[] args) { new SwingDemo(); }
I get the above, create a new instance of SwingDemo in the main thread which starts up the gui through the constructor. However, then the tutorial says that I should avoid doing the above but do this instead:
public class SwingDemo implements ActionListener { SwingDemo(){ JFrame jfrm = new JFrame("Simple gui pro"); //rest of code public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { //why do this instead? public void run(){ new SwingDemo(); } }); } }
Reading, it talks about an event-dispatching thread which has completely lost me... Why not just instantiate the object directly instead of creating another thread?
i have two different buttons. when i clicked first, the picture is shown and then when clicked second button it shown picture and both of will be closed. but i cant see second picture because immediately second button will be closed. how can i stopped second button for a 3 seconds fpr example?
im having an issue with the 3rd thread that are supposed to merge the two sorted sub arrays , i pass the 2 subarrays to my runnable function sortlist and they are renamed IntSortList 1 and 2 and th1.start() and th1.join() are called and it works fine, but then i have another runnable constructor that takes IntSortList 1 and 2 but it does take a runnable. below is the code in my main,
Runnable InSortlist1 = new sortList(data2p1); Runnable InSortlist1 = new sortList(data2p1); Thread th1 = new Thread (IntSortlist1); Thread th2 = new Thread (IntSortlist2); try { th1.start(); th1.join();
Am doing a JSP project for maintaining the details of the interview candidates so now I want to know "How to read the contents of the resume and store in the database"
I've been learning Java in school for lika a year now. Our teacher wanted us to make a game called "Game of Life" .Now it's not very optimized because the only type of GUI we know how to make it with JButtons so if I want a 100x100 grid that means the program has to load 10000 buttons and change text and color on them which I can imagen cause lags and so on but if we ignore that lagg our teacher wanted us to make it loop-able and well I kinda succeeded but the only problem is that I can't get it to wait between every loop so if I tell the program to loop 100 times, the program processes that and that takes forever and then updates the board and sometimes it takes forever and sometimes you cant even see the "cells" moving. Is it possible to get my program to loop and if so how? Here is the code:
What I want to do is very clear, just play sound and pause it. The problem is that I can't pause the music. When I click the pause button music keeps playing .I have been working on for 2 hours. I can't find the mistake; what am I doing wrong?
public class MainC extends JFrame implements Runnable,ActionListener { private JPanel contentPane; private JTextField txtname; JButton btnopen; JButton btnSave; JButton btnplay;
I am coding a game and I am trying to implement a pause and unpause button. in the BoardPanel class I've added the button's 'p' and 'o' to game.wait() and game.notify() (main code is in game class). however the game.wait is having issues and even if i throw interrupted exception or try/catch it doesn't work. I'd like to code it to a single button if at all possible.
In my card game applet I have to play some music and the user must have the ability to play, stop and pause the song using buttons. I got the play and stop to work easy but I'm unable to add the pause option/method.
I want to try and run a thread that starts running on start and quits after pressing a key.I cant seem to detect keypresses. ci cant seem to add code: Post denied. New posts are limited by number of URLs it may contain and checked if it doesn't contain forbidden words.
its just a code snippit without links or anything..this is a small part... but how do i add a keyListener in here that listends to a random key?The class implements KeyListener and the overrided method "keyPressed" sets the isRunning boolean to false... but the keyPressed method is never executed.
public static void main(String[] args) { Thread thread = new Thread() { public void run() { while(isRunning){ System.out.println("running"); try { sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }; thread.start(); }
public class pracDraw extends JFrame { private Color red=Color.red; public int i; private Color white = Color.white; JPanel pr=new JPanel(); JTextField t=new JTextField();
[code]....
If u run it you can see that the JTextField (t1) is drawn on the panel along the line but it does so with a gray border. How do i eliminate that grey border and only draw the text field along the line and finally make the text field green after it reaches the end of line?
I have a program ive been working on and it works, but the flags in the runnables seem to shift the output down 1. for instance i commented playa3. start(); to see whats going on, if i just run playa1 i got no issues, as soon as i enable playa2, i get this output:
Game continues... Dealer places King and Queen on the table.
Game continues... Dealer places Queen and Ace on the table. Player one with 'Ace' places his card on the table. Player one with 'Ace' wins the current deal. King Queen
Game continues... Dealer places Queen and King on the table. Player two with 'King' places his card on the table. Player two with 'King' wins the current deal. Queen Ace
the second time the dealer deals, player one places his cards on the table when he was supposed to do that for the first deal which he didnt. the last line there, (king queen) is just a print statement that is referring to the dealers hand that, that iteration is responding too. below is my code, i
public class P5 { public static int i, count, dealerFirstCard, dealerSecondCard, player1, player2, player3; public static String cardSet [] = {"Ace","King", "Queen"}; public static volatile boolean dealerFinished= false; public static volatile boolean playersFinished= false;
From within that thread I try to call a method from within the same class the contain the thread. This method is not being called. I have confirmed that that section of code is being executed in the thread.
If you look at the following code, the thread being run is called MainThread ln 114, and the method it calls that is not being executed is called onprogressTime() ln 105;
I start my thread, it's for a real basic game I'm learning. Everything was working fine, until I got to recognizing keys. It runs, and I can close using the mouse on the close command, but the keys aren't being generated from the keyboard. I copied it to a working sample, and here are the two files.
The idea was to set the return value to true if any key is pressed, thus quitting, for this short sample program. Problem, it never recognizes any keys pressed on the keyboard. New to java and threading.
I am new to java and still learning. I am trying to execute the code below which compiles successfully, but getting the following error:"Exception in thread "main" java.lang.NoSuchMethodError: main"
Code:
class Dog{ int size; String name; String breed; void bark(){ System.out.println("woof woof"); } } class DogTestDrive{
I have objects (baddies in a game) that have individual clocks/counters assigned to them (when I make an object, I fill an integer based array list with a new digit entry at a value of one) that all happens in threads. then in another thread, I am checking the whole list of clocks, to see if any are equal to a wanted value , then I am taking an action and reseting the clock at that point on the array list.
now, the problem is:even though my code says " yes, I've taken that action because the value of array position x was >= specified value and I will now reset the clock" it doesn't reset the clock. Here is what it looks like
*these are excerpts taken from a really big program, however they should stand on their own as they are individual classes
Java Code:
public class levelclock extends Thread { public void run() { while (Run2 == true) { if (Run == false) { try { Thread.sleep(100); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace();
[code]....
(Attached, just open up the zip and read the .txt)
I created a main class called X and two Y and Z classes.
Y and Z implements Runnable classes.
class X contains a static array A that can be accessed in Y and Z.
The Run () method of the class Y reads an input file and populates the vector A.
The Run () method of the Z class uses data stored into the vector A to process some data.
The objective of using threads in this problem is: as the vector A is filled in the Run () method of class Y, the Run () method of the class Z will processing the received values in the vector A.
to do this I did the following calls in the main method of class X:
ObjectY y = new Y (); Thready thread = new Thread (objectY); threadY.start (); ObjectZ new Z = Z (); Threadz thread = new Thread (objectZ); threadZ.start ();
is that correct? I'm getting the expected results, but dont know if the code is parallelized in fact.
I have situation where a user can request java server to send a value to an embedded device, and if the device is asleep, that value needs to be stored in a queue until the device wakes up and sends a position to java server, at which point the server checks if there is a value in the queue and if there is, it then sends that value to the device. The maximum size of the queue is 1 for now. And if the user makes continuous requests to java server, the old value is removed and the new value is added.
Initially I was looking into BlockingQueue, but the problem with that is, well, it blocks. queue.put(value) will block if queue is full, and queue.take() will block if queue is empty. I can't have something that blocks. When the device responds to server, server checks if value is in queue, if it is not then the server carries on the rest of its responsibility. Thus, I then entertained ConcurrentLinkedQueue. While queue.offer(value) and queue.poll(); allow you to add and remove values respectively from the queue without blocking, it does not allow you to set a maximum size limit of the queue. My queue must have a maximum size limit and it has to always be the newest value the user submits to the queue (where old values are removed).So this is what I came up with:
Java Code: class Unit { private List<Integer> cmdQueue;
public Unit(){ cmdQueue = Collections.synchronizedList(new LinkedList<Integer>());
[code]....
I use Collections.synchronizedList, as I do here, do I still need to use synchronize as I did above?