This week's book giveaway is in the Python forum.
We're giving away four copies of Python Continuous Integration and Delivery and have Moritz Lenz on-line!
See this thread for details.
Win a copy of Python Continuous Integration and Delivery this week in the Python forum!

Piet Souris

+ Follow
since Mar 08, 2009
Cows and Likes
Total received
In last 30 days
Total given
Total received
Received in last 30 days
Total given
Given in last 30 days
Forums and Threads
Scavenger Hunt
expand Rancher Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Piet Souris

You can have formatted output, equivalent to the well-known printf function from other languages. For instance, see this page:
formatted output in pythonformatted output in python
1 month ago

Paul Clapham wrote:Another solution would be to start the timer as soon as the key is pressed, and stop the timer as soon as it's released. (...)

There are two objects rotating, one continously, so you need a Timer anyway. What you can do is having an ActionListener that handles the keyPressed event, and adding or removing that AL to or from the timer in the keyPressed method.. See the API for the Swing Timer. But that is more complicated than OP's solution, although OP's solution can be simplified.
1 month ago
A petite hint: the divisors of 12 are 1, 2, 3, 4, 6, 12. What has that got to do with the problem, and what has that got to do with the Sieve of Eratosthenes?
1 month ago
Is usually a good idea, but here it is not necessary, since the bufferedimage has the same size as the panel (content pane), the frame is not resizable and the bufferedimage has no transparancy.
1 month ago
When you are done with the simulation, can you figure out how many times lightbulb i is switched (i = 1...100), and predict in what state it will end?

edit: oops, I had not noticed Campbells remark about the Sieve. Sorry for that!
1 month ago
There are two possible causes for the jerkiness that I can think of:

one is that Java is too slow to do the painting within the 20 milliseconds from the timer, and thus what you see might be incomplete drawings.

However, that is not the case here. A weakness in your code is the relation between how many times keyPressed is invoked, and how many times paint is executed.
If you add a counter to the paint method, and another counter to the keyPressed method, and print these values in the paint method, you will see a diverge of the two.
So, adjust your code such that whenever paint is invoked, the variables 'rotation' and 'rotationTwo' are updated only once since the last repaint.

Another way is to do the updating of the graphics in a separate Thread, and when the updating is finished, invoke repaint on the panel, or use the timer. This way, you give the updating process the full 20 ms (or whatever your timer is set to).

Lastly: follow Pauls advice and try different values of your parameters (make them variables in your code, so that you can adjust them with ease).
1 month ago
OP is drawing a BufferedImage, and that is not a Component.

You are overriding the 'paint' method. That is something that you should not normally do, although in this case it doesn't matter.
Paint does three things: it invokes the method 'paintComponent', in which you would do your user drawings, then it invokes the method 'paintBorder', that draws a border if any, and lastly it invokes the method 'paintChikldren', that draws all your labels, buttons et cetera. So, in normal circumstances you only should override the 'paintComponent' method.

You use two BufferedImages to rotate your image. Nothing wrong with that, but you can draw your image also directly to your panel, where you do a rotation in the Graphics2D of the panel. That would simplify your code a little.

I just created an example where I override the 'paint' method, to give an example where overriding the paint method is useful. I created a JLabel that contains the image, and centered the label in its panel. Then I override the paint method of the panel, where I first invoke the two methods 'paintComponent' and 'paintBorder', then I create a Graphics2D that I give a rotation, and with this g I invoke 'paintChildren'. As a consuquence, the JLabel with the image is then rotated.
This is only a demo, of course, it has no practical value.
1 month ago
A simple way is to have a Map<String, JLabel>, where you put your labels when you create them.
In your switch, all you have to do is

In fact you do not need the switch.
1 month ago
Right. But have a look at line 27. Do you think that you create s1 in a correct way? Add a println right behind that line and print out what s1 is. Or use a single stepper if you are using an IDE.

Then the second pass of the loop starts. Have a look at lines 18-20. What happens to 'set1'? Add again a println there as well, to see what happens to set1. Do you think the creation of set1 should be in that inner loop?
1 month ago
you are performing the loop starting at line 14 twice. After the first loop. set1 contains the 2 expected chars, that's oke. But then, in the second loop, have a look at line 19. Can you spot the error? What is the meaning of the loop at line 25?

PS: I had no trouble understanding the question.
1 month ago
Oh no, not again!

But can you elaborate a little? I used this code:

1 month ago
Oops... I completely screwed up, missed the 'i = i * i' and 'j = j * j'. Sorry for that!

Well, that change makes it somewhat harder. What I do in these cases is calculating both methods for several different input values, list the output and try to see some connection between the input and output. I used, as input, the values 2, 4, 8, 16, ... up to 2^100 (be careful, use BigIntegers). You see first that, after a certain input value, that the second method is calculated more than the first method. For instance, when n = 2 ^51, the first method is calculated 21 times, the second 51 times. The  second method has O(2log(n)) = O(ln(n)), the first is more complicated, you get that 2 ^(2 ^n) gives a count of 1 + 2 + ... + (n + 1), with a floor function for intermediate values.
1 month ago
I STILL miss my BBC Basic 5 switch:

and then a series of boolean expressions, gosh that was handy from time to time...
1 month ago

Tim Holloway wrote:(...) So you may not see any performance gains by manually unrolling a loop, and it would just result in more lines of code to ride herd on.

On my ancient 25MHz Arm3 processor, with, IIRC 8 kb instruction cache, the advice was not to unroll loops too much, because of the possibility the code might not fit in the cache. But maybe current processors are less limited.
1 month ago
If you want to cheat, then do it in an elegant way. Look at the 'Files.lines(...)' method, with .limit(n) and .toArray(String[]::new]. Just one line, and the 'variables' will be string[0]...string[N-1].
1 month ago