COMPUTER
PROGRAMMING 2014 WEEK 8
GRAPHIC
WINDOW WITH JFRAME and JPANEL CLASSES
EXERCISE
1
import javax.swing.*; import java.awt.Graphics; import java.awt.*; import java.awt.event.*; public class
FrameGraphic extends JFrame { private
static final long serialVersionUID = 8000000L; JPanel d; public
FrameGraphic(String a,JPanel di) {
super(a);
//setLayout(
new FlowLayout() );
d=di;
add(d); } public
static void plot(String a,JPanel di) {
FrameGraphic f = new FrameGraphic(a,di);
f.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
f.setSize(1200,800);
f.setVisible(true);
} } |
import javax.swing.*; import java.awt.Graphics; import java.awt.*; import java.awt.event.*; import java.awt.geom.*; import java.awt.Color.*; public class
WelcomeP extends JPanel { private
static final long serialVersionUID = 8000001L; String isim; public
WelcomeP() {isim=JOptionPane.showInputDialog("enter
your name : ");} public void
paint(Graphics g) { Graphics2D
g2=(Graphics2D)g; g2.setFont(new
Font("Serif",Font.BOLD,48)); g2.drawString("Welcome
to Java class " + isim,50,50); } } |
class H8Ex1 { public static
void main(String args[]) {FrameGraphic.plot("Welcome to
graphic window",new WelcomeP());}} |
EXERCISE
2
import java.awt.*; import java.awt.event.*; import javax.swing.event.*; import java.awt.geom.*; import
java.net.URL; import java.awt.image.*; import java.util.*; public class
pictureP extends JPanel { private
static final long serialVersionUID = 8000002L; private Image
picture; public
pictureP(String s) {super(); URL url =
getClass().getResource(s); picture =
getToolkit().getImage(url); } public void
paint(Graphics g) { Graphics2D
g2=(Graphics2D)g; Dimension
d=getSize(); int dx =
d.width; int dy =
d.height; g2.drawImage(
picture, 0, 0,dx,dy, this); } } |
class H8Ex2 { public static
void main(String args[]) { pictureP
pp=new pictureP("fall.jpg");
FrameGraphic.plot("fall",pp); }} |
EXERCISE
3
import javax.swing.*; import java.awt.Graphics; import java.awt.*; import java.awt.event.*; import java.awt.geom.*; public class
lineP extends JPanel { private
static final long serialVersionUID = 8000003L; public void
paintComponent(Graphics g) { super.paintComponent(g); Graphics2D
g2=(Graphics2D)g;
g2.setFont(new
Font("Serif",Font.BOLD,24)); this.setBackground(Color.BLACK);
g2.setColor(Color.red); g2.setStroke(new
BasicStroke(1.0f)); Line2D x=new
Line2D.Double(50,50,800,800); g2.draw(x); g2.setColor(Color.blue); Line2D
x1=new Line2D.Double(50,50,500,100); g2.draw(x1);
}} |
class H8Ex3 { public static
void main(String args[]) { lineP pp=new
lineP();
FrameGraphic.plot("Plot
window",pp); } } |
EXERCISE
4
import java.awt.Graphics; import java.awt.*; import java.awt.event.*; import java.awt.geom.*; import javax.swing.*; public class
rectangleP extends JPanel { private
static final long serialVersionUID = 8000004L; public void
paintComponent(Graphics g) {
Graphics2D
g2=(Graphics2D)g; super.paintComponent(g);
g2.setFont(new
Font("Serif",Font.BOLD,24));
g2.setColor(Color.red); this.setBackground(Color.MAGENTA); g2.setStroke(new
BasicStroke(10.0f)); Rectangle2D
x=new Rectangle2D.Double(50,50,300,200); g2.fill(x);
} } |
class H8Ex4 { public static
void main(String args[]) { rectangleP
pp=new rectangleP();
FrameGraphic.plot("Plot
window",pp); } } |
EXERCISE
5
import javax.swing.*; import java.awt.Graphics; import java.awt.*; import java.awt.event.*; import java.awt.geom.*; public class
rectangleP1 extends JPanel { private
static final long serialVersionUID = 8000005L; public void
paintComponent(Graphics g) { Graphics2D
g2=(Graphics2D)g; super.paintComponent(g);
g2.setFont(new
Font("Serif",Font.BOLD,24));
g2.setColor(Color.RED); this.setBackground(Color.GREEN);
g2.setStroke(new
BasicStroke(10.0f)); Rectangle2D
x=new Rectangle2D.Double(50,50,300,200); g2.fill(x);
} } |
class H8Ex5 { public static
void main(String args[]) { rectangleP1
pp=new rectangleP1();
FrameGraphic.plot("Plot
window",pp); } } |
EXERCISE
6
import javax.swing.*; import java.awt.Graphics; import java.awt.*; import java.awt.event.*; import java.awt.geom.*; public class
ellipseP extends JPanel { private
static final long serialVersionUID = 8000006L; public
void paintComponent(Graphics g) {
super.paintComponent(g); Graphics2D
g2=(Graphics2D)g;
g2.setFont(new
Font("Serif",Font.BOLD,24)); g2.setColor(Color.RED); this.setBackground(new
Color(0,0,255)); g2.setStroke(new
BasicStroke(2.0f)); Ellipse2D
x=new Ellipse2D.Double(50,50,500,200); g2.draw(x); } } |
class H8Ex6 { public static
void main(String args[]) { ellipseP
pp=new ellipseP();
FrameGraphic.plot("Plot
window",pp); } } |
EXERCISE
7
import java.awt.Graphics; import java.awt.*; import java.awt.event.*; import java.awt.geom.*; public class
ellipseP1 extends JPanel { private
static final long serialVersionUID = 8000007L; public
void paintComponent(Graphics g) {
super.paintComponent(g); Graphics2D
g2=(Graphics2D)g;
g2.setFont(new
Font("Serif",Font.BOLD,24)); g2.setColor(Color.RED); this.setBackground(new
Color(255,255,255)); float
dash3[] = {10.0f,3.0f,3.0f}; BasicStroke
d3 = new BasicStroke(3.0f,BasicStroke.CAP_BUTT,
BasicStroke.JOIN_MITER,
3.0f, dash3, 2.0f); g2.setStroke(d3); Ellipse2D
x=new Ellipse2D.Double(50,50,500,200); g2.draw(x); } } |
class H8Ex7 { public static
void main(String args[]) { ellipseP1
pp=new ellipseP1();
FrameGraphic.plot("Plot
window",pp); } } |
EXERCISE
8 Abstract class version
abstract
class f_x { abstract
double func(double x);} |
import javax.swing.*; import java.awt.Graphics; import java.awt.*; import java.awt.event.*; import java.awt.geom.*; import java.util.Locale; public class
plotP extends JPanel { private
static final long serialVersionUID = 8000008L;
Locale us=new Locale("US"); //real plot
coordinates double x[]; double y[]; //plot
coordinates converted to pixels int x_pixel[]; int y_pixel[]; // min maximum
in real plot coordinates double xmin; double xmax; double ymin; double ymax; //plot window
size pixel coordinates private int absx,absy; //plot
relative to plot window // |---dx1---|-----dx2------|-------dx3| //plot window
draw to dx2, dx1 and dx3 are left // open at the
right and left handside private int
dx1,dx2,dx3; // |---dy1---|-----dy2------|-------dy3| //plot window
draw to dy2, dy1 and dy3 are left // open at the
top and bottom handside private int
dy1,dy2,dy3; //number of data points int n; //number of
plot thicks int ntick; //plot color
and background color Color c,cbg; //plot labels String xlabel,ylabel,label; //Plot number
format String format; //Plot font Font font; boolean
gridon; public void
setXlabel(String s) {xlabel=s;} public void
setYlabel(String s) {ylabel=s;} public void
setPlabel(String s) {label=s;} public void
setLabels(String s1,String s2,String s3) {xlabel=s1;ylabel=s2;label=s3;} public void
setFont(Font ff) {font=ff;} public void
setFormat(String s) {format=s;} public void
setXmin(double xx1) {xmin=xx1;} public void
setXmax(double xx2) {xmax=xx2;} public void
setYmin(double xx) {ymin=xx;} public void
setYmax(double xx) {ymax=xx;} public void
setNthick(int nn) {ntick=nn;} public void
setColor(Color ci) {c=ci;} public void
setBackground(Color ci) {c=ci;} public void
setGridOn() {
gridon=true;} public void
setGridOff() {
gridon=false;} public void
setVariables() {format="%8.2f"; c=Color.blue;cbg=Color.lightGray; font=new
Font("Courier",Font.PLAIN,16); ntick=10; xlabel="x"; ylabel="y"; label="y=f(x)"; } public void
setFunc(f_x f,double xmini,double
xmaxi,int ni) {
n=ni; xmin=xmini; xmax=xmaxi;
double
x1[]=new double[n];
double
y1[]=new double[n];
x=x1;
y=y1;
int
x_pixel1[]=new int[n]; int
y_pixel1[]=new int[n]; x_pixel=x_pixel1; y_pixel=y_pixel1; ymin=1.0e80; ymax=-1.0e80; for(int
i=0;i<n;i++)
{x[i] =
xmin+(xmax-xmin)*i/(double)(n-1);
y[i] = f.func(x[i]);
if(y[i]<ymin) ymin=y[i];
if(y[i]>ymax) ymax=y[i];
} } public
double[] min_max(double xi[]) {double ximin=1.0e80; double
ximax=-1.0e80; double ni=xi.length; for(int
i=0;i<ni;i++) { if(xi[i]<ximin)
ximin=xi[i]; if(xi[i]>ximax)
ximax=xi[i]; } double a[]={ximin,ximax}; return a; } public void
setData(double xi[],double yi[]) {
x=xi; y=yi;
double
a1[]=min_max(x);
n=x.length; xmin=a1[0]; xmax=a1[1]; double
a2[]=min_max(y);
ymin=a2[0]; ymax=a2[1];
int
x_pixel1[]=new int[n]; int
y_pixel1[]=new int[n]; x_pixel=x_pixel1; y_pixel=y_pixel1; gridon=false; } public
plotP(f_x f,double xmini,double
xmaxi,int ni) { setVariables(); setFunc(f,xmini,xmaxi,ni); } public
plotP(double xi[],double yi[]) { setVariables(); setData(xi,yi); } public void
paintComponent(Graphics g) {
Graphics2D g2=(Graphics2D)g;
//g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
g2.setPaint(c);
g2.setFont(font);
Dimension size=getSize();
absx=size.width;
absy=size.height;
dx1=(int)(0.1*absx);
dx3=(int)(0.1*absx);
dx2=absx-dx1-dx3;
dy1=(int)(0.1*absy);
dy3=(int)(0.1*absy);
dy2=absy-dy1-dy3;
//calculate plot coordinates
for(int i=0;i<n;i++)
{x_pixel[i]=dx1+(int)(dx2*(x[i]-xmin)/(xmax-xmin));
y_pixel[i]=dy1+dy2-(int)(dy2*(y[i]-ymin)/(ymax-ymin));
}
//draw plot line
GeneralPath shape=new GeneralPath(GeneralPath.WIND_EVEN_ODD);
shape.moveTo(x_pixel[0],y_pixel[0]);
for(int i=1;i<n;i++)
{ //if
the limits are outside the plot window
//plot the limit value
if(x_pixel[i]<dx1) x_pixel[i]=dx1;
else
if(x_pixel[i]>(dx1+dx2)) x_pixel[i]=(dx1+dx2);
if(y_pixel[i]>(dy1+dy2)) y_pixel[i]=dy1+dy2;
else
if(y_pixel[i]<dy1) y_pixel[i]=dy1;
shape.lineTo(x_pixel[i],y_pixel[i]);}
g2.draw(shape);
Rectangle2D d=new
Rectangle2D.Double(dx1,dy1,dx2,dy2);
g2.draw(d);
//draw xtick
int xtick[]=new int[ntick];
Line2D xt[]=new Line2D[ntick];
for(int i=0;i<ntick;i++)
{xtick[i]=dx1+dx2*i/ntick;
if(!gridon)
xt[i]=new
Line2D.Double(xtick[i],(dy1+dy2),xtick[i],(dy1+dy2*0.98));
else
xt[i]=new
Line2D.Double(xtick[i],(dy1+dy2),xtick[i],dy1);
g2.draw(xt[i]);
}
//draw x numbers
int xtick1[]=new int[ntick+1];
String xnumber[]=new String[ntick+1];
for(int i=0;i<=ntick;i++)
{xtick1[i]=(int)(dx1*0.98)+dx2*i/ntick;
double x1=(xmin+(xmax-xmin)*i/(double)ntick);
xnumber[i]=String.format(us,format,x1);
g2.drawString(xnumber[i],xtick1[i]-20,dy1+(int)(dy2*1.04));
}
//draw ytick
int ytick[]=new int[ntick];
Line2D yt[]=new Line2D[ntick];
for(int i=1;i<ntick;i++)
{ytick[i]=dy1+dy2*i/ntick;
if(!gridon)
yt[i]=new
Line2D.Double(dx1,ytick[i],dx1+10,ytick[i]);
else
yt[i]=new
Line2D.Double(dx1,ytick[i],(dx1+dx2),ytick[i]);
g2.draw(yt[i]);
}
//draw y numbers
int ytick1[]=new int[ntick+1];
String ynumber[]=new String[ntick+1];
for(int i=0;i<=ntick;i++)
{ytick1[i]=dy1+dy2-dy2*i/ntick;
ynumber[i]=String.format("%8.2f",(ymin+(ymax-ymin)*i/(double)ntick));
g2.drawString(ynumber[i],dx1/2,ytick1[i]);
//draw labels;
//x label
g2.drawString(xlabel,(dx1+dx2/2),(dy1+(int)(dy2*1.1)));
int xx1=dx1/3;
int yy1=(dy1+dy2/2);
//y label
AffineTransform at=AffineTransform.getRotateInstance(3.0*Math.PI/2.0,xx1,yy1);
g2.setTransform(at);
g2.drawString(ylabel,xx1,yy1);
at=AffineTransform.getRotateInstance(0,xx1,yy1);
g2.setTransform(at);
//plot label
g2.drawString(label,(dx1+dx2/2),(int)(dy1*0.8));
}
} public void
plot(String s) {
JFrame f = new JFrame(s);
f.add(this);
f.setBackground(cbg);
f.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
f.setSize(1200,800);
f.setVisible(true);
} public void
plot() { plot("Plot
window"); }
} |
import javax.swing.*; class fb
extends f_x { public
double func(double x) {
return Math.tan(x);} } class H8Ex8 { public static
void main(String args[]) { fb ff=new
fb(); plotP pp=new
plotP(ff,0.0,2.0*Math.PI,1000); pp.setYmin(-20.0); pp.setYmax(20.0);
pp.setPlabel("f(x)=tan(x)");
pp.setGridOn(); pp.setNthick(8); pp.plot("tangent"); double x[]={0,1,2,3,4,5,6}; double y[]={0,1,4,9,16,25,36}; plotP pp1=new
plotP(x,y); pp1.setNthick(6); pp1.plot(); } } |
EXERCISE
8I Interface version
public
interface f_xI { double
func(double x);} |
import javax.swing.*; import java.awt.Graphics; import java.awt.*; import java.awt.event.*; import java.awt.geom.*; import java.util.Locale; public class
plotPI extends JPanel { private
static final long serialVersionUID = 80000081L;
Locale us=new Locale("US"); //real plot
coordinates double x[]; double y[]; //plot
coordinates converted to pixels int x_pixel[]; int y_pixel[]; // min maximum
in real plot coordinates double xmin; double xmax; double ymin; double ymax; //plot window
size pixel coordinates private int absx,absy; //plot
relative to plot window // |---dx1---|-----dx2------|-------dx3| //plot window
draw to dx2, dx1 and dx3 are left // open at the
right and left handside private int
dx1,dx2,dx3; // |---dy1---|-----dy2------|-------dy3| //plot window
draw to dy2, dy1 and dy3 are left // open at the
top and bottom handside private int
dy1,dy2,dy3; //number of data points int n; //number of
plot thicks int ntick; //plot color
and background color Color c,cbg; //plot labels String xlabel,ylabel,label; //Plot number
format String format; //Plot font Font font; boolean
gridon; public void
setXlabel(String s) {xlabel=s;} public void
setYlabel(String s) {ylabel=s;} public void
setPlabel(String s) {label=s;} public void
setLabels(String s1,String s2,String s3) {xlabel=s1;ylabel=s2;label=s3;} public void
setFont(Font ff) {font=ff;} public void
setFormat(String s) {format=s;} public void
setXmin(double xx1) {xmin=xx1;} public void
setXmax(double xx2) {xmax=xx2;} public void
setYmin(double xx) {ymin=xx;} public void
setYmax(double xx) {ymax=xx;} public void
setNthick(int nn) {ntick=nn;} public void
setColor(Color ci) {c=ci;} public void
setBackground(Color ci) {c=ci;} public void
setGridOn() {
gridon=true;} public void
setGridOff() {
gridon=false;} public void
setVariables() {format="%8.2f"; c=Color.blue;cbg=Color.lightGray; font=new
Font("Courier",Font.PLAIN,16); ntick=10; xlabel="x"; ylabel="y"; label="y=f(x)"; } public void
setFunc(f_xI f,double xmini,double
xmaxi,int ni) {
n=ni; xmin=xmini; xmax=xmaxi;
double
x1[]=new double[n];
double
y1[]=new double[n];
x=x1;
y=y1;
int
x_pixel1[]=new int[n]; int
y_pixel1[]=new int[n]; x_pixel=x_pixel1; y_pixel=y_pixel1; ymin=1.0e80; ymax=-1.0e80; for(int
i=0;i<n;i++)
{x[i] =
xmin+(xmax-xmin)*i/(double)(n-1);
y[i] = f.func(x[i]);
if(y[i]<ymin) ymin=y[i];
if(y[i]>ymax) ymax=y[i];
} } public
double[] min_max(double xi[]) {double ximin=1.0e80; double
ximax=-1.0e80; double ni=xi.length; for(int
i=0;i<ni;i++) { if(xi[i]<ximin)
ximin=xi[i]; if(xi[i]>ximax)
ximax=xi[i]; } double a[]={ximin,ximax}; return a; } public void
setData(double xi[],double yi[]) {
x=xi; y=yi;
double
a1[]=min_max(x);
n=x.length; xmin=a1[0]; xmax=a1[1]; double
a2[]=min_max(y);
ymin=a2[0]; ymax=a2[1];
int
x_pixel1[]=new int[n]; int
y_pixel1[]=new int[n]; x_pixel=x_pixel1; y_pixel=y_pixel1; gridon=false; } public
plotPI(f_xI f,double xmini,double
xmaxi,int ni) { setVariables(); setFunc(f,xmini,xmaxi,ni); } public
plotPI(double xi[],double yi[]) { setVariables(); setData(xi,yi); } public void
paintComponent(Graphics g) {
Graphics2D g2=(Graphics2D)g;
//g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
g2.setPaint(c);
g2.setFont(font);
Dimension size=getSize();
absx=size.width;
absy=size.height;
dx1=(int)(0.1*absx);
dx3=(int)(0.1*absx);
dx2=absx-dx1-dx3;
dy1=(int)(0.1*absy);
dy3=(int)(0.1*absy);
dy2=absy-dy1-dy3;
//calculate plot coordinates
for(int i=0;i<n;i++)
{x_pixel[i]=dx1+(int)(dx2*(x[i]-xmin)/(xmax-xmin));
y_pixel[i]=dy1+dy2-(int)(dy2*(y[i]-ymin)/(ymax-ymin));
}
//draw plot line
GeneralPath shape=new GeneralPath(GeneralPath.WIND_EVEN_ODD);
shape.moveTo(x_pixel[0],y_pixel[0]);
for(int i=1;i<n;i++)
{ //if
the limits are outside the plot window
//plot the limit value
if(x_pixel[i]<dx1) x_pixel[i]=dx1;
else
if(x_pixel[i]>(dx1+dx2)) x_pixel[i]=(dx1+dx2);
if(y_pixel[i]>(dy1+dy2)) y_pixel[i]=dy1+dy2;
else
if(y_pixel[i]<dy1) y_pixel[i]=dy1;
shape.lineTo(x_pixel[i],y_pixel[i]);}
g2.draw(shape);
Rectangle2D d=new
Rectangle2D.Double(dx1,dy1,dx2,dy2);
g2.draw(d);
//draw xtick
int xtick[]=new int[ntick];
Line2D xt[]=new Line2D[ntick];
for(int i=0;i<ntick;i++)
{xtick[i]=dx1+dx2*i/ntick; if(!gridon)
xt[i]=new
Line2D.Double(xtick[i],(dy1+dy2),xtick[i],(dy1+dy2*0.98));
else
xt[i]=new
Line2D.Double(xtick[i],(dy1+dy2),xtick[i],dy1);
g2.draw(xt[i]);
}
//draw x numbers
int xtick1[]=new int[ntick+1];
String xnumber[]=new String[ntick+1];
for(int i=0;i<=ntick;i++)
{xtick1[i]=(int)(dx1*0.98)+dx2*i/ntick;
double x1=(xmin+(xmax-xmin)*i/(double)ntick);
xnumber[i]=String.format(us,format,x1); g2.drawString(xnumber[i],xtick1[i]-20,dy1+(int)(dy2*1.04));
}
//draw ytick
int ytick[]=new int[ntick];
Line2D yt[]=new Line2D[ntick];
for(int i=1;i<ntick;i++)
{ytick[i]=dy1+dy2*i/ntick;
if(!gridon)
yt[i]=new
Line2D.Double(dx1,ytick[i],dx1+10,ytick[i]);
else
yt[i]=new
Line2D.Double(dx1,ytick[i],(dx1+dx2),ytick[i]);
g2.draw(yt[i]);
}
//draw y numbers
int ytick1[]=new int[ntick+1]; String
ynumber[]=new String[ntick+1];
for(int i=0;i<=ntick;i++)
{ytick1[i]=dy1+dy2-dy2*i/ntick;
ynumber[i]=String.format("%8.2f",(ymin+(ymax-ymin)*i/(double)ntick));
g2.drawString(ynumber[i],dx1/2,ytick1[i]);
//draw labels;
//x label
g2.drawString(xlabel,(dx1+dx2/2),(dy1+(int)(dy2*1.1)));
int xx1=dx1/3;
int yy1=(dy1+dy2/2);
//y label
AffineTransform at=AffineTransform.getRotateInstance(3.0*Math.PI/2.0,xx1,yy1);
g2.setTransform(at);
g2.drawString(ylabel,xx1,yy1);
at=AffineTransform.getRotateInstance(0,xx1,yy1);
g2.setTransform(at);
//plot label
g2.drawString(label,(dx1+dx2/2),(int)(dy1*0.8));
}
} public void
plot(String s) {
JFrame f = new JFrame(s);
f.add(this);
f.setBackground(cbg);
f.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
f.setSize(1200,800);
f.setVisible(true);
} public void
plot() { plot("Plot
window"); }
} |
import javax.swing.*; class H8Ex8I
{ public static
void main(String args[]) { f_xI
ff=x->Math.tan(x); plotPI pp=new
plotPI(ff,0.0,2.0*Math.PI,1000); pp.setYmin(-20.0); pp.setYmax(20.0);
pp.setPlabel("f(x)=tan(x)");
pp.setGridOn(); pp.setNthick(8); pp.plot("tangent"); double x[]={0,1,2,3,4,5,6}; double y[]={0,1,4,9,16,25,36}; plotPI
pp1=new plotPI(x,y); pp1.setNthick(6); pp1.plot(); } } |
EXERCISE
9
import java.util.*; public class
ellipseP2 extends JPanel { private
static final long serialVersionUID = 8000009L; TexturePaint
tp = getImageTexture("fall.jpg");
public
TexturePaint getImageTexture(String imageFile) { URL
url = getClass().getResource(imageFile); Image
img = getToolkit().getImage(url); try
{
MediaTracker tracker = new MediaTracker(this);
tracker.addImage(img,
0);
tracker.waitForID(0); } catch (Exception e) {} int
width = img.getWidth(this); int
height = img.getHeight(this); BufferedImage
buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); Graphics
g = buffImg.getGraphics(); g.drawImage(img,
0, 0, this); Rectangle2D
rect = new Rectangle(0, 0, width, height); return
new TexturePaint(buffImg, rect); }
public
void paintComponent(Graphics g) {
super.paintComponent(g); Graphics2D
g2=(Graphics2D)g; g2.setPaint(tp);
g2.setFont(new
Font("Serif",Font.BOLD,24)); //g2.setColor(Color.RED); //this.setBackground(new
Color(255,255,255)); Ellipse2D
x=new Ellipse2D.Double(50,50,500,200); g2.fill(x); } } |
class H8Ex9 { public static
void main(String args[]) { ellipseP2
pp=new ellipseP2();
FrameGraphic.plot("Plot
window",pp); } } |
EXERCISE
10
import javax.swing.*; import java.awt.Graphics; import java.awt.*; import java.awt.event.*; import java.awt.geom.*; public class
graph2P extends JPanel { public
void paintComponent(Graphics g) { super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setPaint(Color.blue);
Ellipse2D elips1=new Ellipse2D.Double(55,55,90,30);
g2.fill(elips1);
g2.setPaint(Color.black);
Ellipse2D elips2=new Ellipse2D.Double(50,50,100,40);
g2.draw(elips2);
g2.setPaint(Color.black);
Ellipse2D elips3=new Ellipse2D.Double(50,150,100,40);
g2.draw(elips3);
GradientPaint kirmizidanbeyaza=new
GradientPaint(250,50,Color.red,350,90,Color.white);
g2.setPaint(kirmizidanbeyaza);
Ellipse2D elips4=new Ellipse2D.Double(250,50,100,40);
g2.fill(elips4);
GradientPaint kirmizidanmaviye=new
GradientPaint(250,150,Color.red,350,190,Color.blue);
g2.setPaint(kirmizidanmaviye);
Ellipse2D elips5=new Ellipse2D.Double(250,150,100,40);
g2.fill(elips5);
g2.setPaint(Color.black);
g2.draw(elips5); } } |
class H10Ex10
{ public static
void main(String args[]) { graph2P
pp=new graph2P();
FrameGraphic.plot("Plot
window",pp); } } |
EXERCISE
11
import javax.swing.*; import java.awt.Graphics; import java.awt.*; import java.awt.event.*; import java.awt.geom.*; public class
starP extends JPanel { private
static final long serialVersionUID = 8000011L; public void
paint(Graphics g) { int x[]={55,67,109,73,83,55,27,37,1,43}; int y[]={0,36,36,54,96,72,96,54,36,36}; Graphics2D
g2=(Graphics2D)g; GeneralPath star=new GeneralPath(); star.moveTo(x[0],y[0]); for(int
i=1;i<x.length;i++) {star.lineTo(x[i],y[i]);} star.closePath(); g2.setColor(Color.blue); g2.draw(star); } } |
class H10Ex11
{ public static
void main(String args[]) { starP pp=new
starP();
FrameGraphic.plot("Plot
window",pp); } } |
EXERCISE
12
import javax.swing.*; import java.awt.Graphics; import java.awt.*; import java.awt.event.*; import java.awt.geom.*; public class
quadcurveP extends JPanel { private
static final long serialVersionUID = 8000012L; public
void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
Dimension boyut=getSize();
int dx=boyut.width;
int dy=boyut.height;
g2.setPaint(Color.BLACK);
g2.draw3DRect(0,0,dx-3,dy-3,true);
g2.draw3DRect(3,3,dx-7,dy-7,false);
g2.setPaint(Color.BLACK);
QuadCurve2D qc2=new QuadCurve2D.Double(0,125,140,225,225,150);
g2.draw(qc2);
QuadCurve2D qc2_1=new QuadCurve2D.Double(0,200,155,225,225,170);
g2.setPaint(Color.blue);
g2.fill(qc2_1); } } |
class H8Ex12 { public static
void main(String args[]) { quadcurveP
pp=new quadcurveP();
FrameGraphic.plot("Plot
window",pp); } } |
EXERCISE
13
import javax.swing.*; import java.awt.Graphics; import java.awt.*; import java.awt.event.*; import java.awt.geom.*; public class
generalcurveP extends JPanel {
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
setBackground(Color.lightGray);
Dimension boyut=getSize();
int dx=boyut.width;
int dy=boyut.height;
g2.setStroke(new BasicStroke(3));
g2.draw3DRect(0,0,dx-3,dy-3,true);
g2.draw3DRect(3,3,dx-7,dy-7,false);
GeneralPath shape=new GeneralPath(GeneralPath.WIND_EVEN_ODD);
shape.moveTo(20,20);
//quadratic addition
shape.quadTo(160,120,245,45);
//cubic addition
shape.curveTo(195,95,295,145,245,195);
shape.curveTo(-80,110,345,110,20,195);
shape.curveTo(400,250,200,250,20,20);
g2.draw(shape); } } |
class H10Ex13
{ public static
void main(String args[]) { generalcurveP
pp=new generalcurveP();
FrameGraphic.plot("Plot
window",pp); } } |
HOMEWORKS
JFRAME
HOMEWORK
1) In Exercise 8 class
plotP (JPANEL ) is given.
In this class in order to plot the line class
GeneralPath
and method lineTo is used.
Instead of this, write a version that use moveTo
and quadTo (quadratic
line plotter) to
create plot
HOMEWORK 2)
In Exercise 8 class plotP (JPANEL ) is given. Plot
function y=Math.sin(x)*(0.1*Math.random()
) by using class
plotP between x=0
and x=p .
HOMEWORK 3)
In Exercise 8 class plotP (JPANEL ) is given. Plot data
double x[]={1,2,3,4,5,6,7,8,9};
double y[]={-1,-2,-1,2,7,14,23,34,47}
HOMEWORK 4)
Write a program to draw following graphics
HOMEWORK 5) Write a FrameGraphic
program to draw a
pentagon(five sided shape)
APPLET EXERCISES
WITH JAPPLET AVD JPANEL
CLASSES
EXERCISE
1 ABSTRACT CLASS FUNCTION PLOT
abstract
class f_x { abstract
double func(double x);} |
import javax.swing.*; import java.awt.Graphics; import java.awt.*; import java.awt.event.*; import java.awt.geom.*; import java.util.Locale; public class
plotP extends JPanel { private
static final long serialVersionUID = 8100008L;
Locale us=new Locale("US"); //real plot
coordinates double x[]; double y[]; //plot
coordinates converted to pixels int x_pixel[]; int y_pixel[]; // min maximum
in real plot coordinates double xmin; double xmax; double ymin; double ymax; //plot window
size pixel coordinates private int absx,absy; //plot
relative to plot window // |---dx1---|-----dx2------|-------dx3| //plot window
draw to dx2, dx1 and dx3 are left // open at the
right and left handside private int
dx1,dx2,dx3; // |---dy1---|-----dy2------|-------dy3| //plot window
draw to dy2, dy1 and dy3 are left // open at the
top and bottom handside private int
dy1,dy2,dy3; //number of data points int n; //number of
plot thicks int ntick; //plot color
and background color Color c,cbg; //plot labels String xlabel,ylabel,label; //Plot number
format String format; //Plot font Font font; boolean
gridon; boolean
splineon; public void
setXlabel(String s) {xlabel=s;} public void
setYlabel(String s) {ylabel=s;} public void
setPlabel(String s) {label=s;} public void
setLabels(String s1,String s2,String s3) {xlabel=s1;ylabel=s2;label=s3;} public void
setFont(Font ff) {font=ff;} public void
setFormat(String s) {format=s;} public void
setXmin(double xx1) {xmin=xx1;} public void
setXmax(double xx2) {xmax=xx2;} public void
setYmin(double xx) {ymin=xx;} public void
setYmax(double xx) {ymax=xx;} public void
setNthick(int nn) {ntick=nn;} public void
setColor(Color ci) {c=ci;} public void
setBackground(Color ci) {c=ci;} public void
setGridOn() {
gridon=true;} public void
setGridOff() {
gridon=false;} public void
setVariables(boolean b) { format="%8.2f"; c=Color.blue;cbg=Color.lightGray; font=new
Font("Courier",Font.PLAIN,16); ntick=10; xlabel="x"; ylabel="y"; label="y=f(x)"; gridon=false; splineon=b; } public
double[][] setFunc(f_x f,double xmini,double
xmaxi) {n=500; return
setFunc(f,xmini,xmaxi,n); } public
double[][] setFunc(f_x f,double xmini,double
xmaxi,int ni) {
n=ni;
double
x1[]=new double[n];
double
y1[]=new double[n]; for(int
i=0;i<n;i++)
{x1[i] =
xmini+(xmaxi-xmini)*i/(double)(n-1);
y1[i] = f.func(x1[i]);
}
double a[][]={x1,y1};
return a; } public
double[] min_max(double xi[]) {double ximin=1.0e80; double
ximax=-1.0e80; double ni=xi.length; for(int
i=0;i<ni;i++) { if(xi[i]<ximin)
ximin=xi[i]; if(xi[i]>ximax)
ximax=xi[i]; } double a[]={ximin,ximax}; return a; } public void
setData(double xy[][]) {double x1[]=xy[0]; double
y1[]=xy[1]; setData(x1,y1); } public void
setData(double xi[],double yi[]) {
if(splineon) {
double
a[][]=cubic_spline.funcSpline(xi,yi,10);
x=a[0]; y=a[1]; } else {x=xi;y=yi;}
double
a1[]=min_max(x);
n=x.length; xmin=a1[0]; xmax=a1[1]; double
a2[]=min_max(y);
ymin=a2[0]; ymax=a2[1];
int
x_pixel1[]=new int[n]; int
y_pixel1[]=new int[n]; x_pixel=x_pixel1; y_pixel=y_pixel1; gridon=false; } public
plotP(f_x f,double xmini,double
xmaxi,int ni) { //cubic
spline interpolation can be applied to the data setVariables(false); setData(setFunc(f,xmini,xmaxi,ni)); } public
plotP(f_x f,double xmini,double
xmaxi,int ni,boolean spline) { //cubic
spline interpolation can be applied to the data setVariables(spline); setData(setFunc(f,xmini,xmaxi,ni)); } public
plotP(f_x f,double xmini,double
xmaxi) { setVariables(false); setData(setFunc(f,xmini,xmaxi)); } public
plotP(double xi[],double yi[],boolean spline) { setVariables(
spline); setData(xi,yi); } public void
paintComponent(Graphics g) {
Graphics2D g2=(Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
g2.setPaint(c);
g2.setFont(font);
Dimension size=getSize();
absx=size.width;
absy=size.height;
dx1=(int)(0.1*absx);
dx3=(int)(0.1*absx);
dx2=absx-dx1-dx3; dy1=(int)(0.1*absy);
dy3=(int)(0.1*absy);
dy2=absy-dy1-dy3;
//calculate plot coordinates
for(int i=0;i<n;i++)
{x_pixel[i]=dx1+(int)(dx2*(x[i]-xmin)/(xmax-xmin));
y_pixel[i]=dy1+dy2-(int)(dy2*(y[i]-ymin)/(ymax-ymin));
}
//draw plot line
GeneralPath shape=new GeneralPath(GeneralPath.WIND_EVEN_ODD);
shape.moveTo(x_pixel[0],y_pixel[0]);
for(int i=1;i<n;i+=2)
{ //if
the limits are outside the plot window
//plot the limit value
if(x_pixel[i]<dx1) x_pixel[i]=dx1;
else
if(x_pixel[i]>(dx1+dx2)) x_pixel[i]=(dx1+dx2);
if(y_pixel[i]>(dy1+dy2)) y_pixel[i]=dy1+dy2;
else
if(y_pixel[i]<dy1) y_pixel[i]=dy1;
shape.lineTo(x_pixel[i],y_pixel[i]);}
g2.draw(shape);
Rectangle2D d=new
Rectangle2D.Double(dx1,dy1,dx2,dy2);
g2.draw(d);
//draw xtick
int xtick[]=new int[ntick];
Line2D xt[]=new Line2D[ntick];
for(int i=0;i<ntick;i++)
{xtick[i]=dx1+dx2*i/ntick;
if(!gridon)
xt[i]=new
Line2D.Double(xtick[i],(dy1+dy2),xtick[i],(dy1+dy2*0.98));
else
xt[i]=new
Line2D.Double(xtick[i],(dy1+dy2),xtick[i],dy1);
g2.draw(xt[i]);
}
//draw x numbers
int xtick1[]=new int[ntick+1];
String xnumber[]=new String[ntick+1];
for(int i=0;i<=ntick;i++)
{xtick1[i]=(int)(dx1*0.98)+dx2*i/ntick;
double x1=(xmin+(xmax-xmin)*i/(double)ntick);
xnumber[i]=String.format(us,format,x1);
g2.drawString(xnumber[i],xtick1[i]-20,dy1+(int)(dy2*1.04));
}
//draw ytick
int ytick[]=new int[ntick];
Line2D yt[]=new Line2D[ntick]; for(int
i=1;i<ntick;i++)
{ytick[i]=dy1+dy2*i/ntick;
if(!gridon)
yt[i]=new
Line2D.Double(dx1,ytick[i],dx1+10,ytick[i]);
else
yt[i]=new
Line2D.Double(dx1,ytick[i],(dx1+dx2),ytick[i]);
g2.draw(yt[i]);
}
//draw y numbers
int ytick1[]=new int[ntick+1];
String ynumber[]=new String[ntick+1];
for(int i=0;i<=ntick;i++)
{ytick1[i]=dy1+dy2-dy2*i/ntick;
ynumber[i]=String.format("%8.2f",(ymin+(ymax-ymin)*i/(double)ntick));
g2.drawString(ynumber[i],dx1/2,ytick1[i]);
//draw labels;
//x label
g2.drawString(xlabel,(dx1+dx2/2),(dy1+(int)(dy2*1.1)));
int xx1=dx1/3;
int yy1=(dy1+dy2/2);
//y label AffineTransform
at=AffineTransform.getRotateInstance(3.0*Math.PI/2.0,xx1,yy1);
g2.setTransform(at);
g2.drawString(ylabel,xx1,yy1);
at=AffineTransform.getRotateInstance(0,xx1,yy1);
g2.setTransform(at);
//plot label
g2.drawString(label,(dx1+dx2/2),(int)(dy1*0.8));
}
} public void
plot(String s) {
JFrame f = new JFrame(s);
f.add(this);
f.setBackground(cbg);
f.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
f.setSize(1200,800);
f.setVisible(true);
} public void
plot() { plot("Plot
window"); }
} |
import javax.swing.*; import java.awt.Graphics; import java.awt.*; import java.awt.event.*; class fb
extends f_x { public
double func(double x) {
return Math.tan(x);} } public class
AH11Ex1 extends JApplet { public void
init() { fb ff=new
fb();
plotP pp=new
plotP(ff,0.0,2.0*Math.PI,1000); pp.setYmin(-20.0); pp.setYmax(20.0);
pp.setPlabel("f(x)=tan(x)");
pp.setGridOn(); pp.setNthick(8); add(pp); } } |
HTML
file AH8EX1.html
<html> <applet
code="AH8Ex1.class" width=500 height=500> </applet> </html> |
import javax.swing.*; import java.awt.Graphics; import java.awt.*; import java.awt.event.*; class fb
extends f_x { public
double func(double x) {
return Math.tan(x);} } public class
AH8Ex1 extends JApplet { private
static final long serialVersionUID = 8200008L; public void
init() { fb ff=new
fb();
plotP pp=new
plotP(ff,0.0,2.0*Math.PI,1000); pp.setYmin(-20.0); pp.setYmax(20.0);
pp.setPlabel("f(x)=tan(x)");
pp.setGridOn(); pp.setNthick(8); add(pp); } } |
EXERCISE
1I Interface type Plot (same as previous program but use
Interface instead of
abstract class)
public
interface f_xI { double
func(double x);} |
import javax.swing.*; import java.awt.Graphics; import java.awt.*; import java.awt.event.*; import java.awt.geom.*; import java.util.Locale; public class
plotPI extends JPanel { private
static final long serialVersionUID = 8100008L;
Locale us=new Locale("US"); //real plot
coordinates double x[]; double y[]; //plot
coordinates converted to pixels int x_pixel[]; int y_pixel[]; // min maximum
in real plot coordinates double xmin; double xmax; double ymin; double ymax; //plot window
size pixel coordinates private int absx,absy; //plot
relative to plot window // |---dx1---|-----dx2------|-------dx3| //plot window
draw to dx2, dx1 and dx3 are left // open at the
right and left handside private int
dx1,dx2,dx3; // |---dy1---|-----dy2------|-------dy3| //plot window
draw to dy2, dy1 and dy3 are left // open at the
top and bottom handside private int
dy1,dy2,dy3; //number of data points int n; //number of
plot thicks int ntick; //plot color
and background color Color c,cbg; //plot labels String xlabel,ylabel,label; //Plot number
format String format; //Plot font Font font; boolean
gridon; boolean
splineon; public void
setXlabel(String s) {xlabel=s;} public void
setYlabel(String s) {ylabel=s;} public void
setPlabel(String s) {label=s;} public void
setLabels(String s1,String s2,String s3) {xlabel=s1;ylabel=s2;label=s3;} public void
setFont(Font ff) {font=ff;} public void
setFormat(String s) {format=s;} public void
setXmin(double xx1) {xmin=xx1;} public void
setXmax(double xx2) {xmax=xx2;} public void
setYmin(double xx) {ymin=xx;} public void
setYmax(double xx) {ymax=xx;} public void
setNthick(int nn) {ntick=nn;} public void
setColor(Color ci) {c=ci;} public void
setBackground(Color ci) {c=ci;} public void
setGridOn() {
gridon=true;} public void
setGridOff() {
gridon=false;} public void
setVariables(boolean b) { format="%8.2f"; c=Color.blue;cbg=Color.lightGray; font=new
Font("Courier",Font.PLAIN,16); ntick=10; xlabel="x"; ylabel="y"; label="y=f(x)"; gridon=false; splineon=b; } public
double[][] setFunc(f_xI f,double xmini,double
xmaxi) {n=500; return
setFunc(f,xmini,xmaxi,n); } public
double[][] setFunc(f_xI f,double xmini,double
xmaxi,int ni) {
n=ni;
double
x1[]=new double[n];
double
y1[]=new double[n]; for(int
i=0;i<n;i++)
{x1[i] =
xmini+(xmaxi-xmini)*i/(double)(n-1);
y1[i] = f.func(x1[i]);
}
double a[][]={x1,y1};
return a; } public
double[] min_max(double xi[]) {double ximin=1.0e80; double
ximax=-1.0e80; double ni=xi.length; for(int
i=0;i<ni;i++) { if(xi[i]<ximin)
ximin=xi[i]; if(xi[i]>ximax)
ximax=xi[i]; } double a[]={ximin,ximax}; return a; } public void
setData(double xy[][]) {double x1[]=xy[0]; double
y1[]=xy[1]; setData(x1,y1); } public void
setData(double xi[],double yi[]) {
if(splineon) {
double
a[][]=cubic_spline.funcSpline(xi,yi,10);
x=a[0]; y=a[1]; } else {x=xi;y=yi;}
double
a1[]=min_max(x);
n=x.length; xmin=a1[0]; xmax=a1[1]; double
a2[]=min_max(y);
ymin=a2[0]; ymax=a2[1];
int
x_pixel1[]=new int[n]; int
y_pixel1[]=new int[n]; x_pixel=x_pixel1; y_pixel=y_pixel1; gridon=false; } public
plotPI(f_xI f,double xmini,double
xmaxi,int ni) { //cubic
spline interpolation can be applied to the data setVariables(false); setData(setFunc(f,xmini,xmaxi,ni)); } public
plotPI(f_xI f,double xmini,double
xmaxi,int ni,boolean spline) { //cubic
spline interpolation can be applied to the data setVariables(spline); setData(setFunc(f,xmini,xmaxi,ni)); } public
plotPI(f_xI f,double xmini,double
xmaxi) { setVariables(false); setData(setFunc(f,xmini,xmaxi)); } public
plotPI(double xi[],double yi[],boolean spline) { setVariables(
spline); setData(xi,yi); } public void
paintComponent(Graphics g) {
Graphics2D g2=(Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
g2.setPaint(c);
g2.setFont(font);
Dimension size=getSize();
absx=size.width;
absy=size.height;
dx1=(int)(0.1*absx);
dx3=(int)(0.1*absx);
dx2=absx-dx1-dx3;
dy1=(int)(0.1*absy);
dy3=(int)(0.1*absy);
dy2=absy-dy1-dy3;
//calculate plot coordinates
for(int i=0;i<n;i++)
{x_pixel[i]=dx1+(int)(dx2*(x[i]-xmin)/(xmax-xmin));
y_pixel[i]=dy1+dy2-(int)(dy2*(y[i]-ymin)/(ymax-ymin));
}
//draw plot line
GeneralPath shape=new GeneralPath(GeneralPath.WIND_EVEN_ODD);
shape.moveTo(x_pixel[0],y_pixel[0]);
for(int i=1;i<n;i+=2)
{ //if
the limits are outside the plot window
//plot the limit value
if(x_pixel[i]<dx1)
x_pixel[i]=dx1;
else
if(x_pixel[i]>(dx1+dx2)) x_pixel[i]=(dx1+dx2);
if(y_pixel[i]>(dy1+dy2)) y_pixel[i]=dy1+dy2;
else
if(y_pixel[i]<dy1) y_pixel[i]=dy1;
shape.lineTo(x_pixel[i],y_pixel[i]);}
g2.draw(shape);
Rectangle2D d=new
Rectangle2D.Double(dx1,dy1,dx2,dy2);
g2.draw(d);
//draw xtick
int xtick[]=new int[ntick];
Line2D xt[]=new Line2D[ntick];
for(int i=0;i<ntick;i++)
{xtick[i]=dx1+dx2*i/ntick;
if(!gridon)
xt[i]=new
Line2D.Double(xtick[i],(dy1+dy2),xtick[i],(dy1+dy2*0.98));
else
xt[i]=new
Line2D.Double(xtick[i],(dy1+dy2),xtick[i],dy1);
g2.draw(xt[i]);
}
//draw x numbers
int xtick1[]=new int[ntick+1];
String xnumber[]=new String[ntick+1];
for(int i=0;i<=ntick;i++)
{xtick1[i]=(int)(dx1*0.98)+dx2*i/ntick;
double x1=(xmin+(xmax-xmin)*i/(double)ntick);
xnumber[i]=String.format(us,format,x1);
g2.drawString(xnumber[i],xtick1[i]-20,dy1+(int)(dy2*1.04));
}
//draw ytick
int ytick[]=new int[ntick];
Line2D yt[]=new Line2D[ntick];
for(int i=1;i<ntick;i++) {ytick[i]=dy1+dy2*i/ntick;
if(!gridon)
yt[i]=new
Line2D.Double(dx1,ytick[i],dx1+10,ytick[i]);
else
yt[i]=new
Line2D.Double(dx1,ytick[i],(dx1+dx2),ytick[i]);
g2.draw(yt[i]);
}
//draw y numbers
int ytick1[]=new int[ntick+1];
String ynumber[]=new String[ntick+1];
for(int i=0;i<=ntick;i++)
{ytick1[i]=dy1+dy2-dy2*i/ntick;
ynumber[i]=String.format("%8.2f",(ymin+(ymax-ymin)*i/(double)ntick));
g2.drawString(ynumber[i],dx1/2,ytick1[i]);
//draw labels;
//x label
g2.drawString(xlabel,(dx1+dx2/2),(dy1+(int)(dy2*1.1)));
int xx1=dx1/3;
int yy1=(dy1+dy2/2);
//y label
AffineTransform at=AffineTransform.getRotateInstance(3.0*Math.PI/2.0,xx1,yy1);
g2.setTransform(at);
g2.drawString(ylabel,xx1,yy1);
at=AffineTransform.getRotateInstance(0,xx1,yy1);
g2.setTransform(at);
//plot label
g2.drawString(label,(dx1+dx2/2),(int)(dy1*0.8));
}
} public void
plot(String s) {
JFrame f = new JFrame(s);
f.add(this);
f.setBackground(cbg);
f.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
f.setSize(1200,800);
f.setVisible(true);
} public void
plot() { plot("Plot
window"); }
} |
import java.awt.event.*; public class
AH8Ex1I extends JApplet { private
static final long serialVersionUID = 8200008L; public void
init() { f_xI
ff=x->Math.tan(x);
plotPI
pp=new plotPI(ff,0.0,2.0*Math.PI,1000); pp.setYmin(-20.0); pp.setYmax(20.0);
pp.setPlabel("f(x)=tan(x)");
pp.setGridOn(); pp.setNthick(8); add(pp); } } |
<html> <applet
code="AH8Ex1I.class" width=1000 height=500> </applet> </html> |
EXERCISE
2
import java.io.*; import java.applet.Applet; import java.awt.*; import java.awt.event.*; import java.util.*; import javax.swing.*; import javax.swing.table.*; import java.awt.*; import java.awt.event.*; import javax.swing.event.*; import java.awt.geom.*; public class
shapes { private
static final long serialVersionUID = 8100002L; public
static GeneralPath star_polygon( int p,int q,int x,int
y,int Ri,double angle) { GeneralPath
polygon1=new GeneralPath(); double
teta=2.0*Math.PI/p*q; double
R=Ri; polar P1=new polar(); polar P3=new polar(); for(int
i=0;i<p;i++) { double
teta1=angle+teta*i+Math.PI/2.0; double
teta3=angle+teta*(i+1)+Math.PI/2.0; P1.inputPolar(R,teta1); if(i==0)
polygon1.moveTo((x+(int)P1.xi()),(y-(int)P1.xj())); else
polygon1.lineTo((x+(int)P1.xi()),(y-(int)P1.xj()));
P3.inputPolar(R,teta3); polygon1.lineTo((x+(int)P3.xi()),(y-(int)P3.xj())); } polygon1.closePath(); return
polygon1; } public
static GeneralPath star_polygon1( int p,int q,int
x,int y,int Ri,double
angle) { GeneralPath
polygon1=new GeneralPath(); double
teta=2.0*Math.PI/p*q; double
R=Ri; polar P1=new polar(); polar P3=new polar(); for(int
i=0;i<p;i++) { double
teta1=angle+teta*i+Math.PI/2.0; double
teta3=angle+teta*(i+1)+Math.PI/2.0; P1.inputPolar(R,teta1); if(i==0)
{polygon1.moveTo((x+(int)P1.xi()),(y-(int)P1.xj()));
polygon1.lineTo(x,y);
} else {polygon1.lineTo((x+(int)P1.xi()),(y-(int)P1.xj()));
polygon1.lineTo(x,y);
} P3.inputPolar(R,teta3); polygon1.lineTo((x+(int)P3.xi()),(y-(int)P3.xj()));
} for(int
i=0;i<p;i++) { double
teta1=angle+teta*i+Math.PI/2.0; double
teta3=angle+teta*(i+1)+Math.PI/2.0; P1.inputPolar(R,teta1); if(i==0)
polygon1.moveTo((x+(int)P1.xi()),(y-(int)P1.xj())); else polygon1.lineTo((x+(int)P1.xi()),(y-(int)P1.xj()));
P3.inputPolar(R,teta3); polygon1.lineTo((x+(int)P3.xi()),(y-(int)P3.xj()));
} polygon1.closePath(); return
polygon1; }
public
static GeneralPath equilateral_polygon( int n,int
x,int y,int Ri,double
angle) {// bu yıldız cizime
teta=pi/2+aci radyandan baslar GeneralPath
polygon1=new GeneralPath(); double
teta=2.0*Math.PI/n; double
R=Ri; polar P1=new polar(); polar P3=new polar(); for(int
i=0;i<n;i++) { double
teta1=angle+teta*i+Math.PI/2.0; P1.inputPolar(R,teta1); if(i==0)
polygon1.moveTo((x+(int)P1.xi()),(y-(int)P1.xj())); else
polygon1.lineTo((x+(int)P1.xi()),(y-(int)P1.xj()));
double
teta3=angle+teta*(i+1)+Math.PI/2.0; P3.inputPolar(R,teta3); polygon1.lineTo((x+(int)P3.xi()),(y-(int)P3.xj()));
} polygon1.closePath(); return
polygon1; }
public
static GeneralPath ray( int n,int x,int y,int Ri,double angle) {// bu yıldız cizime
teta=pi/2+aci radyandan baslar GeneralPath
ray1=new GeneralPath(); double
teta=2.0*Math.PI/n; double
R=Ri; polar P1=new polar(); polar P3=new polar(); for(int
i=0;i<n;i++) { double
teta1=angle+teta*i+Math.PI/2.0; P1.inputPolar(R,teta1); ray1.moveTo(x,y);
ray1.lineTo((x+(int)P1.xi()),(y-(int)P1.xj()));
} //ray1.closePath(); return
ray1; } public static
GeneralPath star( int n,int x,int y,int ri,int Ri,double angle) {//star start to be drawn at
angle: teta=pi/2+angle
radian GeneralPath
star1=new GeneralPath(); double
teta=2.0*Math.PI/n; double
R=Ri; double
r=ri; polar P1=new polar(); polar P2=new polar(); polar P3=new polar(); for(int
i=0;i<n;i++) { double
teta1=angle+teta*i+Math.PI/2.0; double
teta2=teta/2+teta1; P1.inputPolar(R,teta1); P2.inputPolar(r,teta2); if(i==0)
star1.moveTo((x+(int)P1.xi()),(y-(int)P1.xj())); else
star1.lineTo((x+(int)P1.xi()),(y-(int)P1.xj())); star1.lineTo((x+(int)P2.xi()),(y-(int)P2.xj()));
double
teta3=angle+teta*(i+1)+Math.PI/2.0; P3.inputPolar(R,teta3); star1.lineTo((x+(int)P3.xi()),(y-(int)P3.xj()));
} star1.closePath(); return
star1; } public
static GeneralPath star1( int n,int x,int y,int ri,int Ri,double angle) {//star start to be drawn at
angle: teta=pi/2+angle
radian GeneralPath
star1=new GeneralPath(); double
teta=2.0*Math.PI/n; double
R=Ri; double
r=ri; if(r>R)
r=0.25*R; polar P1=new polar(); polar P2=new polar(); polar P3=new polar(); for(int
i=0;i<n;i++) { double
teta1=angle+teta*i+Math.PI/2.0; double
teta2=teta/2+teta1; P1.inputPolar(R,teta1); P2.inputPolar(r,teta2); if(i==0)
star1.moveTo((x+(int)P1.xi()),(y-(int)P1.xj())); //else
star1.lineTo((x+(int)P1.xi()),(y-(int)P1.xj()));
double
teta3=angle+teta*(i+1)+Math.PI/2.0; P3.inputPolar(R,teta3); star1.quadTo((x+(int)P2.xi()),(y-(int)P2.xj()),(x+(int)P3.xi()),(y-(int)P3.xj()));
} star1.closePath(); return
star1; } } |
import java.applet.Applet; import java.awt.*; import java.awt.event.*; import java.util.*; import javax.swing.*; import javax.swing.table.*; import java.awt.*; import java.awt.event.*; import javax.swing.event.*; import java.awt.geom.*; public class
star2P extends JPanel { private
static final long serialVersionUID = 8100012L; int x,y,r,R; double
angle; Color c; int side; boolean
first; GeneralPath
gp,gp1; public
star2P(Color c1, int n,int xi,int
yi,int ri,int Ri,double anglei) {super(); side=n; c=c1; x=xi;y=yi;R=Ri;r=ri; angle=anglei; first=true; } public void
setColor(Color c1) {c=c1;} public void
setside(int n) {side=n;} public void
setDrawOn() {first=true;} public
void setDrawOff() {first=false;} public void
paintComponent(Graphics g) { super.paintComponent(g); Graphics2D
g2=(Graphics2D)g;
g2.setFont(new
Font("Serif",Font.BOLD,24));
g2.setColor(c); g2.setStroke(new
BasicStroke(2.0f)); gp=shapes.star1(side,x,y,r,R,angle); gp1=shapes.ray(side,x,y,R,angle); if(first)
{g2.draw(gp);g2.draw(gp1);}
else g2.fill(gp); } } |
import javax.swing.*; import java.awt.Graphics; import java.awt.*; import java.awt.event.*; public class
AH11Ex2 extends JApplet { public void
init() { star2P
pp=new star2P(Color.blue,5,200,200,100,50,0); add(pp); } } |
<html> <applet
code="AH8Ex2.class" width=500 height=500> </applet> </html> |
EXERCISE
3
import javax.swing.*; import java.awt.*; public class
AH11Ex6 extends JApplet { public void
init() { Color c=Color.blue; //c=JColorChooser.showDialog(null,"color ",Color.red
); star4P pp=new
star4P(c,5,2,400,400,200,0); add(pp); } } |
import java.io.*; import java.applet.Applet; import java.awt.*; import java.awt.event.*; import java.util.*; import javax.swing.*; import javax.swing.table.*; import java.awt.*; import java.awt.event.*; import javax.swing.event.*; import java.awt.geom.*; public class
star4P extends JPanel { int x,y,R; double
angle; Color c; int p,q; GeneralPath
gp,gp1; public
star4P(Color c1, int pi,int
qi,int xi,int yi,int Ri,double anglei) {super(); p=pi;q=qi; c=c1; x=xi;y=yi;R=Ri; angle=anglei; } public void
setColor(Color c1) {c=c1;} public void
setside(int pi) {p=pi;} public void
setpolygon(int qi) {q=qi;} public void
paintComponent(Graphics g) { Graphics2D
g2=(Graphics2D)g;
g2.setFont(new
Font("Serif",Font.BOLD,24));
g2.setColor(c); g2.setStroke(new
BasicStroke(2.0f)); gp=shapes.star_polygon(p,q,x,y,R,angle); g2.draw(gp);
} } |
<html> <applet
code="AH11Ex6.class" width=500 height=500> </applet> </html> |
APPLET
HOMEWORKS
HOMEWORK 6) Plot function
y=Math.sin(x)*(0.1*Math.random()
) by using class
plotP between x=0
and x=p . Use JApplet class
HOMEWORK 7) Plot data
double x[]={1,2,3,4,5,6,7,8,9}
use
JApplet class ;
double y[]={-1,-2,-1,2,7,14,23,34,47}
HOMEWORK8)
Write a program to draw following graphics by using
JApplet
HOMEWORK 9)
Write a FrameGraphic program to draw a pentagon(five
sided shape) by
using JApplet
HOMEWORK
10) By
using class shape, draw
the following
shape by using JApplet, JFrame version of the program is given
below
import javax.swing.*; import java.awt.*; class H8Ex8 { public static
void main(String args[]) { star5P pp=new
star5P(Color.blue,41,17,500,500,300,0.0); pp.setDrawOn(); FrameGraphic.plot("star",pp); } } |
import javax.swing.table.*; import java.awt.*; import java.awt.event.*; import javax.swing.event.*; import java.awt.geom.*; public class
star5P extends JPanel { private
static final long serialVersionUID = 8100015L; int x,y,r,R; double
angle; Color c; int p,q; boolean
first; GeneralPath
gp,gp1; public
star5P(Color c1, int pi,int
qi,int xi,int yi,int Ri,double anglei) {super(); p=pi;q=qi; c=c1; x=xi;y=yi;R=Ri; angle=anglei; first=true; } public void
setColor(Color c1) {c=c1;} public void
setside(int pi) {p=pi;} public void
setpolygon(int qi) {q=qi;} public void
setDrawOn() {first=true;} public void
setDrawOff() {first=false;} public void
paintComponent(Graphics g) { super.paintComponent(g); Graphics2D
g2=(Graphics2D)g;
g2.setFont(new
Font("Serif",Font.BOLD,24)); g2.setColor(c); g2.setStroke(new
BasicStroke(2.0f)); gp=shapes.star_polygon1(p,q,x,y,R,angle); if(first)
{g2.draw(gp);}
else g2.fill(gp); } } |