Solved

ClassCastException when running my program

Posted on 2003-12-10
12
334 Views
Last Modified: 2006-11-17
Heres the code for my program is takes in a file which contains data like this.

LINE 250 500 100 100
CIRCLE 50 100 75 77
RECTANGLE 100 40 50 77
END

This part of the program displays correctly. My problem is when using the Line,Circle,Rectangle classes to anchor the shapes onto my GUI correctly.

heres the original question instead of repeating myself.
http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_20812032.html

this part causes the problems. Each shape class gives anchor points.

shapelist.add(new Line(10, 10));
shapelist.add(new Circle(50));
shapelist.add(new Rectangle(100, 50));

import java.io.*;
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
import javax.swing.border.LineBorder;
import java.util.*;

public class Proto1 extends JComponent{

private static final int WIDTH=400;
private static final int HEIGHT=300;
private ArrayList shapelist = new ArrayList();
public Proto1(String filename){

this.setPreferredSize(new Dimension(WIDTH,HEIGHT));
this.setBorder(new LineBorder(Color.red,10));
shapelist.add(new Line(10, 10));
shapelist.add(new Circle(50));
shapelist.add(new Rectangle(100, 50));
try{
String line;
BufferedReader in = new BufferedReader(new FileReader (filename));
while(!(line=in.readLine()).equals("END"))
{
StringTokenizer st = new StringTokenizer(line);
String tok = st.nextToken();
if(tok.equals("LINE"))
{
shapelist.add(new Line2D.Float(
Integer.parseInt(st.nextToken()),
Integer.parseInt(st.nextToken()),
Integer.parseInt(st.nextToken()),
Integer.parseInt(st.nextToken())));
}
else if(tok.equals("CIRCLE"))
{
shapelist.add(new Ellipse2D.Double(
Integer.parseInt(st.nextToken()),
Integer.parseInt(st.nextToken()),
Integer.parseInt(st.nextToken()),
Integer.parseInt(st.nextToken())));
}
else if(tok.equals("RECTANGLE"))
{
shapelist.add(new Rectangle2D.Float(
Integer.parseInt(st.nextToken()),
Integer.parseInt(st.nextToken()),
Integer.parseInt(st.nextToken()),
Integer.parseInt(st.nextToken())));
}

}

in.close();
}
catch(IOException e)
{
System.out.println(e.getMessage());
}
}
public void paintComponent(Graphics g){

super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
for(int i=0;i< shapelist.size();i++)
{
Shape shape = (Shape) shapelist.get(i);
g2.setPaint(Color.blue);
g2.draw(shape);
g2.fill(shape);
repaint();
}
}

public static void main( String [] args ){

JFrame jWindow = new JFrame("Prototype 1");
jWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jWindow.getContentPane().add(new Proto1(args[0]),BorderLayout.CENTER);
jWindow.pack();
jWindow.setVisible(true);
}
}

SHAPE CLASSES

// CIRCLE

import java.awt.geom.*;            // enables use of Line2D
import java.awt.Graphics2D;      // enables use of Graphics2D

class Circle implements Drawable
{
private int x;      // anchor point, x
private int y;      // anchor point, y
private int w;      // width of circle
private int h;      // height of circle
private int r;      // radius of circle

public Circle(int r)
{
this.r = r;      // store the radius value so it can be used in the draw method
w = r*2;  // 2 times the radius gives you the length of the width
h = r*2;  // 2 times the radius gives you the length of the height
}

public void draw(Graphics2D g, int ax, int ay)
{
x = ax-r;          //anchor point - radius to give to give centre at anchor point
y = ay-r;
g.draw(new Ellipse2D.Float(x, y, w, h));
}

public String toString()
{
return "Circle: radius=" + r + " centre points (" + x + "," + y + ")";
}
}

// LINE

import java.awt.geom.*;            // enables use of Line2D
import java.awt.Graphics2D;      // enables use of Graphics2D

class Line implements Drawable
{
private int dx;      // x displacement from the anchor point
private int dy;      // y displacement from the anchor point

public Line(int x, int y)       // anchor point starts at x, y
{
dx = x;
dy = y;
}

public void draw(Graphics2D g, int ax, int ay)
{
g.draw(new Line2D.Float(ax, ay, ax+dx, ay+dy));
}

public String toString()
{
return "Line: x-displacement=" + dx + " y-displacement=" + dy;
}
}

// RECTANGLE

import java.awt.geom.*;            // enables use of Line2D
import java.awt.Graphics2D;      // enables use of Graphics2D

class Rectangle implements Drawable
{
private int w;
private int h;

public Rectangle(int w, int h)
{
this.w =w;
this.h = h;
}

public void draw(Graphics2D g, int x, int y)
{
g.draw(new Rectangle2D.Float(x, y, w, h));
}

public String toString()
{
return "Rectangle: width=" + w + " height=" + h;
}
}

please ask for more code if needed.
0
Comment
Question by:k1ngp1n99
  • 6
  • 4
  • 2
12 Comments
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
interface Drawable should extend interface Shape

As I mentioned in earlier posts, you would have only needed the interface Shape
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
if(tok.equals("LINE"))
{
shapelist.add(new Line2D.Float(
Integer.parseInt(st.nextToken()),
Integer.parseInt(st.nextToken()),
Integer.parseInt(st.nextToken()),
Integer.parseInt(st.nextToken())));
}
else if(tok.equals("CIRCLE"))
{
shapelist.add(new Ellipse2D.Double(
Integer.parseInt(st.nextToken()),
Integer.parseInt(st.nextToken()),
Integer.parseInt(st.nextToken()),
Integer.parseInt(st.nextToken())));
}
else if(tok.equals("RECTANGLE"))
{
shapelist.add(new Rectangle2D.Float(
Integer.parseInt(st.nextToken()),
Integer.parseInt(st.nextToken()),
Integer.parseInt(st.nextToken()),
Integer.parseInt(st.nextToken())));
}


You should be creating instances of your Drawable classes, and not Shape instances.
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
> Shape shape = (Shape) shapelist.get(i);
> g2.setPaint(Color.blue);
> g2.draw(shape);
> g2.fill(shape);

should be:

Drawable shape = (Drawable) shapelist.get(i);
g2.setPaint(Color.blue);
g2.draw(shape, 0, 0);
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
Alternatively if you want to support both Shape and Drawable just change your poaintComponent method to:

public void paintComponent(Graphics g){

super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
for(int i=0;i< shapelist.size();i++)
{
   Object next = shapelist.get(i);
   if (next instanceof Shape)
   {
      Shape shape = (Shape) g2.setPaint(Color.blue);
      g2.draw(shape);
      g2.fill(shape);
   }
   else if (next instanceof Drawable)
   {
      Drawable shape = (Drawable) next;
     g2.draw(shape, 0, 0);
   }
}
repaint();
}

That'll allow you to add both Shape and Drawable instances to your shapelist.
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
There's no need for a new interface called Drawable.

a. Shape is specifically designed so that various shapes of different types can be drawn on a graphics context
b. Creating subclasses of the library classes will mean that you're already implementing Shape anyway, so you won't have to do anything

e.g. with the LINE

class LINE extends Line2D.Float {
}

// add an instance to the list

Shape s = (Shape)shapeList.get(i);
0
 

Author Comment

by:k1ngp1n99
Comment Utility
objects i get this errors when i try to replace your paintcomponent method with what you said
>Alternatively if you want to support both Shape and Drawable just >change your poaintComponent method to:


C:\Documents and Settings\kingpin\My Documents\Work\Software construction\Project Draw\prototype2\My ****\Proto1.java:70: inconvertible types
found   : void
required: java.awt.Shape
                        Shape shape = (Shape) g2.setPaint(Color.blue);
                                                                 ^
C:\Documents and Settings\kingpin\My Documents\Work\Software construction\Project Draw\prototype2\My ****\Proto1.java:77: draw(java.awt.Shape) in java.awt.Graphics2D cannot be applied to (Drawable,int,int)
                        g2.draw(shape, 0, 0);
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 92

Expert Comment

by:objects
Comment Utility
sorry, my fault. try this:

public void paintComponent(Graphics g){

super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setPaint(Color.blue);
for(int i=0;i< shapelist.size();i++)
{
   Object next = shapelist.get(i);
   if (next instanceof Shape)
   {
      Shape shape = (Shape) next;
      g2.draw(shape);
      g2.fill(shape);
   }
   else if (next instanceof Drawable)
   {
      Drawable shape = (Drawable) next;
     g2.draw(shape, 0, 0);
   }
}
repaint();
}
0
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
Comment Utility
thats not right either :-)

public void paintComponent(Graphics g){

super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setPaint(Color.blue);
for(int i=0;i< shapelist.size();i++)
{
   Object next = shapelist.get(i);
   if (next instanceof Shape)
   {
      Shape shape = (Shape) next;
      g2.draw(shape);
      g2.fill(shape);
   }
   else if (next instanceof Drawable)
   {
      Drawable shape = (Drawable) next;
      shape.draw(g2, 0, 0);
   }
}
repaint();
}
0
 

Author Comment

by:k1ngp1n99
Comment Utility
objects that solves the first error but the other error still comes up.
That was a quick reply anyway.

C:\Documents and Settings\kingpin\My Documents\Work\Software construction\Project Draw\prototype2\My ****\Proto1.java:78: draw(java.awt.Shape) in java.awt.Graphics2D cannot be applied to (Drawable,int,int)
                        g2.draw(shape, 0, 0);
0
 

Author Comment

by:k1ngp1n99
Comment Utility
brilliant works a charm.
The shapes appear in the right places.
0
 

Author Comment

by:k1ngp1n99
Comment Utility
objects could you please explaint this code to me. You gave me this code last time. Its from the question i linked to about creating a picture and pictureComponent class. A quick tip in implementing this would be helpful, because it seems to be correct from what ive read from the specification.

Points are yours ;)
Thanks again.


public class PictureComponent implements Drawable
{
   private Drawable Shape = null;
   private int X = 0;
   private int Y = 0;

   public PictureComponent(Drawable d, int x, int y)
   {
      D = d;
      X = x;
      Y = y;
   }

   public void draw(Graphics2D g, int ax, int ay)
   {
      D.draw(g, X+ax, Y+ay);
   }
}

public class Picture implements Drawable
{
   private ArrayList Drawables = new ArrayList();

   public void addDrawable(Drawable d, int x, int y)
   {
      Drawables.add(new PictureComponent(d, x, y));
   }

   public void draw(Graphics2D g, int ax, int ay)
   {
      Iterator i = Drawables.iterator()
      while (i.hasNext())
      {
          PictureComponent d = (Drawable) i.next();
          g.draw(g, ax, ay);
     }

}
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
PictureComponent is a Drawable implementation that drfaws a Drawable instance at a specified offset.
Picture is a Drawable implementation that draws a list of Drawable's each at specified offsets.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

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

12 Experts available now in Live!

Get 1:1 Help Now