IITDU Forum
Would you like to react to this message? Create an account in a few clicks or log in to continue.

Shape design assignment: Sample Codes for painting help

4 posters

Go down

Java Shape design assignment: Sample Codes for painting help

Post by BIT0102-Mohaimin Wed Feb 16, 2011 12:49 am

Before you begin, please read [You must be registered and logged in to see this link.] tutorial for basic understanding of painting on swing component. There are two sample codes in that tutorial. The first code should be enough to understand this post.


As discussed in the class, first you need to create an abstract class named Shape, which will implement basic structure of a 2D geometric Shape.
Here is the structure:
Code:
public abstract class Shape{
   private List<Shape>   units;

   public Shape() {
      units = new ArrayList<Shape>();
   }
   
        /**
        * You can add a new unit to this shape from outside.
        */
   public void addUnit(Shape unit) {
      units.add(unit);
   }

   
        /**
        * You may want to remove a unit from this shape.
        */
   public boolean removeUnit(Shape unit) {
      return units.remove(unit);
   }

   public void draw(Graphics g) {
      for (Shape unit : units) {
         unit.draw(g);
      }
   }
}
The draw(Graphics g) method is important. So I am explaining. The loop in the method simply calls draw method of all units in this Shape. This method will be same for all the shapes, except the primitive ones (Line and Circle), in which it will be overridden, you will see letter.
Now you may be confused about the Graphics object passed in the parameter. A shape knows "how" will be drawn, but it does not know "where". The graphics object supplies the "where" information to this Shape. That means, it says, "Draw on me" or "Draw using me". In the primitive Shape, you will see how the Graphics object is used. Latter, you will see from where it will be created.

Now its time to define the primitive Shapes. I am implementing only the Line. Do yourself the Circle.
A line can be defined by two points. So the coordinates of the point is passed through the constructor.
Code:
public class Line extends Shape {
   private int x1, y1, x2, y2;
   
   public Line(int x1, int y1, int x2, int y2) {
      this.x1 = x1;
      this.y1 = y1;
      this.x2 = x2;
      this.y2 = y2;
   }
   /**
    * We are not supposed to add units in a primitive shape. So this method is
    * overridden and made blank. (A better approach is to throw an exception,
    * but I am not going to make this topic complex)
    */
   @Override
   public void addUnit(Shape unit) {
   }
   
   /**
    * Same logic applies here.
    */
   @Override
   public boolean removeUnit(Shape unit) {
      return false;
   }
   
   @Override
   public void draw(Graphics g) {
      g.drawLine(x1, y1, x2, y2);
   }
}
The implementation of draw() method is quite simple here. We have simply added the two points that defines the line. Exactly the same way we do it in paper.

Abstraction of composite shape and one of the primitive shapes are created, now lets create concrete composite shape. Simple one, a triangle.
Before jumping to the code, answer yourself, if I tell you to draw a triangle on a paper, how would you draw? You would draw three interconnected lines, right? So, a triangle is a composition of three lines.
Again, some of you would ask me instead, "with which points should I draw?", then I would possibly give you three points. Thats it. we need three points to define a triangle and we need to draw three lines to draw a triangle.
The design is complete. Lets code!

Code:
public class Triangle extends Shape {
   
   public Triangle(int x1, int y1,int x2, int y2,int x3, int y3) {
      addUnit(new Line(x1, y1, x2, y2));
      addUnit(new Line(x2, y2, x3, y3));
      addUnit(new Line(x3, y3, x1, y1));
   }
}
Triangle is done! Simply 3 lines are created and added as unit shapes of the triangle.

Ok, creation of Shapes are complete. Now we need to draw them on a Swing Component, preferably a JPanel.
We will call the JPanel on which we will draw a DrawingPanel. Once again I am telling, please make sure you have understood the tutorial I have suggested above. Otherwise you may not understand the codes bellow.
DrawingPanel will extend JPanel and override its paintComponent() method. Also, it will have an Object of type Shape, which will be drawn on this DrawingPanel. Lets go to the code.
Code:
public class DrawingPanel extends JPanel {
   private Shape shape;
   
   public DrawingPanel(Shape shape) {
      this.shape = shape;
   }
   
   @Override
   protected void paintComponent(Graphics g) {
      super.paintComponent(g);
      shape.draw(g);
   }
}
You may want to add additional functionalities in this class. I have implemented only the necessary ones. We have drawn the shape in the paint component method, with the Graphics object supplied as its parameter. So, the implementation of draw method will draw itself using the graphics of this DrawingPanel, that means it will be drawn on this Drawing panel.

Now its time to test our codes. Here is a sample run.
We will draw a triangle with the points (100, 10), (20, 180), (160, 170)

Code:
public class Demo {
   public static void main(String[] args) {      
      Shape triangle = new Triangle(100, 10, 20, 180, 160, 170);
      DrawingPanel drawingPanel = new DrawingPanel(triangle);
      
      JFrame frame = new JFrame("Shape Drawing Demo");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setSize(200, 300);
      
      frame.add(drawingPanel);
      
      frame.setVisible(true);
   }
}
The output should be like this:
[You must be registered and logged in to see this image.]
Thats all for now. Let me know if you have farther queries.
BIT0102-Mohaimin
BIT0102-Mohaimin
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : B+
Posts : 415
Points : 715

Back to top Go down

Java Re: Shape design assignment: Sample Codes for painting help

Post by BIT0122-Amit Wed Feb 16, 2011 1:03 am

I don't know what to say.
Saluting you won't be enough Neutral rep++
BIT0122-Amit
BIT0122-Amit
Founder
Founder

Course(s) :
  • BIT

Blood Group : O+
Posts : 4187
Points : 6605

https://iitdu.forumotion.com

Back to top Go down

Java Re: Shape design assignment: Sample Codes for painting help

Post by BIT0122-Amit Wed Feb 16, 2011 2:51 am

ইয়ে, import গুলা কি না দেবার বিশেষ উদ্দেশ্য আছে?
BIT0122-Amit
BIT0122-Amit
Founder
Founder

Course(s) :
  • BIT

Blood Group : O+
Posts : 4187
Points : 6605

https://iitdu.forumotion.com

Back to top Go down

Java Re: Shape design assignment: Sample Codes for painting help

Post by BIT0102-Mohaimin Wed Feb 16, 2011 12:04 pm

BIT0122-Amit wrote:ইয়ে, import গুলা কি না দেবার বিশেষ উদ্দেশ্য আছে?
To make the codes smaller. We all use excellent IDEs those will import them within a fraction of seconds.
BIT0102-Mohaimin
BIT0102-Mohaimin
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : B+
Posts : 415
Points : 715

Back to top Go down

Java Re: Shape design assignment: Sample Codes for painting help

Post by BIT0119-Asif Wed Feb 16, 2011 7:17 pm

Thanks
BIT0119-Asif
BIT0119-Asif
Study Moderator
Study Moderator

Course(s) :
  • BIT

Blood Group : B+
Posts : 163
Points : 432

Back to top Go down

Java Re: Shape design assignment: Sample Codes for painting help

Post by BIT0117-Ibrahim Thu Feb 17, 2011 2:14 pm

Thank you Mohaimin, for writing this type of easily understandable code and its details. So obviously rep++ for you.
BIT0117-Ibrahim
BIT0117-Ibrahim
Study Moderator
Study Moderator

Course(s) :
  • BIT

Blood Group : B+
Posts : 96
Points : 234

Back to top Go down

Java Re: Shape design assignment: Sample Codes for painting help

Post by Sponsored content


Sponsored content


Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum