5

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.

AMDG
  • 1,118
  • 1
  • 13
  • 29
  • The `native` keyword. – user541686 Aug 04 '14 at 01:34
  • @Mehrdad obviously... but how does a `Component` draw from the Graphics context? – AMDG Aug 04 '14 at 01:37
  • 1
    The actual implementation is native. It translates the expectations of the `Graphics` class and translates them through what ever, native, implementation is been used to render those to a native peer, which is acting as the "surface" for the `Component` – MadProgrammer Aug 04 '14 at 02:16
  • If you have a look at `Component#getGraphics`, you will see, if the component is heavy weight, it asks for the `ComponentPeer` `Graphics` context. For the component, it's peer is assigned via `getToolkit().createComponent(this);` – MadProgrammer Aug 04 '14 at 02:23
  • You might also want to take a look at [Painting in AWT and Swing](http://www.oracle.com/technetwork/java/painting-140037.html) – MadProgrammer Aug 04 '14 at 02:25
  • 1
    Building 3d graphics on top of Swing and Graphics2D is a dead end. The primitives do not provide the right operations to do many things a serious 3d library must do beginning with hidden surface removal. Strongly recommend looking at thin bindings to 3d rendering APIs like OpenGL and D3D and implement your engine on top of these. – Gene Aug 04 '14 at 02:50
  • @Gene *sigh*... again, read my first comment. – AMDG Aug 04 '14 at 02:55
  • 3
    @LinkTheProgrammer Sigh. I'm trying to prevent you from wasting time. Don't try to study Java Swing to figure out how graphics work. Study OpenGL or D3D and how they talk to the hardware. Swing uses a fraction of the hardware's power, and that fraction isn't enough to write a serious 3d graphics package. Java3d is a much higher level API than OpenGL. In fact Java3d _uses_ OpenGL as its bottom layer. – Gene Aug 04 '14 at 02:59
  • Also, I realize the inefficiency, which is why I even mentioned adding my own JNI libs for direct access to the graphics drivers. I can guess what `invokevirtual` does now, thanks to a better understanding of JNI, but that is irrelevant to the situation at hand. – AMDG Aug 04 '14 at 03:08
  • 4
    @LinkTheProgrammer Suit yourself. You have an incredible amount of reinventing the wheel ahead of you. Note as stated in http://en.wikipedia.org/wiki/Java_3D that Java3d is implemented over OpenGL and D3D. These _are_ driver APIs. Don't say I didn't warn you. – Gene Aug 04 '14 at 13:52
  • I really need an answer guys! I'm dead in the water otherwise! I have to make this rasterizer work or else my project will be nothing. (except I could go try another library even though I dont want to.) – AMDG Aug 05 '14 at 14:52
  • I made a forum on the java forums too: http://www.java-forums.org/awt-swing/91742-what-root-class-draws-window-using-graphics.html#post393137 – AMDG Aug 11 '14 at 23:58
  • I've decided on an entirely AWT approach to 3d graphics rasterization. Yes I realize the incredible amount of reinventing the wheel, however native libraries have become a problem for portability and obscure the purpose for which java was made: to be platform-independant. Now, I just want to see how efficient I can really get with 3d graphics, even if it means barely touching the graphics card. All-in-all, your CPU can handle everything, including graphics, just using the graphics card to draw, nothing else. – AMDG Aug 13 '14 at 15:58

1 Answers1

3

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.

afzalex
  • 8,598
  • 2
  • 34
  • 61
  • So I could make my own painter by: creating a class called `Paint3DEventQueue` extends EventQueue, throwing a custom PaintEvent for a custom JComponent, then override the paintComponent method like usual in a JPanel, and it would do the exact same thing (minus a few other methods)? – AMDG Aug 13 '14 at 17:08
  • Am I correct in saying the process for drawing to a window is: *1)* define an area to update via `repaint(long tm, int x, int y, int width, int height);`. *2)* create a JPanel to draw stuff to that area. *3)* create a `PaintEvent` with the specified JPanel(as a component) to draw to the screen. Is that right? – AMDG Aug 13 '14 at 17:16
  • 1
    No, abolutely not. Because you are trying to go in too depth. You will have to create your own component for it which will call `repaint(long ....);`. Because if you use JPanel, then this JPanel will call paint(Graphics), not yours. But here you are trying to create your own method to draw on screen. – afzalex Aug 13 '14 at 17:24
  • So I should create my own custom component instead of a JPanel and then have my `Paint3DEventQueue` base it off of a `paintComponent()` method in that? – AMDG Aug 13 '14 at 17:26
  • Yes, It is the only method you can do if you want to paint on screen yourself. @LinkTheProgrammer. As far as I know. – afzalex Aug 13 '14 at 17:33
  • 1
    you @afzalex, are a life saver. I can now get on with creating a rasterizer for Minecraft. – AMDG Aug 13 '14 at 17:42