I'm looking to make a simple(for now) stopwatch application in java which I would eventually like to port to android devices, but first I'm just working on a simple computer run version and I'm wondering what I should do as far as displaying the elapsed time on the screen, so for I just have a JPanel with start and stop buttons, What would be the best way to actually display the time? I feel like simply drawing the numbers and erasing them is slow and inefficient, how can I do this so that the GUI is rapidly updated with the correct current time? I know how to compute the elapsed time using System.currentTimeMillis(), I'm just looking for the best way to display the time on screen that can be updated rapidly. I'd like to show it to at least the hundredth of a second which means the screen needs to be changing that often. Any suggestions?
Should I just use a JLabel?
You can separate Hours, Minutes, Seconds, Milliseconds into different `JLabel's.
So using Timer or ExecutorService you would update JLabel with Seconds and Milliseconds very often.
Other JLabels update only when needed, i.e. a minute elapsed.
Related
The first time I show my Component, it takes a few seconds to show. I am using a card layout. When I switch to a card for the first time is when I experience the slowness. Every time after that, it is almost instant.
I have two questions about this:
For Swing components, is there a way to kick this off early, for example, during application load?
What is happening under the hood that causes the first display to take longer than all the others.
i'm extremely new to java. i have a simple program here and what i want is to show a certain JPanel, after x seconds delay, when a button is clicked.
what i want in particular is something like a "start game" button, and upon clicking it, a panel that contains a "loading..." animation would be shown temporarily in the JFrame in x seconds, then another panel would show up after this.
i'm aware Timers would be the answer to this. but i've done all the thinking and research that needs to be done first. my last resort is to ask it here. i need at least a sample code upon which i can figure out the rest by myself
i'm badly in need. i have acquired poor java knowledge in a short period of time and my instructor expects a spectacular output from me. thanks in advance.
Simply write Thread.sleep(x*1000); after clicking the button.
It will make the application wait for x seconds. After completing x seconds application will proceed further.
I am creating a game where the user must tap the screen faster to allow them to go faster in game, however I have encountered some problems. I have made the game so it calculates the amount of taps per minute from the users last tap and their current tap, however this method seems to be jerky and does not work too well. I also cannot work out another way to do it, so that the when they don't tap at all, it slows down.
How would I go about creating this mechanism for the game where the faster the user taps, the faster the game goes, and if they don't tap at all it slows to a stop.
Thanks
Here is a snippet of what I have so far, calculating the Taps Per Minute (TPM) of the user. (This is in the on touch method). I am currently passing the TPM into an update method which moves the background by TPM/100 each time it updates (it's running at 30FPS)
if (thisTouch == 0 || lastTouch == 0) {
Log.d("info","First Touch");
thisTouch = lastTouch = System.currentTimeMillis();
} else {
Log.d("d","touch");
thisTouch = System.currentTimeMillis();
long difference = thisTouch - lastTouch;
TPM = (int)((1000*60)/difference);
lastTouch = thisTouch;
}
I would like to know how I can change this so that the game speeds up when they click faster, and then slows when they do not touch the screen.
Maybe you could do something like this:
Create a PriorityQueue< Long>
Whenever the user taps the screen, add (currentTime + someTimeOut) to the queue
Whenever you are going to check the current speed (eg size of queue), first remove all elements > currentTime.
Don't forget to add appropriate thread safety measures if you do something like this of course.
Not to be "that guy" but it sounds like your current plan of taps per minute is pretty good but just implemented badly. However, I would strongly recommend a smaller time quantity such as taps per three seconds since games are usually fast paced and having to wait a whole minute to see your character speed up or slow down may produce a sense of delayed lag.
Alternatively, you could could use some form of acceleration that is increased up to a maximum limit every time the screen is tapped and decreases over the time as the screen is not tapped.
Now that I see you updated your question with some code, I'd like to point out that a tap is a touch and release from the screen not necessarily a touch. I honestly don't know from your code whether or not you're compensating for this so this may be the source of some of your problems.
Lastly, for game development algorithms and theory you may want to see this StackExchange site: https://gamedev.stackexchange.com/
It sounds like you're very close to getting your game to feel the way you want it to, good luck!
Your general approach is pretty much the only way to do it, it just needs some fine tuning.
Keep a general TapsPerTime variable. With each new tap you register, do not replace the TapsPerTime, but calculate a new, weighted value:
TapsPerTime = (TapsPerTime * X) + (MeasureTapsPerTime * (1-X))
The factor X you need to find empirically (by trying out what works best for you). The general idea here is that each measurement will only nudge the used value a bit into the right direction.
Also, if you do not detect a tap for a set amount of time, simply update the TapsPerTime with the same formula, but use 0 (zero) for MeasureTapsPerTime. So if the user does not do anything the value goes down.
My goal here is to change the image in my gallery at certain times ,
lets say 5 seconds change , another 5 seconds another change .
Some sort of infinite loop using a handler to change the
img but i think this approach would consume a lot of battery .
what is the best way to do that?
I don't see nothing wrong in your approach.
Activity classes always consume a lot of the battery.
Just stop the timer in onPause().
I have written java code that captures a screenshot. User can set a time duration in seconds, e.g. 20 seconds, then after 20 seconds screenshot of current working application will be taken. Now I want to improve code by allowing user to set Number of screenshots to be captured in sec like if user sets ImageCnt=5 then 5 screenshots should be taken after every 20 sec.the code is like ....
int cnt = ImageCnt;
while(cnt!=0)
{
timerclass t=new timerclass(Time);//captures screenshot after specified Time
cnt--;
}
My Problem is in this code. I want that second screenshot should not be captured before the first is completed and third should not be captured before 2nd is completed and so on........
In my code it simultaneously captures all screenshot.
It sounds to me like instead of creating 5 timers, you ought to create a single timer which knows how many screenshots to take. It can then take one after another when the timer fires.
It's hard to know exactly what that will entail without seeing the rest of your code, but I'd advise adding the logic to your screenshot capture code rather than the code which sets up the timers.
You'll need a method in your timerclass that indicates the capture was completed.
Something like:
while (cnt > 0) {
timerclass t = new timerclass(Time);
t.waitForCapture();
cnt--;
}
I don't understand what the benefit of taking 5 screenshots everytime the Timer fires will do for you. Each screenshot will take milliseconds to perform, so what is going to change on the screen? You can just create copy of the first image which will be more efficient then taking a new screen image.
I would also probably use a Swing Timer to schedule the screenshot so the image is created in the EDT which means the GUI won't be attempting to repaint itself at the same time you are creating the image.
I use the ScreenImage class to create my images.