Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where are methods in java.awt.Graphics defined?

Tags:

java

graphics

I've been thinking a lot about making my own Java3D API for a few projects of mine. (Yes, I've seen the Java3D API provided by Oracle.) Many methods in java.awt.Graphics are defined, such as drawRect(), however some basic functions, such as drawLine(), are abstract. How is it possible to create a Graphics object (as in the code below) when there is no implementation in Graphics.java?

public void paint(Graphics g) {
    g.drawString("Hello World!");
}

Any help or explanation is greatly appreciated! Thanks in advance.

like image 651
Mokolodi1 Avatar asked Mar 01 '26 20:03

Mokolodi1


1 Answers

This is implementation-specific, but these methods are usually implemented natively so that they can interface directly with the native windowing system. You cannot instantiate a Graphics object because the concrete implementation of Graphics that you would want to use is probably an implementation-specific subtype of Graphics that interacts with the native system. Forcing the Java libraries and JVM implementers to create this object for you prevents you from having to deal with the nastiness of OS-level details and provides a generic, type-safe interface to the graphics package.

If you want to implement your own package, you would probably have to use JNI to implement a graphics package. You could then implement the graphics on top of OpenGL, DirectX, or whatever other system that you'd like.

Hope this helps!

like image 163
templatetypedef Avatar answered Mar 03 '26 09:03

templatetypedef



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!