t4.1&t4.2x

paltryboarpigSoftware and s/w Development

Nov 3, 2013 (4 years and 5 days ago)

84 views

T4.1

a)

// DrawPanel.java


import java.awt.Graphics;

import javax.swing.JPanel;


public class DrawPanel extends JPanel

{


public void paintComponent(Graphics g)


{


super.paintComponent(g);




int width=getWidth();


int height=getHeight();




int i=0;


while(width>=i)


{


g.drawLine(0,0,width
-
i,0+i);


i=i+15;


}


}

}


// DrawPanelTest.java


import javax.swing.JFrame;


public class DrawPanelTest

{


public static void main(String[] args)


{


DrawPanel panel=new
DrawPanel();




JFrame application=new JFrame();




application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);




application.add(panel);


application.setSize(250,250);


application.setVisible(true);


}

}







b)

// DrawPanel.jav
a

import java.awt.Graphics;

import javax.swing.JPanel;


public class DrawPanel extends JPanel

{


public void paintComponent(Graphics g)


{


super.paintComponent(g);




int width=getWidth();


int height=getHeight();


int i=0;


while(width>=i)


{


g.drawLine(0,0,width
-
i,0+i);


g.drawLine(width,0,width
-
i,height
-
i);


g.drawLine(0,height,width
-
i,height
-
i);


g.drawLine(width,height,0+i,height
-
i);


i=i+15;


}


}

}

// DrawPanelTest.java


import javax.swing.JFr
ame;


public class DrawPanelTest

{


public static void main(String[] args)


{


DrawPanel panel=new DrawPanel();




JFrame application=new JFrame();




application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);




application.add
(panel);


application.setSize(250,250);


application.setVisible(true);


}

}







T4.2

a)

// DrawPanel.java


import java.awt.Graphics;

import javax.swing.JPanel;


public class DrawPanel extends JPanel

{


public void paintComponent(Graphics g)


{



super.paintComponent(g);




int width=getWidth();


int height=getHeight();




int i=0;


while(width>=i)


{


g.drawLine(0,0+i,i,height);


i=i+15;


}


}

}



// DrawPanelTest.java


import javax.swing.JFrame;


public class
DrawPanelTest

{


public static void main(String[] args)


{


DrawPanel panel=new DrawPanel();




JFrame application=new JFrame();




application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);




application.add(panel);


applicatio
n.setSize(250,250);


application.setVisible(true);


}

}






b)

// DrawPanel.java

import java.awt.Graphics;

import javax.swing.JPanel;


public class DrawPanel extends JPanel

{


public void paintComponent(Graphics g)


{


super.paintComponent(g);




int width=getWidth();


int height=getHeight();




int i=0;


while(width>=i)


{


g.drawLine(0,0+i,i,height);


g.drawLine(width,0+i,width
-
i,height);


g.drawLine(0,height
-
i,i,0);


g.drawLine(0+i,0,width,i);


i=i+15;


}


}

}

// DrawPanelTest.java


import javax.swing.JFrame;


public class DrawPanelTest

{


public static void main(String[] args)


{


DrawPanel panel=new DrawPanel();




JFrame application=new JFrame();




application.setDefaultCloseOper
ation(JFrame.EXIT_ON_CLOSE);




application.add(panel);


application.setSize(250,250);


application.setVisible(true);


}

}