Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the "secret" behind Graphics drawing to a window?

Tags:

java

3d

rasterize

Graphics in itself is just some abstract Class. How does calling g.drawImage(Image img, tx, null) or something like that actually draw to the window? I looked a bit and I got that maybe something is going on in an instance of java.awt.Component? Is that right? I don't know.

My main reason is I want to make my own Graphics context called Graphics3D. My rasterizer will utilize this, and then from a Graphics3D context you can draw and manipulate 3D objects. Even if I could just inherit Graphics into my Rasterizer, I wouldn't know what to do.

like image 781
AMDG Avatar asked Oct 21 '22 04:10

AMDG


1 Answers

java.awt.Component is the superclass of any class that can be drawn on screen.

In this class you can see how really a Pixel is drawn on screen.
There is a method in this class i.e. public void repaint(long tm, int x, int y, int width, int height). In this function you have to look at 3403'th line to understand how it works.

It instantiate a PaintEvent for this. PaintEvent e = new PaintEvent(this, PaintEvent.UPDATE, new Rectangle(x, y, width, height)) where constructor for PaintEvent is PaintEvent(Component source, int id, Rectangle updateRect).

There is another line Toolkit.getEventQueue().postEvent(e).
Toolkit gets the EventQueue of system, and adds a PaintEvent into it.
EventQueue keep the track of all Events in a queue and fires them accordingly.
PaintEvent is the event to draw a rectangle on screen.

like image 109
afzalex Avatar answered Oct 23 '22 09:10

afzalex