Solved

Class derived from Graphics

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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

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…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
The viewer will learn how to implement Singleton Design Pattern in Java.
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 …

730 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