13 Replies - 2546 Views - Last Post: 04 April 2011 - 10:44 PM Rate Topic: -----

#1 lostinclass  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 29
  • Joined: 18-March 11

implementing the shape interface on JFrame

Posted 03 April 2011 - 12:13 PM

import javax.swing.JComponent;
import java.awt.Graphics;
import java.awt.Graphics2D;

public class RandomShapesComponent extends JComponent
{
   public void paintComponent(Graphics g)
   {
      Graphics2D g2 = (Graphics2D) g;
      RandomShapeGenerator r = new RandomShapeGenerator(getWidth(), getHeight());

      for (int i = 1; i <= 10; i++)
         g2.draw(r.randomShape());
   }
}



import javax.swing.JFrame;

public class RandomShapeViewer
{
   public static void main(String[] args)
   {
      JFrame frame = new JFrame();
    
      final int FRAME_WIDTH = 300;
      final int FRAME_HEIGHT = 400;

      frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
      frame.setTitle("RandomShapeViewer");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      RandomShapesComponent component = new RandomShapesComponent();
      frame.add(component);

      frame.setVisible(true);
   }
}




I am suppose to write a method randomShape that randomly generates objects implementing the Shape Interface: rectangles, circles ect. call it 10 times. They gave me the RandomShapeViewer and RandonShapeComponent above.

I do not understand how do i make a shape interface and call or create different shapes in my program. I created the class RandomShapeGeneator but i donot know how to write the correct code for the interface. Can someone point me in the write direction. I thought I needed to create a circle.draw() in my program.


import java.awt.Shape;

public class RandomShapeGenerator
{

	public RandomShapeGenerator(int i, int j)
	{
		// TODO Auto-generated constructor stub
	}

	public Shape randomShape()
	{
		// TODO Auto-generated method stub
		return null;
	}

}



Is This A Good Question/Topic? 0
  • +

Replies To: implementing the shape interface on JFrame

#2 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1011
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: implementing the shape interface on JFrame

Posted 03 April 2011 - 03:41 PM

The trick here is, to think of a Shape as an abstract idea.
I have written a tutorial about abstract clases vs interfaces and I discuss topics that would help you there.

Anyways, representing a Shape as abstract class\interface is a common example in the web. you can find numerous examples. here is a nice one:
http://www.javabegin...s-and-interface
Was This Post Helpful? 2
  • +
  • -

#3 lostinclass  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 29
  • Joined: 18-March 11

Re: implementing the shape interface on JFrame

Posted 03 April 2011 - 06:04 PM

that doesnt help me at all
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,913
  • Joined: 06-March 08

Re: implementing the shape interface on JFrame

Posted 03 April 2011 - 06:22 PM

You have a weird design but anyhow it may work

Your RandomShapeGenerator should save the size of the futures shapes to draw
import java.awt.Shape;

public class RandomShapeGenerator
{
    int width, height;
    Random ran = new Random();

	public RandomShapeGenerator(int i, int j)
	{
		int width = i;
		int height = j;
  
	}


then your randomShape method should return a Shape based on a Random condition
	public Shape randomShape()
	{
		int n = ran.nextInt(5);   // assuming 5 id the number of different shapes
                switch(n) {
                   case 0:
                     return a Shape object of some kind
                   case 1:
                     return a Shape object of some kind
                   case 2:
                     return a Shape object of some kind
                   case 3:
                     return a Shape object of some kind
                   case 4:
                     return a Shape object of some kind
                }   
	}


Was This Post Helpful? 2
  • +
  • -

#5 lostinclass  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 29
  • Joined: 18-March 11

Re: implementing the shape interface on JFrame

Posted 04 April 2011 - 03:03 PM

how do i return a shape from the different cases.
it wont let me return more than one. DO i need to create a shape interface. (not sure how to do that either)
i can return one shape(circle) using the code below. i was trying graphics2d.draw(shape circle) but that doesnt work

	public Shape randomShape()
	{
		Shape circle = new Ellipse2D.Float(10, 10, 10, 10);		
		return circle;



switch(n) {
case 0:
   return  (((( [b]how do i return a shape??[/b])
case 1: 
return


Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,913
  • Joined: 06-March 08

Re: implementing the shape interface on JFrame

Posted 04 April 2011 - 03:11 PM

View Postlostinclass, on 04 April 2011 - 05:03 PM, said:

how do i return a shape from the different cases.
it wont let me return more than one. DO i need to create a shape interface. (not sure how to do that either)
i can return one shape(circle) using the code below. i was trying graphics2d.draw(shape circle) but that doesnt work

	public Shape randomShape()
	{
		Shape circle = new Ellipse2D.Float(10, 10, 10, 10);		
		return circle;



switch(n) {
case 0:
   return  (((( [b]how do i return a shape??[/b])
case 1: 
return


You wrote it
switch(n) {
case 0:
   return new Ellipse2D.Float(10, 10, 10, 10);		
case 1:
   return new OtherTypeOfShape();


Was This Post Helpful? 0
  • +
  • -

#7 lostinclass  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 29
  • Joined: 18-March 11

Re: implementing the shape interface on JFrame

Posted 04 April 2011 - 08:41 PM

	public Shape randomShape()
	



ok i did that but i get a error: method must return a result of type shape

what does that mean..

i have to make a interface called shape ?
Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10825
  • View blog
  • Posts: 40,363
  • Joined: 27-December 08

Re: implementing the shape interface on JFrame

Posted 04 April 2011 - 08:47 PM

Post your code, as well as the line number(s) where the error message occurs. The Shape interface exists in the java.awt package, so you do not have to define it.
Was This Post Helpful? 0
  • +
  • -

#9 lostinclass  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 29
  • Joined: 18-March 11

Re: implementing the shape interface on JFrame

Posted 04 April 2011 - 08:48 PM

I guess i am not suppose to use case method

     public Shape randomShape()
    {
        Shape circle = new Ellipse2D.Float(50, 50, 50, 50);
        return circle ;
    


My instructor wrote this to me . What does this mean

Let's assume you wanted to randomly return a String with values "red" "green" or "blue". How would you do that? Probably generate a random integer, and use that to decide on the value to return for this particular method call?

Now think about returning different Shapes instead of "red" "green" etc. You've got a start below, though I'd use Ellipse2D.Double instead of Float, and tweak the constructor's parameter values; but the idea is headed in the right direction.
Don't forget variable scope.
Was This Post Helpful? 0
  • +
  • -

#10 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10825
  • View blog
  • Posts: 40,363
  • Joined: 27-December 08

Re: implementing the shape interface on JFrame

Posted 04 April 2011 - 08:51 PM

View Postpbl, on 04 April 2011 - 06:11 PM, said:

You wrote it
switch(n) {
case 0:
   return new Ellipse2D.Float(10, 10, 10, 10);		
case 1:
   return new OtherTypeOfShape();


You can use a switch block. Nowhere did your instructor say not to; in fact, I think he or she is encouraging it. Just do as pbl suggested.
Was This Post Helpful? 0
  • +
  • -

#11 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,913
  • Joined: 06-March 08

Re: implementing the shape interface on JFrame

Posted 04 April 2011 - 08:55 PM

Oups...

Ellipse2D , Arc2D, Rectanle2D, and RoundRectangle2D extends RectangularShape not Shape

If you can't cope whith RectangularShape you will have to effectively write a wrapper around it or an interface like

interface Shape {
  void draw(Graphics g);
}

class MyEllipse implements Shape {
    private Ellipse2D.Double x;

    void draw(Graphics g) {
    }
}


Was This Post Helpful? 0
  • +
  • -

#12 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10825
  • View blog
  • Posts: 40,363
  • Joined: 27-December 08

Re: implementing the shape interface on JFrame

Posted 04 April 2011 - 08:59 PM

@pbl: It will certainly be confusing to maintain having java.awt.Shape as well as a local Shape interface. I agree with you on the concept, but not on the naming conventions here. :)
Was This Post Helpful? 1
  • +
  • -

#13 lostinclass  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 29
  • Joined: 18-March 11

Re: implementing the shape interface on JFrame

Posted 04 April 2011 - 10:35 PM

View Postmacosxnerd101, on 04 April 2011 - 08:59 PM, said:

@pbl: It will certainly be confusing to maintain having java.awt.Shape as well as a local Shape interface. I agree with you on the concept, but not on the naming conventions here. :)


im totally lost, it wont let me return my 2 circles
the interface doesnt work
blah

public class RandomShapeGenerator
{
	int width, height;
	Random ran = new Random();


	public RandomShapeGenerator(int i, int j)
	{
		int width = i;
		int height = j;
		
	}

	public  shape randomShape()
	{
		int n = ran.nextInt(2);
		
		switch(n)
		{
		case 0:
			return new Ellipse2D.Double(50, 50, 50, 50);
		case 1:
			return new Ellipse2D.Double(100, 100, 100, 100);

		}
		
			
	}

}



can someone explain to me what he is talking about

     public Shape randomShape()
    {
        Shape circle = new Ellipse2D.Float(50, 50, 50, 50);
        return circle ;
    


My instructor wrote this to me . What does this mean

Let's assume you wanted to randomly return a String with values "red" "green" or "blue". How would you do that? Probably generate a random integer, and use that to decide on the value to return for this particular method call?

Now think about returning different Shapes instead of "red" "green" etc. You've got a start below, though I'd use Ellipse2D.Double instead of Float, and tweak the constructor's parameter values; but the idea is headed in the right direction.
Don't forget variable scope.
[/quote]
Was This Post Helpful? 0
  • +
  • -

#14 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,913
  • Joined: 06-March 08

Re: implementing the shape interface on JFrame

Posted 04 April 2011 - 10:44 PM

View Postmacosxnerd101, on 04 April 2011 - 10:59 PM, said:

@pbl: It will certainly be confusing to maintain having java.awt.Shape as well as a local Shape interface. I agree with you on the concept, but not on the naming conventions here. :)

Sorry, you are 100% right :^:
Must be time to go to bed, I totally missed that one

@OP no need to write your own Shape interface it already exists and Ellipse2D implements it so Ellipse2D IS a Shape your

Quote

ok i did that but i get a error: method must return a result of type shape

what does that mean..

i have to make a interface called shape ?

confused me. An Elllipse2D is a Shape post your exact error
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1