+ 9

in java.awt package Graphics is an abstract class so how we can create the Graphics class object and call the methods.

public void paint(Graphics g) { g.drawString("java",10,10); } we can write like this but how it is possible because Graphics is an abstract class and how we can call Graphics class method using that reference.

7th Nov 2019, 10:21 AM
Saroj Patel
Saroj Patel - avatar
8 Réponses
+ 5
Actually in applet and awt concept i. see and make this type of program but i have doubts only on this methods because Graphics class is abstract and how system pass his child class object to the argument it is actually i don't understand yet but now i clear .
9th Nov 2019, 5:18 PM
Saroj Patel
Saroj Patel - avatar
+ 3
I am not really familiar with graphics. But I know that you never call this method. The System does it. And normally you override the paintComponent() method. https://www.bogotobogo.com/Java/tutorials/javagraphics3.php
9th Nov 2019, 4:32 PM
Denise Roßberg
Denise Roßberg - avatar
+ 2
After debugging the code (the example in my first post) I found out that g is not from type Graphics, it is from type SunGraphics2D. The class SunGraphics2D is a Subclass of Graphics2D, which is a subclass of Graphics. http://srcrr.com/java/oracle/openjdk/6/reference/sun/java2d/SunGraphics2D.html Whenever a component (button, label, panel) is displayed the JVM automatically creates a Graphics object for the component on the native platform and parses this object to invoke the paintComponent() method to display the drawings. This method is definded in JComponent Class. It invokes wherever a component is first displayed or redisplayed.
10th Nov 2019, 4:22 PM
Denise Roßberg
Denise Roßberg - avatar
+ 2
See also the documentation about the paint() method: https://docs.oracle.com/javase/tutorial/uiswing/painting/closer.html
10th Nov 2019, 4:28 PM
Denise Roßberg
Denise Roßberg - avatar
+ 2
You can extend that class. And call the super class method's with Child class object reference. 🌀Saroj Patel🌀🇮🇳 .
30th Nov 2019, 2:37 PM
P∆WAN M∆URY∆
P∆WAN M∆URY∆ - avatar
+ 1
For a more detailed answer I will do some research. At the moment I can't explain where Graphics g came from and how the System calls this method.
9th Nov 2019, 5:21 PM
Denise Roßberg
Denise Roßberg - avatar
+ 1
I got similar result as Denise, the implementation and initialization of the Graphics object is handled by the swing (awt) itself this is possible way how to get Graphics too, but it is not standard draw way: Graphics g = panel.getGraphics(); //here from JPanel System.out.println(g); //--------- import java.awt.Graphics; // import java.awt.Graphics2D; import javax.swing.JFrame; import javax.swing.JPanel; ... JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(300, 300); frame.setLocationRelativeTo(null); JPanel panel = new JPanel() { public void paintComponent(Graphics g) { //Graphics is initialised // Graphics2D g2 = (Graphics2D) g; g.drawRoundRect(95,75,100,100, 30,30); //use Graphics method } }; panel.setSize(300, 300); frame.add(panel); frame.setVisible(true);
10th Nov 2019, 11:50 PM
zemiak
0
Your welcome :)
10th Nov 2019, 5:25 PM
Denise Roßberg
Denise Roßberg - avatar