EX1: investigate the graphic programs listed above EX 2) Investigate

advertisement
WEEK 3 2012 CLASS EXERSIZES (Wil be shown at the and of Lab class)
Applet Name
Frame Name
Panel
Additional
program
html
Control_boxA.java
Control_boxF.java
Control_boxP.java
Control_boxA.html
Control_boxA1.java
Control_boxF1.java
Control_boxP1.java
Control_boxA1.html
Control_boxA3.java
Control_boxF3.java
Control_boxP3.java
Control_boxA3.html
listA.java
listF.java
listP.java
listA.html
listA1.java
listF1.java
listP1.java
shapeP
listA1.html
sliderA.java
sliderF.java
sliderP.java
ovalP
sliderA.html
radio_controlA.java
radio_controlF.java
radio_controlP.java
radio_controlA.html
radio_control_boxA3.java
radio_control_boxF3.java
radio_control_boxP3.java
radio_control_boxA3.html
mouseA.java
mouseF.java
mouseP.java
mouseA.html
mouseA1.java
mouseF1.java
mouseP1.java
mouseA1.html
mouseA2.java
mouseF2.java
mouseP2.java
mouseA2.html
mouseA3.java
mouseF3.java
mouseP3.java
mouseA3.html
mouseA4.java
mouseF4.java
mouseP4.java
mouseA4.html
Menu1.java
Menu2.java
BorderLayoutTest.java
FlowLayoutTest.java
GridLayoutTest.java
JTabbedPaneTest.java
JOptionPane1.java
EX1: investigate the graphic programs listed above
EX 2) Investigate mouseF4.java mouseP4.java and draw the following shape by using the program
sliderTest.java
EX3 ) OvalP.java and SliderTest.java is given above. Investigate these programs. By using starP.java
ctreate SliderTest1.java to draw a 5 sided star and change the size of the star through changing the
slider position.
EX4 ) Investigate shapeP and listA1.java. ChangelistA1.java to a new program listA2.java so that
page will be divided into two pages by using a JTabbedPane layout formatting. In the first page list
controls will be located. In the second page the shape will be located
HOMEWORK PROBLEMS
HW1) Investigate JOptionPane1.java program replace Object[] o=new Object[]{"…}; term from the
program and replace with the following and then select your own name.
Object[] o={"MUSA
"BERK
"Başak
"Veysel
"NAİL
"TUNÇ
"ARDA
"BENGÜL
"MEHMET ARDA
"ONUR
"RİYAD GÖRKEM
"AYBÜKE
"ABDURRAHMAN
"ERDAL
"OKAN
"YASİN
"SEYFETTİN
"HAKAN
"BURAK
"İLKER OKAN
"ŞENER
"AYDIN
"HARUN
"MEHMET
"BAKHROM
"İBRAHİM
"SERAY
"YUNUS
"HAYDAR MURAT
"MURAT
"ERDİ
"HAKAN ÖNDER
"FARUK
"EMRE
"FATİH
"TOLGA
"TUĞBA
"ÇAĞRI ALİM SEYİT
"NİHAT
"EMRE
GÜNGÖRÜRLER
",
",
",
",
",
",
",
",
",
KURAN",
ORTAÇ
",
HASTUÇ
ÖREN
",
ALAK
",
BAKIRTAŞ
",
CENGİZ ",
KALKAN
TEMİZ
",
YILMAZ ",
AKSU
",
ELBİRLİK
",
NARİN
",
TOZLU
",
ZHUMAEV
USLUER
",
ŞEN
",
IŞILDAR
",
SÜTŞURUP",
URAL
",
GÖKALP
",
ÇOLAK ",
YILMAZ
",
ŞİMŞEK
",
TOPLAR
",
ALTINOLUK
",
BİLGİLİ
",
METİN",
KURT
",
GÜRSOY
",
",
SUROYI
ŞİLEN
AKSOY
YAKUPOĞLU
YAZICI
İNÇELİ
ERKEK
ÖZDEN
ÖCAL
",
",
",
"SERTAÇ
"GÖKSEL
"SERCAN
"AHMET HİLMİ
"ORHUN ESAT
"FIRAT
"MERTCAN
"KAZİM
"SEFA
"ENDER
"ANIL
"TANJU
"ÖMER KEMAL
"ONUR
"ONUR
"ŞEREF
"MERVE
"OYTUN OZAN
"EZGİ
"HATİCE EBRU
"SERHAT
"NEDİM
"OĞULCAN
"SERKAN
"ARDA
"ÖMER
"ÖZGÜN
"İSMAİL CEM
"İREM
"BERK
"FURKAN
"DOĞUKAN
"CAN BERKER
"YİĞİT
"HASAN
"FATİH
"OLGAY
"AHMET
"ERKAN
"MIRAM
"ARTUN
"ERİNÇ
"SİNAN
"İSMAİL
“AYTEKİN
SERDUMAN
",
KÖSE
",
KAYNAK
PAKSOY
",
ŞAFAK ",
TOKUL
",
MUT
",
ORAN ",
EKER ",
YILDIRIM",
YILDIZ ",
AKYÜZ ",
ÇELİK",
YILMAZTÜRK",
ŞEN
",
KERECİ ",
KARABEY
",
KORKMAZ",
AKÇA
",
EVREN ",
SAYDAM
PEHLİVAN
",
IŞITMAN
ORUN
",
ÇAKIRCI
",
RÜZGAR
",
KÖKSAL
",
YÜRÜŞEN
",
AYDIN
",
KARAKAŞ
",
KADAKAL
DENİZ
",
ÖZDEM ",
BÜYÜKBOYACI ",
OĞUROL
",
KAYA
",
ÇAKIR
",
PEHLİVANOĞULLARI",
ARSLAN
",
BAISSEYEV
",
TÜTEN
",
BAYTEKİN
",
ÇİFTÇİ
",
YILMAZ
",
RUMİBEYOĞLU”};
",
",
",
",
HW2: Write the following program and find your name, make screen blue and print the result sheet as
output
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.*;
import java.awt.Graphics;
public class Menu3 extends JFrame implements ListSelectionListener
{
JPanel inputPanel;
JList colorlist;
JList studentlist;
JTextField name;
JMenuBar menuBar;
JMenu m1,m2;
String studentnames={"MUSA
GÜNGÖRÜRLER ",
"BERK
SUROYI
",
"Başak
ŞİLEN
",
"Veysel
AKSOY
",
"NAİL
YAKUPOĞLU
",
"TUNÇ
YAZICI
",
"ARDA
İNÇELİ
",
"BENGÜL
ERKEK
",
"MEHMET ARDA
ÖZDEN ",
"ONUR
ÖCAL
",
"RİYAD GÖRKEM
KURAN",
"AYBÜKE
ORTAÇ
",
"ABDURRAHMAN
HASTUÇ
",
"ERDAL
ÖREN
",
"OKAN
ALAK
",
"YASİN
BAKIRTAŞ
",
"SEYFETTİN
CENGİZ ",
"HAKAN
KALKAN
",
"BURAK
TEMİZ
",
"İLKER OKAN
YILMAZ ",
"ŞENER
AKSU
",
"AYDIN
ELBİRLİK
",
"HARUN
NARİN
",
"MEHMET
TOZLU
",
"BAKHROM
ZHUMAEV
",
"İBRAHİM
USLUER
",
"SERAY
ŞEN
",
"YUNUS
IŞILDAR
",
"HAYDAR MURAT
SÜTŞURUP",
"MURAT
URAL
",
"ERDİ
GÖKALP
",
"HAKAN ÖNDER
ÇOLAK ",
"FARUK
YILMAZ
",
"EMRE
ŞİMŞEK
",
"FATİH
TOPLAR
",
"TOLGA
ALTINOLUK
",
"TUĞBA
BİLGİLİ
",
"ÇAĞRI ALİM SEYİT
METİN",
"NİHAT
KURT
",
"EMRE
GÜRSOY
",
"SERTAÇ
SERDUMAN
",
"GÖKSEL
KÖSE
",
"SERCAN
KAYNAK
",
"AHMET HİLMİ
PAKSOY
",
"ORHUN ESAT
ŞAFAK ",
"FIRAT
TOKUL
",
"MERTCAN
MUT
",
"KAZİM
ORAN ",
"SEFA
EKER ",
"ENDER
YILDIRIM",
"ANIL
YILDIZ ",
"TANJU
AKYÜZ ",
"ÖMER KEMAL
ÇELİK",
"ONUR
YILMAZTÜRK",
"ONUR
ŞEN
",
"ŞEREF
KERECİ ",
"MERVE
KARABEY
",
"OYTUN OZAN
KORKMAZ",
"EZGİ
AKÇA
",
"HATİCE EBRU
EVREN ",
"SERHAT
SAYDAM
",
"NEDİM
PEHLİVAN
",
"OĞULCAN
IŞITMAN
",
"SERKAN
ORUN
",
"ARDA
ÇAKIRCI
",
"ÖMER
RÜZGAR
",
"ÖZGÜN
KÖKSAL
",
"İSMAİL CEM
YÜRÜŞEN
",
"İREM
AYDIN
",
"BERK
KARAKAŞ
",
"FURKAN
KADAKAL
",
"DOĞUKAN
DENİZ
",
"CAN BERKER
ÖZDEM ",
"YİĞİT
BÜYÜKBOYACI ",
"HASAN
OĞUROL
",
"FATİH
KAYA
",
"OLGAY
ÇAKIR
",
"AHMET
PEHLİVANOĞULLARI",
"ERKAN
ARSLAN
",
"MIRAM
BAISSEYEV
",
"ARTUN
TÜTEN
",
"ERİNÇ
BAYTEKİN
",
"SİNAN
ÇİFTÇİ
",
"İSMAİL
YILMAZ
",
“AYTEKİN
RUMİBEYOĞLU”};
String colornames[]={"black","blue","cyan","dark gray","gray",
"green","light grey","magenta","orange","pink","red","white","yellow"};
Color colors[]={Color.black,Color.blue,Color.cyan,Color.darkGray,Color.gray,
Color.green,Color.lightGray,Color.magenta,Color.orange,Color.pink,
Color.red,Color.white,Color.yellow};
public Menu3()
{
super("Menu student list");
inputPanel=new JPanel();
name=new JTextField(studentnames[0]+"
");
inputPanel.add(name);
colorlist=new JList(colornames);
colorlist.setVisibleRowCount(10);
colorlist.addListSelectionListener(this);
studentlist=new JList(studentnames);
studentlist.setVisibleRowCount(10);
studentlist.addListSelectionListener(this);
menuBar=new JMenuBar();
setJMenuBar(menuBar);
m1 = new JMenu("Color List");
m1.setMnemonic('C');
m1.getAccessibleContext().setAccessibleDescription("color list");
m1.add(new JScrollPane(colorlist));
menuBar.add(m1);
m2 = new JMenu("Student List");
m2.setMnemonic('S');
m2.getAccessibleContext().setAccessibleDescription("student list");
m2.add(new JScrollPane(studentlist));
menuBar.add(m2);
add(inputPanel);
}
public void valueChanged( ListSelectionEvent e)
{
if(e.getSource()==colorlist)
{
Color r1=colors[colorlist.getSelectedIndex()];
inputPanel.setBackground(r1);
}
else if(e.getSource()==studentlist)
{
int i=studentlist.getSelectedIndex();
name.setText(studentnames[i]);
}
}
public static void main(String s[])
{
Menu3 f = new Menu3();
f.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
f.setSize(800,500);
f.setVisible(true);
}
}
HW3) Following programs draws a star in the window by using Mouse clik and drag. Number of
Sides and color of the star can be changed through menu controls.
public class polar
{
//polar coordinate system
protected double R,teta;
public polar()
{
R=0;
teta=0;
}
public polar(double Ri,double tetai)
{
R=Ri;
teta=tetai;
}
public polar(polar y)
{
R=y.R();
teta=y.teta();
}
public void inputR(double Ri)
{
R=Ri;
}
public void inputTeta(double tetai)
{
teta=tetai;
}
public void inputPolar(double Ri,double tetai)
{
R=Ri;
teta=tetai;
}
public void inputCartesian(double xi,double xj)
{
R=Math.sqrt(xi*xi+xj*xj);
teta=Math.atan2(xj,xi);
}
public double R()
{
return R;
}
public double teta()
{
return teta;
}
public double xi()
{
return R*Math.cos(teta);
}
public double xj()
{
return R*Math.sin(teta);
}
public void add(polar y)
{
kartesienGir((xi()+y.xi()),(xj()+y.xj()));
}
public void substract(polar y)
{
kartesienGir((xi()-y.xi()),(xj()-y.xj()));
}
public polar read_polar()
{
return this;
}
public boolean equals(polar v)
{
boolean b=((R==v.R())&&(teta==v.teta()));
return b;
}
public boolean greater_than(polar v)
{
return (this.R>v.R());
}
public boolean smaller_than(polar v)
{
return (this.R<v.R());
}
public String toString()
{
return ""+R+"*exp("+teta+"i) ";
}
}
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 starP extends JPanel implements MouseListener,MouseMotionListener
{
int x1,y1,x2,y2;
int starRadius;
Color c;
int side;
boolean first;
GeneralPath x;
public starP(Color c1,int n)
{super();
side=n;
c=c1;
x1=100;y1=100;x2=x1;y2=y1+30;
first=true;
addMouseListener(this);
addMouseMotionListener(this);
}
public void setColor(Color c1)
{c=c1;}
public void setside(int n)
{side=n;}
public void setCizgi(int x1i,int y1i,int x2i,int y2i)
{x1=x1i;y1=y1i;x2=x2i;y2=y2i;}
public GeneralPath star(int x,int y, int n,int starRadius,double aci)
{// bu yıldız cizime teta=pi/2+aci radyandan baslar
GeneralPath star1=new GeneralPath();
double teta=2.0*Math.PI/n;
double R=starRadius;
double r=starRadius*0.25;
polar P1=new polar();
polar P2=new polar();
polar P3=new polar();
for(int i=0;i<n;i++)
{
double teta1=teta*i+Math.PI/2.0+aci;
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=teta*(i+1)+Math.PI/2.0+aci;
P3.inputPolar(R,teta3);
star1.lineTo((x+(int)P3.xi()),(y-(int)P3.xj()));
}
star1.closePath();
return star1;
}
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));
starRadius=(int)Math.sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
x=star(x1,y1,side,starRadius,0);
g2.fill(x);
}
//MouseListener (fare dinleyicisi)
public void mouseClicked(MouseEvent e)
{}
public void mousePressed(MouseEvent e)
{
x1=e.getX();y1=e.getY();}
public void mouseReleased(MouseEvent e)
{ x2=e.getX();y2=e.getY();repaint();}
public void mouseEntered(MouseEvent e)
{}
public void mouseExited(MouseEvent e)
{}
//MouseMotionListener methods
public void mouseDragged(MouseEvent e)
{}
public void mouseMoved(MouseEvent e)
{}
}
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 star extends JApplet implements ListSelectionListener
{
starP sekil;
JMenuBar menuBar;
JMenu m1,m2;
JPanel outputPanel;
JList sekilList;
JList colorList;
String sekilNames[]={"three sided star","four sided star","five sided star",
"six sided star","seven sided star","eight sided star","nine sided star","ten sided star"};
String colorNames[]={"black","blue","cyan","dark gray","gray",
"green","light grey","magenta","orange","pink","red","white","yellow"};
Color colors[]={Color.black,Color.blue,Color.cyan,Color.darkGray,Color.gray,
Color.green,Color.lightGray,Color.magenta,Color.orange,Color.pink,
Color.red,Color.white,Color.yellow};
public void init()
{
int i=3;
sekil=new starP(Color.black,i);
colorList=new JList(colorNames);
colorList.setVisibleRowCount(4);
sekilList=new JList(sekilNames);
sekilList.setVisibleRowCount(4);
outputPanel=new JPanel();
outputPanel.setLayout(new BorderLayout());
outputPanel.add(sekil,BorderLayout.CENTER);
menuBar=new JMenuBar();
setJMenuBar(menuBar);
m1 = new JMenu("Color List");
m1.setMnemonic('R');
m1.getAccessibleContext().setAccessibleDescription("Starların hangi colorte olacağını seçer");
m1.add(new JScrollPane(colorList));
menuBar.add(m1);
m2 = new JMenu("Star List");
m2.setMnemonic('Y');
m2.getAccessibleContext().setAccessibleDescription("Starların kaç köşeli olacağını seçer");
m2.add(new JScrollPane(sekilList));
menuBar.add(m2);
add(outputPanel,BorderLayout.CENTER);
colorList.addListSelectionListener(this);
sekilList.addListSelectionListener(this);
}
public void valueChanged( ListSelectionEvent e)
{
if(e.getSource()==colorList)
{
Color r1=colors[colorList.getSelectedIndex()];
sekil.setColor(r1);
}
if(e.getSource()==sekilList)
{sekil.setside(sekilList.getSelectedIndex()+3);}
repaint();
}
public static void main(String s[])
{
JFrame f = new JFrame("Star control program push mouse and drag to draw");
f.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
JApplet applet = new star();
f.getContentPane().add("Center", applet);
applet.init();
f.pack();
f.setSize(new Dimension(545,400));
f.setVisible(true);
}
}
Investigate these programs. The following program is given to draw multi-sided shapes (triangle,
rectangle, pentagon, hexagon etc.). By changing star.java to create multiangle.java create a program to
draw multisided shaped instead of stars in the star.java program.
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 multiangleP extends JPanel implements MouseListener,MouseMotionListener
{
int x1,y1,x2,y2;
Color c;
int Corner;
boolean first;
GeneralPath x;
public multiangleP(Color c1,int n)
{super();
Corner=n;
c=c1;
x1=0;y1=0;x2=0;y2=0;
first=true;
addMouseListener(this);
addMouseMotionListener(this);
}
public void setColor(Color c1)
{c=c1;}
public void setCorner(int n)
{Corner=n;}
public void setCizgi(int x1i,int y1i,int x2i,int y2i)
{x1=x1i;y1=y1i;x2=x2i;y2=y2i;}
public GeneralPath multiangle(int x,int y, int n,int multiangleboyu,double aci)
{
GeneralPath multiangle=new GeneralPath();
double teta=2.0*Math.PI/n;
double R=multiangleboyu;
polar P1=new polar();
for(int i=0;i<n;i++)
{
double teta1=teta*i+Math.PI/2.0+aci;
P1.inputPolar(R,teta1);
if(i==0) multiangle.moveTo((x+(int)P1.xi()),(y-(int)P1.xj()));
else multiangle.lineTo((x+(int)P1.xi()),(y-(int)P1.xj()));
}
multiangle.closePath();
return multiangle;
}
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));
int multiangleboyu=(int)Math.sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
x=multiangle(x1,y1,Corner,multiangleboyu,0);
g2.fill(x);
}
//MouseListener
public void mouseClicked(MouseEvent e)
{}
public void mousePressed(MouseEvent e)
{
x1=e.getX();y1=e.getY();}
public void mouseReleased(MouseEvent e)
{ x2=e.getX();y2=e.getY();repaint();}
public void mouseEntered(MouseEvent e)
{}
public void mouseExited(MouseEvent e)
{}
//MouseMotionListener
public void mouseDragged(MouseEvent e)
{}
public void mouseMoved(MouseEvent e)
{}
}
Download