سؤال

I tried using java.awt.Graphics for the first time and came across so many confusions.

Graphics gp = new Graphics();

and the compiler threw an error saying an abstract class cannot be instantiated. I had a look at the oracle java documentation and then tried the following:

import java.awt.Graphics;
public abstract class GUI extends Graphics{

public void painter(){
GUI gp = new GUI();
gp.drawString("Hello People!",2,10);
} 
}

Still it shows the same error: GUI is abstract;cannot be instantiated

I do not have any idea about the abstract classes and implementation. However, I managed to get some knowledge in the Oracle's documentation. But still I'm confused, please someone help me. Thank you so much!!!!

I also tried removing abstract scope of GUI, but the compiler again threw another error saying: GUI does not override the abstract methoddispose();`in java.awt.Graphics();

هل كانت مفيدة؟

المحلول

Create a subclass of JComponent instead of Graphics. An instance of Graphics is then passed in to the paintComponent method:

class GUI extends JComponent {
    public void paintComponent(Graphics g) {
        super.paintComponent(g);       

        g.drawString("Hello People!", 2, 10);
    }  
}

If you try creating an instance of Graphics or a subclass yourself, then they'll be no useful way to use it.

نصائح أخرى

The best would be to use the Graphics2D and do the casting with Graphics.

An abstract class is one which informs what to do, along it has implementation of normal method that can be used by its child classes .
for e.g you can define a class Vehical as abstract that will define the behavior for other vehical classes. Other classes need to extends this Vehical class and provide the implementation for abstract methods.

you cannot create an object of Abstract class ,for using abstract classes you need to extends it by other class and then you can create an object of child class. for more referenec here

You don't want to reimplement the whole graphics stack; don't reimplement Graphics. Instead, you need to get the graphics context from somewhere; this is usually handled by the AWT toolkit, you just override void paint(Graphics g) in your components. See also this: http://docs.oracle.com/javase/7/docs/api/java/awt/Component.html#getGraphics() .

An abstract class is a class that can't be instantiated. It's only purpose is for other classes to extend.

An abstract class is a class that is declared abstract—it may or may not include abstract methods. Abstract classes cannot be instantiated, but they can be subclassed.

An abstract method is a method that is declared without an implementation (without braces, and followed by a semicolon), like this:

abstract void moveTo(double deltaX, double deltaY); If a class includes abstract methods, then the class itself must be declared abstract, as in:

public abstract class GraphicObject {    
   // declare fields    
   //declare nonabstract methods
     abstract void draw(); }

The main purpose of an abstract class is if you have common code to use in sub classes but the abstract class should not have instances of its own.

You can read more about it here: Abstract Methods and Classes

Remove the abstract:

import java.awt.Graphics;
public class GUI extends Graphics{

 public void painter(){
 GUI gp = new GUI();
 gp.drawString("Hello People!",2,10);
 } 
}

That is because you have to create a class that extends GUI or use one already defined, that also extends GUI.

Also u can remove abstract keyword, to use the one you have.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top