Override Java awt repaint system (or at least make a paint listener)
I'm trying to hook up Java awt to a glfw window. I have a container (no parent) with some children, like a button. I want to know when the container is trying to repaint an area, then make my own repainting system that sends the painted content to a texture to be rendered. How can I do this? I've tried overriding repaint and paint in the container, and they don't get called. I don't want to have to insert code for all components that I add to the container. Is there some paint listener thingy I can use to do this?
Also, kinda related question. If the container is trying to paint a specific section, I create a BufferedImage, create a graphics for it, then tell the container to paint to it, right? But it would just paint the whole window to that image, but I only want that specific section. And is there a better way than the BufferedImage? I need the output to eventually go to a ByteBuffer.
Hope this is clear enough
64 Replies
⌛
This post has been reserved for your question.
Hey @The Typhothanian! Please useTIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here./close
or theClose Post
button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically marked as dormant after 300 minutes of inactivity.
If you want to make a UI component with custom things, I think you can extend
JPanel
(or similar) and extend paintComponent
(or whatever that was called)
For the second question: If you want to paint to a component, you can use the Graphics
/Graphics2D
object directly, you don't need to use a BufferedImage
if you want to save it to a PNG/JPEG or whatever they you need ityeah looks like I didn't do a good job of explaining. Say I have a Button that is a child of my custom Container. The container is my custom window system (via glfw). I want to know when the Button calls repaint(), so that I can handle the painting myself, to the glfw window. I found this method that looks like it's sending the repaint request to the parent awt window:
but I can't figure out how to intercept the repaint request (including bounds)
Doesn't the button repaint just get called when your repaint is called?
uh yeah
Also I think you might be able to extend
JButton
well I don't really want to have to modify all components for use with this system, and that would get really annoying with complex components with a lot of subcomponents
Also I think you should probably override
paintComponent
instead of repaint()
wdym
it wouldn't get called
because there isn't an awt window
What wouldn't be called?
the paint methods
not?
I've checked
cus there isn't a graphics for it to be called on
You are dealing with Swing, right?
kinda
I'm making a game with lwjgl, and want to hook up java awt to the glfw window so I don't have to make a whole component system myself
already got most of the stuff done like input and such
I just need to know when a child of the window needs to repaint, then handle that myself, because a glfw window isn't an awt window and it wouldn't get handled
for the ui
if you want I can share my code
Just repaint the subcomponent when you are asked to repaint?
uhhhhhhhh huh
what
Sorry, getting really confused on what you are saying. The child (say, a button) calls repaint() when it needs to repaint, then weird java voodoo magic tells the AWT window to repaint that area. It doesn't go through the component tree until it gets to the window, so I can't override my custom glfw window container's repaint method to paint to the glfw window
Why does the button call repaint?
like when the mouse moves over it or something
Can't you extend the button class and override repaint?
yeah I could
but I'd have to do that for every single component, which I don't feel like doing
Do you have many different component types?
maybe you can add a container listener or similar? https://docs.oracle.com/en/java/javase/21/docs/api/java.desktop/java/awt/Container.html#addContainerListener(java.awt.event.ContainerListener)
Container (Java SE 21 & JDK 21)
declaration: module: java.desktop, package: java.awt, class: Container
I'm not sure what exactly that does though
that's for adding and removal of comps
How many different types of containers do you have?
well currently I have a Container that's acting as the window and a JButton as a child of the container, and I'm testing calling repaint() on the button
but that's just for testing
there's gonna be a lot more for my actual game
With extending, you'd only have to extend
JButton
that waymmmm yeah
that's just for testing part
btw
BufferedImage
will likely be slowI'd have a whole slew of other stuff I'd need to override
well I need to get a Graphics to an opengl texture
Aren't there lwjgl components you can use instead of Swing?
I'm planning on making a ByteBuffer that stays between frames, then when a section needs repainting I create a BufferedImage that size, get graphics, paint comp to graphics, put the img content in the byte buf at appropriate area, and upload byte buf to texture again
uh no
no one really does what I'm attempting
worst case I could make a whole new component system
but that would take a long while
What do you need the components for?
Why?
like buttons for my game's ui, hotbar, inventory, stuff like that
because I need to render the content to the window with opengl
if you draw to a
BufferedImage
, that might take longer than you are suppsed to drawis there a better alternative?
I eventually need to move the content to a byte buffer
if I was coding in C++ there would be a much better way, but I like Java more
Why?
Why put it in a ByteBuffer?
to upload to the opengl texture to send to the gpu to render each frame on top of game content
the texture only takes byte buf
for the texture pixels
Isn't lwjgl supposed to do that for you?
what part
Are you doing that each frame?
no
only when the awt needs to redraw
putting it to the GPU
and I need to figure out how to tell when it needs to redraw
yeah it does do that for you
I just have to tell it what to put in the texture
(texture covers whole window btw)
When does a button need to repainted without you being informed?
wdym
You wanted to get called when the button needs repainting
But why is that happening without you getting informed?
yeah, code to actually paint the button cus that isn't happening
because java awt isn't meant to be hacked like this, usually it handles the window itself
including repainting
so I might just have to remake a component system, ugh
yes but AWT/Swing shouldn't randomly randomly call repaint on buttons
it... isn't
if I move my mouse over the button or click it or something, it needs to repaint
the issue I have is that there isn't a system that I can find that can redirect repaint calls to my code
Can you capture mouse move events?
yes
I've already done that easily
then just do the repainting whenever you have anything happening that would need to repaint it
well again I'd have to do that for every component I plan on adding
If if you only have a few component types, extending shouldn't be a lot of work
I think I should just create a new component system
well I don't
you can do it with composition maybe
composition?
but when the target calls repaint internally it won't call the wrapper repaint
You can't do anything against it except extending or also call your repaint method in all of the relevant methods
mmmmmkay
guess I'm gonna make a custom component system
💤
Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.
In case your post is not getting any attention, you can try to use /help ping
.
Warning: abusing this will result in moderative actions taken against you.