Saturday, October 10, 2015

IP : Basic Form to Java Servlet Communication Tutorial


If you haven't tried the "Hello World" program in the blog, I recommend you to try it. Or else, you won't be able to follow this tutorial. And, you can use my code.

http://semlabs.blogspot.in/2015/09/ubuntu-java-servlet-code-for-displaying.html

because, I am gonna use the files in that and teach you stuff in this tutorial. Should have taught the Hello-World program itself actually, but I couldn't because of time constraints.

Anyways, let's start!

First of all, get the Hello-World Code files.

Now if you see, the folder is named "hello". This is the name of your web app.

A web application or web app is a client-server software application in which the client (or user interface) runs in a web browser. (Source : Wikipedia)

You would have placed the "hello" folder in your webapps folder, which is present in the tomcat files directory.

Now, let's take a look at the hierarchy of folders and files in "hello" folder.

cd /opt/tomcat/bin

./startup.sh



Below is a series of screen shots of the file explorer in my Ubuntu system.






Now, in each screen shot, if you see the top part, you will be able to see the path of the directories. That should help you understand what file is in which folder.

And, by the way, the "src" folder is not necessary, but we just keep it for our convenience.

But other folders : "WEB-INF" and "classes" are important and they must be named in the same way as they are named over here. These stuff are case sensitive, so please be careful when you are naming them, when you create a new web app.

So, now that we know the hierarchy of the folders, we are going to create a new web app with this hierarchy, and we will create the files one by one.

Go to your webapps folder. Create a new folder named "formexample".

Go inside this folder and create a new folder and name it "WEB-INF" and inside "WEB-INF" create two folders "classes" and "src", like how it was created in Hello-World program.

Now, we are going to create the files. First of all, let me tell you the aim of the program.

The aim is to display a form in a web page, which can contain anything, for simplicity, let's say we are going to get details of a student, we will just get his name. And when the student clicks "Submit" in the form, the form reaches the server and the server should display "Hi Karuppiah, your form is submitted" in a new web page, if the name typed was "Karuppiah".

First, let's create the web page with the form.

<html>

<head>
<title>Example</title>
</head>

<body>

    <form method="POST" action="server">
        Name : <input type="text" name="studname"> <br/><br/>
        <input type="submit" value="Submit">
    </form>
</body>

</html>

Now, that will create the form. And note the name attribute for the first input tag. The first input tag is used to create a text box to get text input, our name. And the next input tag is to create a button (meaning type is "submit") for submitting our form. Note the action attribute of form tag. We will talk about this soon.

And we are using POST method to submit information to the server. We can use GET method too. Google about the differences!

Place the example.html in "formexample" folder.

Now, let's create the server.java , the servlet code, which is used to send response webpage, when the user clicks Submit button in the form.

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class server extends HttpServlet
{
    public void doPost(HttpServletRequest request,HttpServletResponse response)
    {
        PrintWriter out = null;

        response.setContentType("text/html");
               
        try
        {
            out = response.getWriter();
           
            String studname = request.getParameter("studname");

            out.println("Hey " + studname);

            out.close();

        }
        catch(Exception e)
        {
            out.println(e);
        }

    }

}

The class's name is server. And since, we used POST method in our form, the method used is doPost(). It's actually a built in function of HttpServlet class, which we override for our purpose. And if we use GET method in our form, we override doGet().

The "request" object contains all the data sent by the client to the server and response is the object which is going to be sent by the server to the client as a response to the request. We use the request object to get the information typed by the client. Here, we use "studname" , the name attribute of the form, to refer to the  input tag in the form and to get the value typed in the text box in the form, by using getParameter() function. And to write to the response object, that is put data in the response object, we use PrintWriter class object "out". So, after getting the information typed by the client, we put this information along with the word "Hey " in the response object using "out" and now we are done. The servlet engine will take care of sending this response to the client who sent the request.

Place this server.java file in "src" folder, which is in formexample -> WEB-INF -> src

And compile this java program using the command

javac -cp /opt/tomcat/lib/servlet-api.jar server.java

If you installed your tomcat somewhere else, accordingly change the path in the command, to the appropriate path, instead of "/opt/tomcat" .

Place the class file in the "classes" folder, which is in
formexample -> WEB-INF -> classes

After this, we need to define the web.xml file

<web-app>

    <welcome-file-list>
        <welcome-file>example.html</welcome-file>
    </welcome-file-list>

    <servlet>
        <servlet-name>server</servlet-name>
        <servlet-class>server</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>server</servlet-name>
        <url-pattern>/server</url-pattern>
    </servlet-mapping>
</web-app>

"welcome-file" tag is used to mention the first page or welcome page of the webapp. Others are self explanatory. "servlet-name" is the name of servlet, you can give anything. "servlet-class" is the name of the class file which we get after compiling the servlet's java file. Here we created server.java, so when we compile, we will get server.class , so the class name is "server".

And coming to "url-pattern" , well, this will tell the url to be displayed when the response page of the server is displayed. And remember to put the forward slash along with the name you want in the url. For me it's "/server". And the name you mention here in the "url-pattern", by name, I mean "server" in my case, excluding the slash; this is the same name as the one provided in the action attribute in the form tag! If the url pattern name and the action tag's attribute are not the same, you are gonna have issues!

And after creating this web.xml, place it in "WEB-INF" folder, which is in
formexample -> WEB-INF

Now, go to the tomcat's bin folder and execute "startup.sh"

cd /opt/tomcat/bin

./startup.sh

And then, go to your browser and type

localhost:8080/formexample

And then type a name and press Enter !

Below are some screen shots. Note the URLs in the images!

Below are the source files !












Tuesday, October 6, 2015

Tips For IP Lab ?

[ This is the same stuff that I sent for IP Model Lab, through mail. If you haven't seen the mail, see this ]

** Note : BEWARE. It's a long post! Never thought I would type so much! **

Our IP Semester Practicals is gonna come soon. It's high time you started coding, if you haven't.
And it's not all about just knowing how to code.

"What else do I need to know?" - you need to know the possible errors while you code. And it's not very easy this time, if I have to tell the truth.

There will be a hell lot of possible problems/errors, when it comes to Servlets and JSP stuff. And I think it will be more, when it comes to XML, I don't know much, since I am yet to learn it. And, don't think the compilers will help you. Sometimes, there are database problems, which don't appear in the webpage, unless you use some good error catching and error printing techniques, which I will talk about later. And then other times, even when compilers help you...it's not enough help. It's really tough to understand what's the actual problem.

Besides all this, there's our crappy lab. Sometimes there are missing files. Sometimes, you have to do some manual stuff to compile things. Like, in servlets, there's this servlet-api.jar file that you need when you compile the servlet program. So, if it's not available along with your java library files, well, you will have to use an option called "classpath" while compiling, and use the servlet-api.jar present in the lib folder of the apache-tomcat files. If you have already coded some servlet programs, especially, in your own pc, you would definitely know all this, because sometimes, that servlet-api.jar is present in some of the lab computers and we never know what we did in lab.

That's just one thing, that I talked about. There's one more important jar file for servlet programs...and even JSP programs. It's mysql-connector.jar , do google about it. I know very little about it. It provides standards-based drivers for JDBC, ODBC etc to use MySQL in any application (Googled it). And then, there are some jar files to use mail related functions, in the surgemail exercise, i suppose. These are some problems of missing files. I hope the lab is ready without such problems.

And then there are some technical/logical problems that I faced. Like, there was a problem, where my webpage would never open in the browser. All my files were indeed right. The problem was that, my server was not running properly even though I had started it using startup.sh ; Meaning - even the tomcat homepage wouldn't open in that case. But I was going straight to my own homepage, I mean, my webapp's homepage, so I never realized this problem. Later, I found out that I didn't shutdown the server and simply ran the startup.sh twice or thrice. Sometimes it's a problem. I m not sure about the intricate details. So, better startup only once. And if you changed your files (even html files) , do shutdown once and startup once. Only once. Besides, if you try to use shutdown.sh twice in a row, it would give you an error saying tomcat might already be shutdown and might not be running. That's not the case with startup.sh ; So, be careful. And always open the tomcat homepage first, check if it's working, then go to your webapp's page.

Then there's this crazy problem with cached pages. Sometimes, even when you change your html page. and even when you reload, the browser sometimes loads the same old page, using it's cache, instead of the new page. Caching is usually done for optimization, but this might be a problem for us, of course. So, if you change html files, do check the source html file by right clicking on the page and selecting "View Page Source". And this is also a good habit to check if your html page is rendered properly.

Besides all this, there are some HTTP Response Codes that you need to learn.
Especially, the Error Code, which are like 4xx and 5xx, some 3 digit numbers starting with 4 and 5.
4xx means client error : that is, the client is doing something wrong. Probably, you are requesting for some wrong resource and it would say "Error 404 : File or resource not found". That's a very common error. This way, you can realize there's something wrong you are doing on your side, like there's 5xx error, where there's problem in the server side. But yeah, 4xx error is not that helpful at all times though. If you want to know what are the possible causes, you could mail me and I will tell you whatever problems I have encountered till now. One of them, is the worst. It was a problem in my web.xml and it took me so much time to realize it. Even a small "/" closing tag slash can be a problem. I missed it for the closing tag of url-pattern tag. And my webpage kept saying 404 File Not Found. But tomcat homepage opened. That should mean how important the web.xml file is. Let it be servlets or JSP. But yeah, in JSP, just telling about the welcome file in the web.xml is enough I suppose. All the jsp-file tags is not necessary. Anyways, it's all your wish. And then, I encountered this another error, which is error 405 : Method Not Allowed. You should google that out and find about it.
5xx means server error : that is, there is some problem in the server side. Like, some compilation problem occurred, after converting a JSP file to a servlet java file and trying to compile it. There will be errors like ClassNotFoundException and stuff like that, like how it is displayed while compiling. I encountered a lot of 5xx errors while doing JSP programs. But this time it's good, because these errors are shown in the page. These are compiler errors mostly. So, you can understand them to some extent. There was another problem that I encountered while trying JSP programs. I believe it was some ClassNotFoundException...later I tried to start the tomcat server as root, that is run startup.sh as root, and my page worked fine. I am yet to find out more about it.
So that's some good list of possible errors...I am not trying to scare you. I m just telling you that you should start concentrating on these too.
It's not bad to make mistakes.... have some errors in your code and stuff. But you should know what probably caused it and what might be the possible solution. You should be smart enough to debug it. Or you cannot afford to make mistakes. So, start coding. And code from scratch. Like, really from scratch. So, that, you can know what mistakes you do. And to know what are the possible errors. Like, seriously. And understand stuff at least at an abstract level, to start with.
If you still don't understand the seriousness, you can read about an incident (see below) that happened to me.

Recently, I was trying to code a Servlet program in lab. The student information system. I coded the core of the program. Like, the basic stuff for a servlet and compiled and stuff and opened up the webpage to see how it works and I got some big errors saying 'can't access members with modifiers "" ' as far as i remember. I was actually using some variables in my servlet class, to use it in the different functions that I have. I never declared if they are private or public or what. So, I thought that was the mistake. And tried changing them, but nothing helped. Later, when I googled and check StackOverflow, they said I have to declare my Servlet class as public like :

public class server extends HttpServlet{


}

like that. So, I missed the public keyword. Well, we usually don't use it when we write programs. We just write "class employee { }" and similar stuff. Anyways, I changed that and it worked fine. I didn't look much into the error though. Like, why it was a problem. It was saying something about not being able to call or access the server class, I suppose. Do google if you are curious and if you find the problem, please do share it with me.

So, that's an example of how crazy mistakes can happen even because of a single keyword like "public".

Saturday, October 3, 2015

Computer Graphics : 3D using glut Tutorial




Thanks to whoever mentioned that comment (in cplusplus.com ) about this super cool site. It's called

www.videotutorialsrock.com

I have just started to check out the material and the videos. Looks like they will help a lot! Do check it! :D It's only for 3D though. And I think we need more knowledge about 3D, as of now...because we are well versed in 2D.

And there's one more comment



Check this site too! :)

www.zeuscmd.com/tutorials/opengl/index.php




Tuesday, September 29, 2015

Computer Graphics : Code For Cohen Sutherland Line Clipping !


It's high time you started coding...

And I don't know how this post helps...because I am just providing you the code...

Try coding yourself and refer my code if you want to. And if you are learning from my code (I know it's rare, still), please be sure about knowing the concept before checking my code, and I hope it's not too tough to understand.

Cohen-Sutherland Line Clipping Code

Tuesday, September 22, 2015

IP : Installing Surgemail and adding mail accounts and How to use


First, you need to download a file. Go to

http://netwinsite.com/cgi-bin/keycgi.exe?cmd=download&product=surgemail&

and choose your Linux distribution. I chose " Linux (All 64 bit variants) " since I have Ubuntu 64 bit.

After downloading, you should have a .tar.gz file , place it in your home folder and extract it by right clicking it. It should give you a folder named "mtemp" and if you don't have any extracting application, then open your terminal and type this 


tar -xvf surgemail_XYZ_linux.tar.gz

replacing surgemail_XYZ_linux with whatever your file is named. Like, my file is named as surgemail_70c2_linux.tar.gz , so i used this command : 


tar -xvf surgemail_70c2_linux64.tar.gz

Now you should have a folder named "mtemp". Now use the terminal and change the directory to this folder using 'cd' command. And now, type

sudo ./install.sh

With this, your installation should start. Press Enter when it asks if it's ok to install in the default the installation path.



Now, type "karupsmail.com" as the full domain name. And type the host name as "mail.karupsmail.com" and then username for web administration as "root" and password as "root" too, and retype it.



Now, it asks for email account for management emails and alerts, and tells the default is "root@karupsmail.com" and now, just press enter.

The installation proceeds.

And it will be complete in a few seconds.




Press enter to exit.

Now, the server is started. To check it, go to

http://127.0.0.1:7026

Now, you will see a web page with many options and buttons. Some of the options that we need are gonna be discusses below. One of them is "Create User Account".



Just click this button and you will be asked the details of the new user. Just type the username and password. And create two users, so that, later, you can use one to send and another to receive.

I have created users : "karuppiah" and "keshav" , with password as their username. So now, their email ids will be "karuppiah@karupsmail.com" and "keshav@karupsmail.com"


Now, you just have to click  "Manager Info" on top left and then click "Web Email Client" and then login to your email accounts to see a GUI interface.









But we have to write java code to actually send mails using code and not using these GUI stuff.

Here's a basic code for Sending and Receiving Text Mail for my mail server :



You can change the code according to your own surgemail configuration ! :)


And finally, you need two jar files : mail.jar and activation.jar which contain the packages which start with the name : javax.mail.x and javax.activation.y

And we import them like 

" import javax.mail.*;
  import javax.activation.*; "

Here are the two jar files :



Place them in

/usr/lib/jvm/Your-Java-folder/jre/lib/ext/

Where "Your-Java-folder" is the name of the folder (containing java files) based on whatever java version you have.

And to place them in the above path, you should have root permissions. So, do it through terminal, using sudo and cp commands. 

Saturday, September 12, 2015

IP : Java Servlet. Code for displaying "Hello World"


First install tomcat apache server using this post :

http://semlabs.blogspot.com/2015/09/installing-tomcat-server.html

I am assuming you have your tomcat files in /opt/ , or else, change the path accordingly in the below steps and replace "/opt/".

Put this "hello" folder in /opt/tomcat/webapps/

hello - code.

The above folder contains all the code needed to print "Hello World" in your server response page.

To start tomcat server, type these commands in the terminal :

cd /opt/tomcat/bin/

./startup.sh

and type this in your browser :

http://localhost:8080/hello/HelloWorld

This should print "Hello World".

Here "hello" is the web-app , and "HelloWorld" is the url map for the servlet class file, which is also named "HelloWorld".

This is how I coded this Servlet :
  • Created a folder called "hello" in /opt/tomcat/webapps
  • Created a folder called "WEB-INF" inside "hello" folder
  • Created two folders called "classes" and "src" in "WEB-INF" folder.
  • The "WEB-INF" and "classes" folders are very important and must be named same way. I mean, it is case sensitive, and is important.
  • To understand the hierarchy in a better manner, download my "hello" folder!
  • Then created HelloWorld.java and coded it based on Servlet programming and placed it in "src" folder.
  • Created web.xml and defined the servlet name, servlet class name and defined the mapping.
  • Compiled the HelloWorld.java like this :

         javac -cp /opt/tomcat/lib/servlet-api.jar HelloWorld.java




  • Placed the HelloWorld.class file produced the previous step in "classes" folder.
  • Started the server using commands mentioned above and checked the output.


  • To know more about what each of these things mean, and to also know how to code it, look at this pdf by tutorialspoint :

    Servlets

    For any doubts or problems, mail me. This is the most basic Servlet program. This should give you a feel of how to code Servlets! :)

    IP : Installing Tomcat Server in Linux


    I am showing the procedure using Ubuntu. And don't worry, installation procedure won't change for different Linux distributions. I am just mentioning that I am using Ubuntu.

    Installing Tomcat Server :

    Follow these steps with some slight modifications that I am mentioning:

    https://www.digitalocean.com/community/tutorials/how-to-install-apache-tomcat-on-ubuntu-12-04

    First of all, Mam was telling that there are some problems in installing tomcat using apt-get command, I suppose some packages will be missed. Not sure. So, I downloaded the tar file from the url :

    http://apache.mivzakim.net/tomcat/tomcat-7/v7.0.64/bin/apache-tomcat-7.0.64.tar.gz

    the link keeps changing, so if you get 404 File Not Found, go to http://apache.mivzakim.net/tomcat/

    and try downloading newer versions.

    Now, follow the steps in the Digital Ocean website from the "untar file" step using "tar" command. And where ever they say "~/path/to/tomcat" use "/opt/tomcat"

    After following all the steps, you should have all the files of tomcat in your /opt folder.


    Tuesday, July 21, 2015

    Boolean Expression Evaluator : App by Karthik


    This is an app by a friend, based on the Logic Lesson in Discrete Mathematics.

    App Link

    You can send your feedback and appreciation directly to him at : karthik_m_a_m@outlook.com

    He develops apps under the name of Kappspot ( www.kappspot.yolasite.com )



    Sunday, July 19, 2015

    IP Lab : Java GUI based on Frames


    I had a tough time searching google for this one.

    Many sites talked about something called "Swing" for GUI Java Applications. It's some widget toolkit. Which is the new one, actually.

    But Felix Mam was actually showing some codes in Internet, in class, and it was something different from what I saw about Swing. Then later, I realized, she showed the code based on AWT (Abstract Window Toolkit) which is an old one compared to Swing. Swing is some sophisticated thing it seems. That's what Google says.

    I am gonna follow Mam and code using AWT stuff.

    So, here's some basic material I found. I will post more when I find more. Even you could Google about AWT and AWT Events.

    http://www.studytonight.com/java/java-awt.php

    http://java-demos.blogspot.in/2012/10/close-awt-frame-in-java-using-windowlistener.html

    http://java-demos.blogspot.in/2012/04/using-windowlistener-for-awt-frame.html

    http://www.landofcode.com/java-tutorials/java-gui-layout.php

    Saturday, July 18, 2015

    Computer Graphics : Transformations


    According to the lab exercise, we need to use homogeneous coordinates. That's a catch, since we need to use matrix and stuff for that.

    I started out with the basic implementation, instead of the matrix stuff. Here's a code for the basic thing. You could build it up in a similar manner for the homogeneous coordinates implementation. I didn't do shearing and reflection :P

    Code

    PDF to learn the transformations :

    PDF

    Thursday, July 16, 2015

    IP Tutorial Problems Solutions


    These are some of the solutions for the tutorial problems.

    I am uploading whatever I have and whatever I am coding.

    Keep checking the link for solutions to other problems.

    For updates, may be you could send me a mail to add you to the exclusive subscribers group.

    Tutorial Problems and Solutions

    Saturday, July 11, 2015

    IP Lab Ex 3 : Socket Programming - UDP


    Now, we gotta learn some basics in socket programming in Java. You could use internet and check out about it. I have a basic code over here, that I found in many sites :

    Source Code

    And in this code, they used BufferedReader Class Object and all, for obtaining the input from the user. You could actually use the one with the Scanner Class Object.

    Rest of the code is kinda self explanatory, based on networking. And there is a while loop in the server code, just to show that the server is infinitely running and provides the service to many clients.

    Friday, July 10, 2015

    Computer Graphics : Target Board


    Drawing the target board is a just very simple. You just need to code for circle, then draw concentric circles.



    Computer Graphics : DDA Algorithm Implementation


    Now this is revisiting for those who saw the old post or just saw the lab exercise Q, and tried something in DDA and got some output. For others, it's totally new and an easy road, since I am giving a part of the code. But seriously, I recommend you try something at least, before seeing my code, or else...it's like you telling yourself that you can't code it. So, have some self-respect.

    DDA Code

    It's just a part of the code. I was actually thinking to put up the simple math, using which I arrived at this implementation. Then I felt that you can do it yourself. Just in case you have problems with the math, the equations, the conditions and how they are justified, just mail me. I will explain it.

    Besides, you might think DDA is just too simple and that these many posts might not be needed, remember, even these small things matter, if you can understand this, you can move forward to Bresenham with some confidence. It's all about math and understanding. And if you understand Bresenham, then Circle and Ellipse will follow one by one. It will be like a piece of cake. I will try putting up an explanation for all the equations.

    Thursday, July 9, 2015

    Having Bugs in CG Code ?


    I see a lot of people having problems in CG codes.

    In codes like DDA, especially when it comes to the different cases.

    And some have problems in deriving stuff in Bresenham's line algorithm for cases other than positive m, m<1.

    And take it from me, try not to look too much into the "Left to Right" and "Right to Left" thingy in the line algorithms...try to generalize that concept in your implementation, and it will consequently decrease the number of cases.

    And try some math before coding and understand what's actually going on.

    And you can always mail me your code, I will help you out ! :)

    My mail id : karuppiah7890@gmail.com

    Wednesday, July 8, 2015

    IP Lab Ex 2 : Creating a Site

    We were actually told to create site using many tags...very new ones too, like <svg> , <canvas>

    I have no idea about them, some graphics stuff I heard. But no big deal, you could actually go to www.w3schools.com site and search any tag you want.

    But then, use tags only if you need them. You don't have to use them just for the sake of using them.
    Though I did use tags like <header> and <footer> just like that :P I know right, I am one to talk.

    Now, from examination point of view, I don't think you need a looooot of creativity, may be you just need some. And some basic knowledge about websites, like how they look, and you should be done.
    Rest all is about presentation, and appropriate usage of tags and I used even JavaScript, very basic stuff actually. I learnt it from w3schools site, actually you can learn from anywhere.

    I learnt some "event handling" stuffs, like what happens when you click something, what happens when you roll over your mouse over something. It's really cool how your website can be dynamic. And I heard you can even do animations using JavaScript or even JQuery, which I mentioned in an earlier post.

    There are lots of concepts out there, like " DOM Elements " and stuff, I don't know much about them, but you could take a look if you are interested. If you have been active in Paradigm work and have seen some seniors asking you to login to your facebook account to invite your friends using scripts, well....it's JavaScript (again JQuery can be used) and while writing those scripts, these DOM concepts will be really useful. They just run these scripts in the browser console (? Google it, I just did :P), to simulate a click on the "invite" button on the page.

    There are really some cool stuff out there like these! Get a glimpse at it! We are Computer Science Engineers and we should know these!

    My Website

    It's not that good actually. And if you are gonna see it, I would like you to know that no ebook pdfs are attached :P and some video links are missing :P and if you click the "SSN" word on top, you go to home page. Well, I am saying because the cursor symbol doesn't change to a hand symbol (like for a link) when you roll over the "SSN" word on top :P

    P.S : That's one big post! And if you read between lines or just came straight to "P.S"... no wonder ! :P

    Saturday, July 4, 2015

    Computer Graphics : Smiley Face :D


    This smiley face was drawn using ellipse and circle. Looks cool, right ?;)

    Probably I should try the " :P " smiley too :P it's my favorite :P


    Computer Graphics : Midpoint Ellipse Algorithm


    Again, just a matter of formulas, and then you can code this one easily. And it's more easier if you have already done midpoint circle algorithm.

    Some Theory

    Here's a glance at the output :



    Friday, July 3, 2015

    Computer Graphics : "WAX" word


    Now that's a word drawn using lines, using bresenham's algorithm. I passed the end points of the lines as parameters for drawing the lines.

    And yes, you need to code for negative slope too, in bresenham's algorithm, where |m| < 1 and |m| > 1. As you see, the left most line in the drawing of the letter 'W' has negative slope with |m| > 1, so it requires a code which can draw lines with negative slope. And so do many other lines in the above drawing. So, find equations for negative slope too and try to code accordingly.

    By the way, I haven't used standing lines, since my word didn't need it, but if you are using standing lines, like for the stem in the letter 'T' , then you have to be careful, you see, in standing lines, the slope is not defined as

    m = (y2 - y1)/(x2 - x1)

    And in standing lines, x2 - x1 = 0, so m is not defined. So code properly for this situation and then the other cases are |m| < 1 and |m| >= 1.

    Wednesday, July 1, 2015

    Learn Programming App


    It's a cheatsheet app. Cheatsheet is something which is used to refer to when you forgot what you learnt...but it is also a good way to grasp and learn things faster...at least things like HTML tags...Heard it's nice...and it has cheatsheets for many languages and even for linux basic commands

    App Link

    Computer Graphics : Bresenham's Midpoint Circle Algorithm


    Theory for the algorithm

    That's some good basic theory there, like how it is taught in class. For detailed theory, check your class notes :P ... I am sure the topic is taught.

    And using this theory, you can code very easily based on the formulae.

    And use the formula only to plot one octant of the circle. And then using mirror image logic - symmetry logic , you can find other pints for the other octants. See the above link.

    Actually, I never knew about this octant stuff and mistook it as quadrant, since I didn't attend the class or check the above theory properly and you should see the output I got, based on 'quadrant' theory and symmetry plotting :


    Now, that's some bad circle due to some crazy math which I am yet to check out (Care to help me ?)

    So, use the octant theory and the actual output should look something like the below. And you can actually click the images to see them in full size. Don't mind the cross...I put it to check the accuracy and stuff...looks like a sniper view in a shooting game, doesn't it ? :P Mail me if you have problems understanding this theory :)



    Monday, June 29, 2015

    Computer Graphics : Digital Differential Algorithm (DDA)


    We don't have this in the lab exercise, but if you haven't done Bresenham line algorithm, you could start with this, then do Bresenham algorithm.

    Anyways, if you try it, it's just good to see how this works too. You might not see much difference when compared to Bresenham, based on accuracy and stuff...at least I couldn't...may be I should run them both with same input and keep the output windows nearby and try to check the difference, and use some magnifying glass too probably...:P

    You should try it! And this algorithm considers negative slope also, so code for that and the code should work even if the two points are given as input in different order, like point A first then point B or vice versa. It's obvious, still, just reminding you.

    I was planning to post the code, since it's not a lab exercise, but since I asked you to try it, first try. And if you don't get it, mail me and I will help you with your code :) and later, may be I will provide my code if necessary.
     


    Computer Graphics : Basic Circle !


    Well, our teacher didn't teach circle yet, but I wanted to try it with the basic circle formula, like how I tried for basic line

    This is how it looks :


    You should notice the accuracy problems at a few points...I gotta analyze some things and try something different for those parts of the circle

    This is the code :

    Basic Circle Code

    Computer Graphics : Bresenham Line Algorithm


    The formula for m<1 has been implemented below.

    The formula for m>1 should be found out and implemented.

    Though I don't know if we should even find out the formulas for negative slope where |m| < 1 and |m| >1 ; for now, you could just try for positive m for which m>1

    Bresenham Line Code



    Saturday, June 27, 2015

    Internet Programming : map tag


    The first post based on Internet Programming Lab!

    Ex 1 is based on basic HTML 5 tags, like, paragraph, break line, anchor, image, head,title and also based on the attributes of tags like src in img tag and href in anchor tag

    Learn them from google and w3schools.com

    Then there's map tag, below is an example :

    <img src="planets.gif" width="145" height="126" alt="Planets" usemap="#planetmap">

    <map name="planetmap">
      <area shape="rect" coords="0,0,82,126" href="sun.htm" alt="Sun">
      <area shape="circle" coords="90,58,3" href="mercur.htm" alt="Mercury">
      <area shape="circle" coords="124,58,8" href="venus.htm" alt="Venus">
    </map>
     
    You can try the code in this page if you have never used this tag : 

    http://www.w3schools.com/tags/tag_map.asp
     
    In the above example, you will see that different parts of the image is mapped to pages that  contain enlarged image of that part. It's planets here.

    About the attributes. To give the coordinates for circle shape, you need to know the center of the circle and the radius.

    It goes like : x,y,r
     
    where (x,y) is the center and r is the radius.
     
    You can find the points by opening the  image in an editor that can tell the coordinates of various points on the image just by scrolling over the mouse over to that point. Editors like Paint in windows and KolorPaint in CentOS can do that.
     
    For rectangle, you need to the points on the opposite sides of a diagonal, like the left top corner and the right bottom corner point of the rectangle.

    left top : (a,b)
    right bottom : (c,d)
     
    The attribute is like :
    coords="a,b,c,d"
     
    Try it out if you haven't! It's nice! :)

    Feedback and About Blog


    If you are visiting this blog, and if you think it helps you, do give feedback through comments or mail, about what you expect from the blog. Like, is it good to have posts that teach you how to do stuff ? or you want code ? or both ? or what ?

    About posting code...well, I am really not the type of person who just copies code or encourages the same. Though I must say, I never typed much of SQL queries in 3rd sem and had lots of issues learning it later.

    Most times, it's some lazy person asking for code...very few people get code from others and learn from it, by analyzing it. And believe me when I say - One of the toughest jobs is, understanding another guy's code. I would say you better break your head and try writing the code instead of breaking your head trying to understand the way someone else thinks. Just a suggestion.

    So, codes won't be posted much. At least, not exact codes for lab work, though related codes and references will be posted.

    Don't forget to give feedback!

    Friday, June 26, 2015

    Computer Graphics : Just some Line Algorithm


    Try analyzing the below code. It's not exactly DDA algorithm. It just uses some basic math like DDA, and the trick for 'sampling' is based on the slope value.

    In DDA algorithm, the equations for the coordinates are recurrence equations, here I haven't used recurrence stuff, that's it.

    Refer this pdf if you don't know about DDA algorithm :

    Line Algorithms PDF


    The Code :

    Basic Line Code

    And do check on the glVertex2f() function...I am checking on it too...if that works fine, then we don't need to go for better algorithms etc...we can pass floating point arguments and plot the line. Though it doesn't make sense...like, pixels are discrete countable stuff. So, we can't just say plot a pixel at ( 234.6 , 156.7 )..it looks weird...Look about it. And do mail me if you get something.

    Computer Graphics : Triangle Primitive


    Triangle Fan

    #include<GL/glut.h>

    void myInit()
    {
        glClearColor(1.0,1.0,1.0,0.0);
        glColor3f(1.0,0.0,0.0);
        glPointSize(1);
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        gluOrtho2D(0.0,640.0,0.0,480.0);
    }

    void myDisplay()
    {
        glClear(GL_COLOR_BUFFER_BIT);

        glBegin(GL_TRIANGLE_FAN);
            glVertex2d(200,200);
           
            glVertex2d(150,250);
            glVertex2d(250,250);
           
            glVertex2d(300,225);
            glVertex2d(300,175);
           
            glVertex2d(250,150);
            glVertex2d(150,150);
           
            glVertex2d(100,175);
            glVertex2d(100,225);
            glVertex2d(150,250);
        glEnd();

        glFlush();
    }

    int main(int argc,char* argv[])
    {
        glutInit(&argc,argv);
        glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
        glutInitWindowSize(640,480);
        glutCreateWindow("First Exercise");
        glutDisplayFunc(myDisplay);
        myInit();
        glutMainLoop();
        return 1;
    }

    This was nice...you should try this


    Wednesday, June 24, 2015

    Computer Graphics Ex 1 : Primtives


    If you have attended Computer Graphics Lab, read further.

    This is some good page I found about primitives, except for Polygons :

    https://www.opengl.org/wiki/Primitive

    I was planning to put up some codes and show some outputs based on the above explanation...anyways, most of them are easy, and you would understand them on your own, if you read it along with the examples given in the above link...

    If you still want to look at code and some output pictures, here's a link I found out just now :

    http://www.codeproject.com/Articles/23991/OpenGL-Geometric-Primitives

    The above link has code, output pictures and some stuff about polygons, actually, convex polygons, which I never understood when I just tried the code and checked the output to understand it by trial and error...It's still a bit confusing for me...so, read it slow and twice...other than that, everything should be okay with some reference using the links and practice.

    And if you have attended the lab, you would know that Ex 1 is just about drawing these simple primitives. And then we have a chess board drawing. It's cool, try it soon! You can draw these using the primitives, which makes things easier. But later, I think, we will even do stuff like - use just points to draw stuff. Just point primitive. Cool, isn't it ? :P

    Tuesday, June 23, 2015

    Java Compiler for linux


    You could use eclipse editor and stuff. This is the simplest method, where you just need any editor, like gedit, and a java compiler and java interpreter (Java Runtime Environment etc) for executing, I don't know much actually.

    To compile a java program in linux, you need the java compiler, javac... like gcc for C and g++ for C++

    To get javac, type this in terminal :

    sudo apt-get install openjdk-8-jdk

    and to execute, you need the 'java' command, I think it's preinstalled in the new versions of ubuntu, so, just type in the terminal :

    java

    and you should see many options for the command. Which means, it is already installed.

    Or get it by typing this :

    sudo apt-get install openjdk-8-jre


    To verify it's working, open gedit or some editor and type this code :

    class test
    {
        public static void main(String[] args)
        {
            System.out.println("Hello World");
        }
    }

    And save this as test.java (don't change the name)

    and run the following commands one by one :

    javac test.java

    java test

    and it should print "Hello World" as output. Good luck ! :)


    Argo UML for linux and windows

    For Windows, download the setup file from here :

    http://filehippo.com/download_argouml/

    and install it.


    For Linux :

    Download this archive :

    http://argouml-downloads.tigris.org/nonav/argouml-0.34/ArgoUML-0.34.tar.gz

    and put the archive in the home folder. Now, type these commands in the terminal one by one :

    tar -zxvf ArgoUML-0.34.tar.gz

    chmod +x argouml-0.34/argouml.sh

    ./argouml-0.34/argouml.sh

    The last line will open up Argo UML. And the above process will create a folder in your home folder, named "argouml-0.34", this is the place where the software files are kept, and this folder will have the .sh script file to run the application. This is understood from the last line.

    Monday, June 22, 2015

    Computer Graphics - getting glut library


    We need glut to practice all our code, because the code contains functions from the glut library...so to get the glut library, you need to type this in the terminal :

    sudo apt-get install freeglut3-dev

    Now, just to verify, try this code :

    #include<GL/glut.h>

    void myInit()
    {
        glClearColor(1.0,1.0,1.0,0.0);
        glColor3f(1.0,0.0,0.0);
        glPointSize(1);
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        gluOrtho2D(0.0,640.0,0.0,480.0);
    }

    void myDisplay()
    {
        glClear(GL_COLOR_BUFFER_BIT);

        glBegin(GL_LINES);
            glVertex2d(100,100);
            glVertex2d(100,200);
        glEnd();

        glFlush();
    }

    int main(int argc,char* argv[])
    {
        glutInit(&argc,argv);
        glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
        glutInitWindowSize(640,480);
        glutCreateWindow("First Exercise");
        glutDisplayFunc(myDisplay);
        myInit();
        glutMainLoop();
        return 1;
    }

    save the above code as sample.c

    and to compile, type this in the terminal :

    gcc sample.c -lGL -lGLU -lglut -o sample

    ( it's 'l' , that is, a small L after every hyphen )

    and now to see the output, type the usual thing :

    ./sample

    The output should appear in a new window, and the output is a red line at the left bottom of the window. This way you can verify that glut libraries are installed properly and that you can run programs which use glut library functions, good luck! :)

    Sunday, April 12, 2015

    Saturday, April 11, 2015

    Reversing String using RPC


    It seems that last year, everyone got 2 questions each, and that almost everyone got one question as - Ping / RPC / NS2

    Learn Ping too :P
    And do check the possible questions pdf

    Networks Lab Possible Questions

    Below is the code ( all the files ) for string reverse using RPC.And check the README

    String Reverse using RPC



    Character Stuffing Code



    Below is the code for character stuffing.
    Client is the sender.
    Server is the receiver.

    Character Stuffing Code



    Friday, April 10, 2015

    Bit Stuffing Code


    Below is the code for bit stuffing.
    Client is the sender.
    Server is the receiver.

    Bit Stuffing Code



    Thursday, April 9, 2015

    MASM and DOSBOX

    For MPMC, I hear a lot of stuff about MASM. Saying that it's highly probable to get a MASM question in the semester lab exam, since there are not much kits etc :P

    However, it's always good to learn and know as much as possible. So, if you don't have MASM, get it!


    The above link also has DOSBOX installation files. And the 8086 folder will also have a simple asm code to display a string, along with it's obj and exe files.

    Below is a video that will help you with the basics on how to use DOSBOX and MASM. Start soon!


    Images for the Bind Error situation.


    The above picture shows a server running, and in a terminal, this command has been executed :

    netstat -a -tulpn

    and it shows the process id and name of the processes and the ports they are using and their states, then it shows the type of connection - udp,tcp and all.

    Now, below are two images to show how server was close unexpectedly using Ctrl+C ( you should actually use Ctrl+Z ) and client is also close unexpectedly.



    Now, here, I closed server first, then the client. Both in an unexpected manner using Ctrl+C.

    If you use the netstat command, you will see this :


    I used grep with the port number, to get processes that have the port number. Actually, now, the port number is being used by the kernel and not by any process and if you try the server program now, you will get bind error. [ Error occurs only if the netstat command shows "TIME_WAIT" as the state of the socket associated with port 6700 (the one on left). And note that socket associated with 6700 occurs at the left (local port) and another port number 36594 on the right(remote port), which is of the client, here.]

    But if you close the client first, then the server. Both in an unexpected manner, and then use netstat command, you will see this :


    Here, the socket associated with port 36595 (the one on left ) is in TIME_WAIT state and socket with port 6700 does not happen to be in that state. Now, if you run server, listening at port 6700, you won't get bind errors.

    And this happens in TCP, where there are states like "TIME_WAIT"  "CLOSED" "FIN_WAIT" and stuff in the connection termination phase. I don't think there is any possibility of bind error in UDP, due to unexpected closing of the server / client. Thought it may occur due to some other fault.