?
Solved

Class derived from Graphics

Posted on 1998-04-27
3
Medium Priority
?
237 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 1

Author Comment

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

Accepted Solution

by:
imladris earned 160 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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month12 days, 4 hours left to enroll

752 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