Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

ClassCastException when running my program

Posted on 2003-12-10
12
Medium Priority
?
351 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 2000 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This video teaches viewers about errors in exception handling.
Suggested Courses

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