Solved

Class derived from Graphics

Posted on 1998-04-27
3
209 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Impossible to extract MSI from new JAVA releases 2 47
Java / Linux and Regular Expressions 11 68
Java Timer (static) 9 36
nextBoolean(double p) for Random class 3 35
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…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

929 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now