Solved

Class derived from Graphics

Posted on 1998-04-27
3
205 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
Comment Utility
Edited text of question
0
 
LVL 16

Accepted Solution

by:
imladris earned 40 total points
Comment Utility
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
Comment Utility
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
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:
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

772 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

11 Experts available now in Live!

Get 1:1 Help Now