Stephen Pillinger

Ranch Hand
+ Follow
since Mar 03, 2019
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
2
Received in last 30 days
0
Total given
32
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Stephen Pillinger

Greetings all,

I have created a large panel which I want the user to be able to scroll around by moving the mouse to the edge of the screen (like in most strategy games with a top-down map).

To do this I am resetting the 'value' on the scroll bar using a timer that ticks rapidly to set the value of the scroll bar according to the location of the mouse on the screen:



I feel the above is a very messy way of doing this and there must be a much cleaner way of basically simulating the click/drag scrolling functionality of the scroll bar using code.

By simply setting the value in this way I find as it scrolls it is not smooth at all and the image has these blips of horrible white background (panel background) appearing as it's trying to keep up with the constant changing.

I'm fascinated to know how the clicking & dragging works in such a way as for it to remain completely smooth and to find out if there's a way I can simulate that using code!

I hope that makes sense - open fire!

Cheers,

S.
2 years ago

Rob Camick wrote:Swing doesn't know how to paint components with transparency correctly.

Swing expects the component to be:

1. opaque - in which case it doesn't paint the parent component and you get painting artifacts because the background is not cleared before the child component is painted, or
2. transparent, in which case the parent component is painted, but the background of the child component is not painted, which means you manually need to paint the background.

For a reusable solution check out Backgrounds With Transparency.

You would use it in your code as follows:



THANK YOU!!! Your AlphaContainer class works like an absolute dream - created the class per your recommendation and wrapped the JLabel in it and it now works.

Thank you very much.
3 years ago

Rob Camick wrote:

with a foreground on the label that has its transparency set.


The foreground has nothing to do with this problem.

The foreground is used for setting the color of the text. There is no text to display so setting that property does nothing.



I thought as much but I'm setting it anyway as I'm likely to have text at some point.
3 years ago
Hey Rob - I'll see if I can get your suggestion working
3 years ago

Stephan van Hulst wrote:Why are you calling setOpaque(true) when clearly you want the component to be transparent?

setOpaque(true) tells Swing that the container underneath the opaque component doesn't need to draw its content, because the component already draws every pixel that it occupies. I don't know if that is the source of your problems, but it could definitely be a reason why the container underneath the label appears white.

I find the way you're trying to do this very iffy. Can you explain your use case a bit better, maybe we ca suggest a better method of doing things.



Hi Stephan, thanks for your response.

When you say 'explain your use case' a bit better... what do you mean? I'm not sure what I can add to make my objective any clearer. I could perhaps provide screenshots of the end objective...

But you're right about setting opacity to 'true' on the label not making sense and I think I've worked out a possible solution - instead of adding the label to the button, I'll add the button to the label with a foreground on the label that has its transparency set. I'll see if that works.

The problem is that if opacity is 'false' then the label is 100% transparent, and setting a foreground or background colour with a transparency parameter makes no difference - which is what I'm trying to work around.

Can I ask - what is so 'iffy' about this way of doing things? If 'Color' has a constructor that enables you to deliberately specify the RGB and then add a transparency parameter as to how transparent you want it to be, applying a transparent colour to a label or button should not seem iffy at all - it's downright intuitive!

I'm grateful for your attempt to help, it's a bit discouraging though that what seems like such a common-sense approach here seems so left-field to someone so much more experienced than I am! Makes me think I've got something very fundamentally wrong here!!


3 years ago
Greetings

Struggling to get a button with a background image to overlay that image with a colour that has a transparent quality i.e. a background image of grass with a black overlay which can become more and more transparent until you can see the grass behind it as though through a fog (literally trying to create a 'fog of war' in a game). I've done this before so I know it's possible but now cannot for the life of me remember how I did it.

Below is the test scenario I set up which you can run and see for yourself what I'm trying to accomplish and perhaps what I'm getting wrong.

Main Method:


Testing Class:


As you can see I've tried various things and I feel I'm getting close but missing something. I'm confused as to the point of having a method for a JLabel that allows you to set opacity if doing so simply insists on having a white background.

Help much appreciated!

Thanks,

SteveyP.
3 years ago
Further to the above, I've created a self-contained example of what I'm talking about - I've managed to replicate the issue which you can run for yourself and see what I'm getting wrong.

The objective is to gain full mastery & control over the positioning of the horizontal and vertical scroll bars.

In order to do this, I'm wanting to use the 'set value' method of the scroll bars but am unable to as long as I am unable to obtain relative positioning by making calculations based on the extent of the bars.

Here is the main method I'm using:




The system outputs demonstrate the issue - the extent comes back 'zero'. Have I misunderstood what the 'extent' is?  According to the Oracle Docs sent to me above, this shouldn't be a zero, it should be 'the visible amount'.

Thanks again,

Stephen.
3 years ago
Hi Campbell,

Thanks for your response.

Yes, I have looked at the training and it was very useful. I did have a look at the API's too and can probably work something out...

At this stage, although I will eventually find a workaround, I'm just very confused and I don't like working around something just because I don't understand it.

If this issue has to do with repainting, why is it that it successfully outputs the maximum and value of the scroll bar but not the extent?

If it's to do with repainting or the visibility of the component I would have thought none would return any value - this problem seems specific to the extent.

So I'm still somewhat at a loss...

Cheers,

Stephen.
3 years ago
Good morning,

I'm assuming I am missing something extremely obvious so will not throw loads of code into this query unless it's not so obvious and you need more than this...

I have an instantiated scroll bar which is already visible on the screen called mainHorizontalScroll.

I successfully manage to obtain the following:


What I would like to do upon opening the program, is get the bar to simply centralise itself. Seems simple enough but... apparently not!

In order to do this I need these three things apparently:
1) The maximum
2) The value
3) The extent (viewable area)

... of the scroll bar.

When I attempt, however, to obtain the extent it always returns zero, no matter what:


What I am wanting to do to centralise the bar is as follows:


The results are fruitless.

What am I missing?

Thank you very much in advance.
3 years ago

Tim Holloway wrote:Don't keep the File Chooser as an instance variable. Obtain it when needed, then discard it.

Also, keep your data (serializable - Model) classes separate from your UI classes. The classes that construct and use the GUI (View, Controller) should not be serialized.



Is MVC architecture still the accepted norm/conventional way of structuring code?

Honestly, I'm brand new, right now - my only focus is to make something very simple work - haven't even started studying the architecture of structuring code yet (properly, at least).
3 years ago
Good afternoon,

I'm getting the following exception: java.io.NotSerializableException: javax.swing.plaf.metal.MetalFileChooserUI

From what I understand, a non-serializable exception occurs when a class does not implement serializable.

I've checked and all the class variables in the main class I am writing to an object are serializable.

However, one of the variables is a file chooser instance.

So I have tried to make that file chooser instance transient, in case that was what was causing the issue - but it seems not to have resolved.

Any pointers as to where I might be going wrong?

Thanks all,

S.
3 years ago

Campbell Ritchie wrote:Don't extend JFrame. Your application ISN'T‑A JFrame; it HAS‑A JFrame.



I'll need more of an explanation than this to justify not extending JFrame... seems like it's just 'not the done thing' which isn't much of a reason. Is there some functional reason extending JFrame is a bad idea? Or is it just not the conventionally accepted way of doing things? Would be interested to know.
3 years ago

Rob Spoor wrote:A must-watch when using GridBagLayout/GridBagConstraints:



HAHA yes!!! Thank you everybody for your help I've got some sense of where to go next.
3 years ago
Hi everyone,



Worked yesterday, broken today, no changes made to the code - simply loaded Eclipse and it decided GridBagConstraints isn't a thing. Says it cannot resolve itto a type (compile error).

Suggestions?

Ever feel like if Java & Eclipse was a person you'd want to kick him in the balls?

Thanks,

S.
3 years ago