Solved

ClassCastException when running my program

Posted on 2003-12-10
12
336 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
ID: 9915003
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
ID: 9915065
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
ID: 9915078
> 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
ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 
LVL 92

Expert Comment

by:objects
ID: 9915107
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
ID: 9915162
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
ID: 9915730
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
 
LVL 92

Expert Comment

by:objects
ID: 9915827
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
ID: 9915840
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
ID: 9915878
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
ID: 9915893
brilliant works a charm.
The shapes appear in the right places.
0
 

Author Comment

by:k1ngp1n99
ID: 9915967
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
ID: 9916189
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
more than one jdk and one jre 1 41
Tomcat: Unable to run tomcat service. 2 20
eclipse buid path vs tomcat lib path 10 23
Convert from a json string array to a Java object 3 32
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
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…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
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.

809 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