Solved

Class derived from Graphics

Posted on 1998-04-27
3
220 Views
Last Modified: 2013-11-23
It is possible to derive a class from the Graphics one ?  

I made this simple applet that draws cross in the screen at mouse click point with double buffering.  To make the cross, I wanted to derive my own class from the Graphics one, and then, create the drawCross function.

In compilation, all fits.  But, when I start the applet under J++, Vstudio returns me this error "Unhandled exception in Object: 0x00000000 : Java Exception" and breaks at the line "tmp2 = (GraphicsEnhanced)tmp;".

What's wrong ?

How can I implement my own Graphics class ?

==========================================
import GraphicsEnhanced;
import java.applet.*;
import java.awt.*;

public class Object extends Applet
{
      //Double buffering
      private Graphics tmp;
      private GraphicsEnhanced tmp2;
      private Image img;
    private Dimension dim = new Dimension();
      //---------------------

      public Object(){
      }

      public void init(){
            dim.height = getSize().height;
        dim.width = getSize().width;
        img = createImage(dim.width, dim.height);
       
            tmp = img.getGraphics();
            tmp2 = (GraphicsEnhanced)tmp;
      }

      public void destroy(){
      }

      public void paint(Graphics g){
            update(g);
      }

      public void update(Graphics g){
            //Double buffering
        g.clearRect(0,0, dim.width, dim.height);
        g.drawImage(img, 0, 0, this);
            //------------------------
      }

      public boolean mouseDown(Event evt, int x, int y){
            tmp.setColor(new Color(0,0,255));
            tmp.drawLine(x-10, y-10, x+10,  y+10);    
            tmp.drawLine(x-10, y+10, x+10,  y-10);    
            repaint();            
            return true;
      }

      public boolean mouseUp(Event evt, int x, int y){
            tmp.setColor(new Color(255,0,0));
            tmp.drawLine(x-10, y-10, x+10,  y+10);    
            tmp.drawLine(x-10, y+10, x+10,  y-10);    
            repaint();            
            return true;
      }

      public boolean mouseDrag(Event  evt, int  x, int  y){
            tmp.setColor(new Color(0,0,0));
            tmp.drawLine(x-10, y-10, x+10,  y+10);    
            tmp.drawLine(x-10, y+10, x+10,  y-10);    
            repaint();            
            return true;
      }

      public void Clear(){
            tmp.clearRect(0,0, dim.width, dim.height);
            repaint();
      }

      public void Trait(int x, int y){
            tmp.setColor(new Color(0,0,0));
            tmp.drawLine(x-10, y-10, x+10,  y+10);    
            tmp.drawLine(x-10, y+10, x+10,  y-10);    
            repaint();
      }
}
=========================================
import java.awt.Graphics;

public abstract class GraphicsEnhanced extends Graphics
{
      protected GraphicsEnhanced(){
            //super();
      }
      
      public void drawCross(int x, int y, int width, int thickness){
            int xPoints[] = new int[2];
            int yPoints[] = new int[2];

            xPoints[0] = x - (width / 2);
            xPoints[1] = x + (width / 2);
            yPoints[0] = y - (width / 2);
            yPoints[1] = y + (width / 2);

            fillPolygon(xPoints, yPoints, thickness);
      }

}
0
Comment
Question by:mhebert
  • 2
3 Comments
 
LVL 1

Author Comment

by:mhebert
ID: 1219951
Edited text of question
0
 
LVL 16

Accepted Solution

by:
imladris earned 40 total points
ID: 1219952
Yes, and no. Yes, the graphics class could be extended. But no, not the hopeful and straightforward fashion you attempted. You (correctly) declared it to be abstract, but you can't use an abstract class. To use and abstract class - like in the line

tmp2 = (GraphicsEnhanced)tmp;

you would have to have a concrete instantiation somewhere, i.e. an extension of GraphicsEnhanced that implemented all of the abstract methods.

The solution I have used, for such a case is an "indirection" class, that takes the Graphics object as an argument.

0
 
LVL 16

Expert Comment

by:imladris
ID: 1219953
For example you could write a class:

class Cross
{  int width,thickness;

  public Cross(int w,int t)
  {  width=w;
     thickness=t;
     return;
  }
   public void draw(Graphics g,int x, int y){
   int xPoints[] = new int[2];
   int yPoints[] = new int[2];

   xPoints[0] = x - (width / 2);
   xPoints[1] = x + (width / 2);
   yPoints[0] = y - (width / 2);
   yPoints[1] = y + (width / 2);

   g.fillPolygon(xPoints, yPoints, thickness);
   }
}


So for this example you would create a Cross object for every "type" of Cross you wanted to use. It could also, of course, simply be done as a static method.

0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
fibonacci ten numbers 4 45
login jsp example 24 63
ejb entity bean example 2 7
Eclipse Help Java EE 5,6,7 Documentation, why not Java EE 8 8 30
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question