Shape design assignment: Sample Codes for painting help
4 posters
IITDU Forum :: Academics (BIT) :: Semester 5 :: SE 506
Page 1 of 1
Shape design assignment: Sample Codes for painting help
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:
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.
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!
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.
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)
[You must be registered and logged in to see this image.]
Thats all for now. Let me know if you have farther queries.
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);
}
}
}
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);
}
}
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));
}
}
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);
}
}
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);
}
}
[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- Programmer
- Course(s) :
- BIT
Blood Group : B+
Posts : 415
Points : 715
Re: Shape design assignment: Sample Codes for painting help
I don't know what to say.
Saluting you won't be enough
Saluting you won't be enough
Re: Shape design assignment: Sample Codes for painting help
ইয়ে, import গুলা কি না দেবার বিশেষ উদ্দেশ্য আছে?
Re: Shape design assignment: Sample Codes for painting help
To make the codes smaller. We all use excellent IDEs those will import them within a fraction of seconds.BIT0122-Amit wrote:ইয়ে, import গুলা কি না দেবার বিশেষ উদ্দেশ্য আছে?
BIT0102-Mohaimin- Programmer
- Course(s) :
- BIT
Blood Group : B+
Posts : 415
Points : 715
BIT0119-Asif- Study Moderator
- Course(s) :
- BIT
Blood Group : B+
Posts : 163
Points : 432
Re: Shape design assignment: Sample Codes for painting help
Thank you Mohaimin, for writing this type of easily understandable code and its details. So obviously for you.
BIT0117-Ibrahim- Study Moderator
- Course(s) :
- BIT
Blood Group : B+
Posts : 96
Points : 234
Similar topics
» Design Pattern: Assignment Shapes
» Design pattern Assignment - fly weight pattern
» Java Tutorial: Painting in swing components, part 2 (Interactive painting)
» Different CURSOR shape
» Sample program using J2ME
» Design pattern Assignment - fly weight pattern
» Java Tutorial: Painting in swing components, part 2 (Interactive painting)
» Different CURSOR shape
» Sample program using J2ME
IITDU Forum :: Academics (BIT) :: Semester 5 :: SE 506
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum
|
|
Tue Sep 29, 2015 2:45 pm by Sophiawood
» Cisco EHWIC SFP/GE WAN Card
Mon Sep 07, 2015 11:08 am by Sophiawood
» Huawei S1700-28GFR-4P-AC Price
Thu Jun 25, 2015 2:31 pm by Sophiawood
» teach yourself C++ / Herbert Schildt Solutions
Wed Jun 03, 2015 1:52 pm by Abdullah Al Noman
» teach yourself c by herbert schildt pdf
Wed May 13, 2015 11:01 pm by Raquib Ridwan
» ASA 5506X With Firepower ASA5506-K9
Fri Apr 10, 2015 4:31 pm by Sophiawood
» New Trends in Deal Business
Tue Feb 03, 2015 9:38 pm by nersoa
» PoE Power Allocation for WS-C2960S-24PS-L
Wed Nov 05, 2014 11:12 am by Sophiawood
» How to cure back pain
Fri Oct 31, 2014 7:15 pm by Bergen Guildford