PHP 2 – Variables and Strings Intro file

Posted March 20, 2009 by wdoyle
Categories: Week 9

PHP3.html

<html>
<head><title>form_php test page</title></head>
<body>

<form action=”php_3.php” method=”post”>
What is your last name?<input type=”text” name=”last_name” size=”20″ maxlength=”40″>
<INPUT TYPE=”submit” VALUE=”Submit”> <INPUT TYPE=”reset” VALUE=”Reset”>
</form>

</body>
</html>

php_3.php

<html>
<head><title>form_php test page</title></head>
<body>

I know your last name is

<?php
echo $_POST['last_name'];
?>

<?php

echo “<center><br><a href=\”javascript:history.go(-1)\” class=\”text\”>Click here to go back</a></center>”;

?>

</body>
</html>

Resulting ScreenShot

php_screen

Week 5

Posted February 13, 2009 by wdoyle
Categories: Week 5

This week we had our continous Assessment Exam but afterwards we worked on a program on GUI dynamics.  It was firstly a java program but by removing some elements of code we code easily implement it as a Java Applet.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

 public class Intro extends JApplet {

 private int count = 0;
 private JButton myButton = new JButton(“Push Me!”);
 private JLabel label = new JLabel(“Count: ” + count);
 private JScrollBar myScrollbar = new JScrollBar();
 private Choice myChoice = new Choice();
 private JCheckBox myCheckbox = new JCheckBox();
 private TextArea myText = new TextArea();
 private TextField myField = new TextField();
 private Canvas myCanvas = new Canvas();

 public void init() {
  setLayout(new FlowLayout(FlowLayout.LEFT)); //set layout manager
  add(myButton); //add components
  add(label);
  add(myScrollbar);
  add(myChoice);
  add(myCheckbox);
  add(myText);
  add(myField);

  label.setPreferredSize(new Dimension(60, 10));

 myButton.addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent e) {
   count++;
   label.setText(“Count: ” + count);
  }
 });

}

}

week51

Week 4

Posted February 6, 2009 by wdoyle
Categories: Week 4

Applets GUI Exercises

Flow Layout:

For the Flow Layout program we we did it in two different ways, both shown below with there resulting screen shot.  Both sets of code produce the same output its just the buttons were created differently in both.

import java.awt.*;
import java.applet.*;

public class NumberOne extends Applet {
public void init () {

 setLayout (new FlowLayout ()); //default

 Button But1 = new Button (“One”);
 add (But1);

 add (new Button (“One”));
 add (new Button (“Two”));
 add (new Button (“Three”));
 add (new Button (“Four”));
 add (new Button (“Five”));
 add (new Button (“Six”));

 }
}

Flow Layout 2

import java.awt.*;
import java.applet.*;

public class NumberOne2 extends Applet {
public void init () {

 setLayout (new FlowLayout ()); //default

 Button But1 = new Button (“One”);
 add (But1);

 Button But2 = new Button (“Two”);
 add (But2);

 Button But3 = new Button (“Three”);
 add (But3);

 Button But4 = new Button (“Four”);
 add (But4);

 Button But5 = new Button (“Five”);
 add (But5);

 Button But6 = new Button (“Six”);
 add (But6);

 }
}

flowlayout1

Border Layout

import java.awt.*;
import java.applet.*;

public class NumberTwo extends Applet {
public void init () {

setLayout (new BorderLayout());

 add(new Button(“One”), BorderLayout.NORTH);
 add(new Button(“Two”), BorderLayout.WEST);
 add(new Button(“Three”), BorderLayout.CENTER);
 add(new Button(“Four”), BorderLayout.EAST);
 add(new Button(“Five”), BorderLayout.SOUTH);
 add(new Button(“Six”), BorderLayout.SOUTH);

  }
 }

borderlayout1

Grid Layout

import java.awt.*;
import java.applet.*;
public class NumberThree extends Applet {
public void init () {

setLayout(new GridLayout(2, 3));
add(new Button(“One”));
add(new Button(“Two”));
add(new Button(“Three”));
add(new Button(“Four”));
add (new Button (“Five”));

 }
}

gridlayout

My Button Listener

This program is similar to the flow layout execpt we are adding interaction to the buttons.

import java.awt.*;
import java.applet.*;
import java.awt.event.*;

public class NumberFour extends Applet {
public void init () {

 setLayout (new FlowLayout ()); //default

  Button But1 = new Button (“One”);
  add (But1);

  Button But2 = new Button (“Two”);
  add (But2);

  Button But3 = new Button (“Three”);
  add (But3);

  Button But4 = new Button (“Four”);
  add (But4);

  Button But5 = new Button (“Five”);
  add (But5);

  Button But6 = new Button (“Six”);
  add (But6);

But1.addActionListener(new MyButtonListener());
But2.addActionListener(new MyButtonListener());
But3.addActionListener(new MyButtonListener());
But4.addActionListener(new MyButtonListener());
But5.addActionListener(new MyButtonListener());
But6.addActionListener(new MyButtonListener());
}
class MyButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e){
showStatus (“Ouch!”);
}

}
}

buttonlistener

Week 3

Posted January 30, 2009 by wdoyle
Categories: Week 3

The below program is similar to the SquareInt Applet. This one however also takes in 2 values and adds them together using a method. It then uses another method to work out if the result of the added numbers is odd or even.

//SquareInt2.java
//a programmer-defined square method

import java.awt.*;
import javax.swing.*;

public class SquareInt2 extends JApplet{
double sum =0;
public void init(){

String output = “”;
JTextArea outputArea = new JTextArea(100,200);

//get the applet’s HUI component display area
Container c = getContentPane();

//attach outputArea to Container c
c.add(outputArea);

int result;
for(int x = 1; x <= 10; x++){
result = square(x);
output += “The square of “+ x + ” is ” + result + “n”;
}

String firstNumber = JOptionPane.showInputDialog(”Please enter first value”);
String secondNumber = JOptionPane.showInputDialog(”Please enter second value”);

double fNumber;
double sNumber;

fNumber = Double.parseDouble(firstNumber);
sNumber = Double.parseDouble(secondNumber);

double answer = addNums(fNumber,sNumber);
output = output + “nn The answer to ” + fNumber + ” + ” + sNumber + ” is = ” + answer;
boolean even = is_even((int)answer);

if(even){
output = output + “n” + answer + ” is an even number”;
}
else{
output = output + “n” + answer + ” is an odd number”;
}
outputArea.setText(output);
}

//square method definition
public int square(int y){
return y * y;
}

//even odd method definition
public boolean is_even(int num){
if(num % 2 == 0){
return true;
}
return false;
}

//adding method definition
public double addNums(double a, double b){
double answer = a + b;
return answer;
}
}

Below is the resulting screenshot

sqaureint21

SquareInt Applet

//SquareInt.java
// a programmer-defined square method

import java.awt.Container;
import javax.swing.*;

public class SquareInt extends JApplet{
public void init ()
{
String output=””;

JTextArea outputArea = new JTextArea(10,20);

//get the applet’s HUI component display area
Container c = getContentPane();

//attach outputArea to Container c
c.add(outputArea);

int result;
for(int x=1; x<=10; x++){
result=square(x);
output+=”The square of ” +x+ ” is ” + result + “\n”;
}

outputArea.setText(output);
}
//square method definition
public int square (int y)
{
return y*y;
}
}

The above code generates number 1 to 10 square as below.

squareint

Addition Applet

Today the first applet we created by one that takes in two numbers by the user and then returns the sum of those two numbers.

Below is the following code:

//AdditionApplet.java
//Adding two floating-point numbers

import java.awt.Graphics;  //import class Graphics3 Graphics
import javax.swing.*;   //import package javax.swing

public class AdditionApplet extends JApplet{
double sum;     //sum of the vaules entered by the user

public void init()
{
String firstNumber,   //first string entered by user
secondNumber;    //second string entered by user
double number1,    //first number to add
number2;     //second number to add

//read the second number from user
firstNumber = JOptionPane.showInputDialog(”Enter first floating-point value”);
//read the second number from user
secondNumber = JOptionPane.showInputDialog(”Enter second floating-point value”);

//covert numbers from type String to type double
number1 = Double.parseDouble(firstNumber);
number2 = Double.parseDouble(secondNumber);

//add the numbers
sum = number1 + number2;
}

public void paint(Graphics g)
{
//draw the results with g.drawString
g.drawRect(15,10,270,20);
g.drawString(”The sum is ” + sum,25,25);
}
}

This code then generated the following with the Applet from the html file.

First prompting the user to enter in the first value. Then the second.

additionapplet

The results are shown as soon as the 2nd value are entered.

additionsum1

Week 2

Posted January 23, 2009 by wdoyle
Categories: Week 2, Week 3

This week was our first practical and we made a simple applet to display Hello World in a web browser.  It consisted of two files HelloWorld.java and HelloWorld.html.  The html file simply calls the java code and displays the applet.  Both source code and output screen shots are shown below.

HelloWorld.java

import java.awt.*;
import java.applet.Applet;

public class HelloWorld extends Applet{
public void paint( Graphics g ) {
g.drawString(“Hello World!”, 30, 30);
}
}

HelloWorld.html

<html>
<body>

<applet
code=”HelloWorld.class”
width=150 height=100
</applet>

</body>
</html>

Both were run, compiled using Textpad, and saved in the same directory.  Once the HTML file was opened the output of the applet is shown below.

helloworld

The Complete Applet

This next applet was used to draw shapes and lines using the paint method.  The code and screen output is shown below.

drawing.java

import java.applet.Applet;
import java.awt.*;

public class Drawing extends Applet {
public void paint(Graphics g){

g.setColor(Color.BLUE);
g.fillRect(20,20,50,30);
g.setColor(Color.RED);
g.fillRect(50, 30,50,30);
g.drawLine(50,50,50,100);
g.setColor(Color.WHITE);
g.fillOval(45,25,45,25);
g.setColor(Color.BLACK);
g.drawArc(100,20,30,50,90,90);

}
}

drawing.html

<html>
<body>
<h1>My Applet</h1>
<applet code=”Drawing.class”
width=”250″ height=”200″>
</applet>
</body>
</html>

Resulting screenshot

drawingapplet1

Final Applet

import javax.swing.JApplet;
import javax.swing.JLabel;
import java.awt.BorderLayout;
import java.awt.Color;

public class FirstApplet extends JApplet
{
public void init()
{
getContentPane().setBackground(Color.ORANGE);
setLayout(new BorderLayout());
JLabel aLabel =
new JLabel (“An applet a day keeps the doctor away”);
add(aLabel, BorderLayout.CENTER);

}
}

Resulting screenshot:

firstapplet2

We then edited this applet to include more Labels, bLabel cLabel etc. and we positioned these differently using the NORTH SOUTH etc.. Below is the code and resulting screen shot.

import javax.swing.JApplet;
import javax.swing.JLabel;
import java.awt.BorderLayout;
import java.awt.Color;

public class FirstApplet extends JApplet
{
public void init()
{
getContentPane().setBackground(Color.ORANGE);
setLayout(new BorderLayout());
JLabel aLabel =
new JLabel (“An applet a day keeps the doctor away”);
add(aLabel, BorderLayout.CENTER);

JLabel bLabel =
new JLabel (“An applet a day keeps the doctor away”);
add(bLabel, BorderLayout.SOUTH);

JLabel cLabel =
new JLabel (“An applet a day keeps the doctor away”);
add(cLabel, BorderLayout.NORTH);
}
}

firstapplet21

Week 1

Posted January 15, 2009 by wdoyle
Categories: Week 1

This is the first week of semester 2 year 3 BSc.Multimedia Applications Development, we were introduced to our new lecture Michael McMahon.  No practical classes were on this week.