CS506-finalterm code
CS506-finalterm code
// Animation Code
package lecture_19;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class myPanel extends JPanel
{
int mx = 20, my = 20;
@Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.blue);
g2.fillRect(mx, my, 30, 30);
// g2.fillOval(mx, my, 20, 20); //for ball animation
}
}
public class Lecture_19 {
static JFrame F = new JFrame();
static myPanel p = new myPanel();
public static void main(String[] args) {
F.setTitle("My Painting");
F.setBounds(150, 150, 300, 300);
F.getContentPane().setLayout(new BorderLayout ());
F.getContentPane().add(p);
p.addMouseMotionListener(new Event());
F.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
F.setVisible(true);
}
static class Event extends MouseMotionAdapter
{
@Override
public void mouseDragged(MouseEvent me)
{
p.mx = me.getX();
p.my = me.getY();
p.repaint();
}
}
}
Lecture # 20
Create Simple Applet
import javax.swing.*;
import java.awt.*;
public class MyApplet extends JApplet {
@Override
public void Paint(Graphics g)
{
g.drawString(“Hello World!” 30, 30);
}
}
// Init method in applet
@Override
Public void init()
{
JOptionPane.showMessageDialog(null, “ init method ”);
}
// start method in applet
@Override
Public void start()
{
JOptionPane.showMessageDialog(null, “ start method ”);
}
// Paint method in applet
@Override
Public void paint()
{
g.drawString(“Hello World! “, 30, 30);
}
// Stop method in applet
@Override
Public void stop()
{
JOptionPane.showMessageDialog(null, “ stop method ”);
}
// Destroy method in applet
@Override
Public void destroy()
{
JOptionPane.showMessageDialog(null, “ destroy method ”);
}
Lecture # 21
Client.java file code
import java.net.*;
import java.io.*;
import javax.swing.*;
public class Client {
public static void main(String args[])
{
try{
while(true)
{
// create socket
Socket s = new Socket("localhost", 2222);
// input Stream
InputStream is = s.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
// Output Stream
OutputStream os = s.getOutputStream();
PrintWriter pw = new PrintWriter(os, true);
while(true)
{
// port accept or not
Socket s = ss.accept();
System.out.println("Connection Request Received");
// Input Stream
InputStream is = s.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
// Output Stream
OutputStream os = s.getOutputStream();
PrintWriter pw = new PrintWriter(os, true);
// close communication
s.close();
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
Lecture # 22
// serialization with file
package lecture_22;
import java.io.*;
class PersonInfo implements Serializable
{
int id;
String name;
String address;
transient String phone;
PersonInfo(int id, String name, String address, String phone)
{
this.id = id;
this.name = name;
this.address = address;
this.phone = phone;
}
void PersonInfoDisplay()
{
System.out.println("Id: " + id);
System.out.println("Name: " + name);
System.out.println("Address: " + address);
System.out.println("Phone: " + phone);
}
}
public class Lecture_22 {
public static void main(String[] args) {
PersonInfo p = new PersonInfo(210407911, "Maryam", "Wah
Cantt", "03175184101");
try {
// send data into file
FileOutputStream fos = new FileOutputStream("Person.dat");
ObjectOutputStream os = new ObjectOutputStream(fos);
os.writeObject(p);
// close connection
os.close();
fos.close();
is.close();
fis.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
Lecture # 23
// code example using Interface
package lecture_23;
class Worker implements Runnable
{
String job;
Worker(String j)
{
job = j;
}
@Override
public void run()
{
for(int i=1; i<=5; i++)
{
System.out.println(job + " = " + i);
}
}
}
public class Lecture_23 {
public static void main(String[] args) {
Worker w1 = new Worker("First: ");
Worker w2 = new Worker("Second: ");
Worker w3 = new Worker("Third: ");
Thread t1 = new Thread(w1);
Thread t2 = new Thread(w2);
Thread t3 = new Thread(w3);
t1.start();
t2.start();
t3.start();
}
}
Lecture # 24
package lecture_24;
class Worker implements Runnable {
String job;
Worker(String j) {
job = j;
}
@Override
public void run() {
for (int i = 1; i <= 5; i++) {
System.out.println(job + " = " + i);
}
}
}
// Debugging outputs
System.out.println("Starting threads...");
t1.start();
t2.start();
try {
t1.join();
t2.join();
} catch (Exception e) {
e.printStackTrace();
}
Lecture 28