Monday, December 21, 2015

Java Basic 2

1. ArrayList
public static void main(String[] args) {
// TODO Auto-generated method stub
ArrayList<String> list1 = new ArrayList<String>();
list1.add("Zoloo");
list1.add("Bataa");
list1.add("Miigaa");
list1.add("Hurlee");
String input = JOptionPane.showInputDialog("Please enter name:");
list1.add(input);
String msg="";
for(String s: list1) msg+=s + "\n";
JOptionPane.showMessageDialog(null, msg);
}
2. Reading and writing files 
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
String[] array = new String[]{"Carmelo Anthony",
                                                                        "Lebron James", "Kevin Durant",
                                                                        "Paul Pierce", "Paul George" };
BufferedWriter bw = new BufferedWriter(new FileWriter("file.txt"));
for(String s : array)
bw.write(s + "\n");
bw.close();
BufferedReader br = new BufferedReader(new FileReader("file.txt"));
String s = "";
String msg= "";
while((s = br.readLine())!=null)
msg += s + "\n";
br.close();
JOptionPane.showMessageDialog(null, msg);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
3. Map and HashMap
public static void main(String[] args) {
// TODO Auto-generated method stub
HashMap<String, Integer> map = new HashMap<String, Integer>();
map.put("Carmelo", 15);
map.put("Lebron", 23);
map.put("Kobe", 24);
map.put("Duncan", 21);
map.put("Dirk", 41);
int markofKobe = map.get("Kobe");
System.out.println("Marks of Kobe :" + markofKobe);
int markofDuncan = map.get("Duncan");
System.out.println("Marks of Duncan :" + markofDuncan);
map.remove("Kobe");
if(map.containsKey("Kobe"))
System.out.println("Kobe exists!");
else System.out.println("Kobe is gone!");
for(Entry<String, Integer> s : map.entrySet())
System.out.println("Key: " + s.getKey() + " Value: " + s.getValue());
}

4. Interface Example
Interface:
public interface Person {
public String whoRyou();
public String getName();
}

Class 2:
public class GoodPerson implements Person{

String name="";
public GoodPerson(String n) {
// TODO Auto-generated constructor stub
name = n;
}
@Override
public String whoRyou() {
// TODO Auto-generated method stub
return "Good";
}
@Override
public String getName() {
// TODO Auto-generated method stub
return name;
}
}

Class 1:
public class BadPerson implements Person{
String name="";

public BadPerson(String n) {
// TODO Auto-generated constructor stub
name  = n;
}
@Override
public String whoRyou() {
// TODO Auto-generated method stub
return "Bad";
}

@Override
public String getName() {
// TODO Auto-generated method stub
return name;
}
}
Main Class:
public class Killer {
public static void main(String[] args){
GoodPerson gp1 = new GoodPerson("Percy");
GoodPerson gp2 = new GoodPerson("Harry");
BadPerson bp1 = new BadPerson("Black");
BadPerson bp2 = new BadPerson("Reddy");
kill(gp1);
kill(gp2);
kill(bp1);
kill(bp2);
}
public static void kill(Person p){
if(p instanceof BadPerson)
System.out.println("Killed " + p.getName() + ". Feels good to kill a bad person.");
else if(p instanceof GoodPerson)
System.out.println("Killed " + p.getName() + ". Cannot kill a good person.");
}
}
5. Abstraction 
public abstract class Shape {

String colorname = "";
int vertices = 0;
public abstract void Draw();
public String getColorname() {
return colorname;
}
public void setColorname(String colorname) {
this.colorname = colorname;
}
public int getVertices() {
return vertices;
}
public void setVertices(int vertices) {
this.vertices = vertices;
}
}
public class Circle extends Shape{

public Circle(String n){
super.setColorname(n);
super.setVertices(0);
System.out.println("Circle has been created!");
}
@Override
public void Draw() {
// TODO Auto-generated method stub
System.out.println("Circle has been drawn");
}
}
main Class:
public static void main(String[] args){
Circle c1=  new Circle("Black");
Circle c2 = new Circle("Red");
Shape c3 = new Circle("Blue");
c1.Draw();
c2.Draw();
c3.Draw();
}
https://www.youtube.com/playlist?list=PL8F0C07390BAC102A

Wednesday, December 16, 2015

Java GUI Basic 1

1. JAVA GUI
JRadioButton Example:
public class someClass extends JFrame{
      JTextField tf;
      Font pf;
      Font bf;
      Font itf;
      Font bif;
      JRadioButton pb;
      JRadioButton bb;
      JRadioButton ib;
      JRadioButton bib;
      ButtonGroup group;
     
      public someClass(){
            super("The Title");
            setLayout(new FlowLayout());
            tf = new JTextField("Bucky is awesome and hot", 25);
            add(tf);
            pb = new JRadioButton("plain",true);
            bb = new JRadioButton("bold", false);
            ib = new JRadioButton("italic", false);
            bib = new JRadioButton("bold and italic", false);
            add(pb);
            add(bb);
            add(ib);
            add(bib);
           
            group = new ButtonGroup();
            group.add(pb);
            group.add(bb);
            group.add(ib);
            group.add(bib);
           
            pf = new Font("Serif", Font.PLAIN, 14);
            bf = new Font("Serif", Font.BOLD, 14);
            itf = new Font("Serif", Font.ITALIC, 14);
            bif = new Font("Serif", Font.BOLD + Font.ITALIC, 14);
           
            tf.setFont(pf);
            pb.addItemListener(new HandlerClass(pf));
            bb.addItemListener(new HandlerClass(bf));
            ib.addItemListener(new HandlerClass(itf));
            bib.addItemListener(new HandlerClass(bif));
      }
     
      class HandlerClass implements ItemListener{
            Font font;
           
            public HandlerClass(Font f){
                  font = f;
            }

            @Override
            public void itemStateChanged(ItemEvent arg0) {
                  // TODO Auto-generated method stub
                  tf.setFont(font);
            }
           
      }
     
}

mainClass:
      public static void main(String[] args) {
            // TODO Auto-generated method stub
            someClass sc = new someClass();
            sc.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            sc.setSize(300, 200);
            sc.setVisible(true);
      }
Result:

JList Example:
public class someClass extends JFrame{
      JList list;
      static String[] colornames = {"black", "blue", "red", "white"};
      static Color[] colors = {Color.BLACK, Color.BLUE, Color.RED, Color.WHITE};
      public someClass(){
            super("Title");
            setLayout(new FlowLayout());
            list = new JList(colornames);
            list.setVisibleRowCount(4);
            list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
            add(new JScrollPane(list));
           
            list.addListSelectionListener(new ListSelectionListener() {
                 
                  @Override
                  public void valueChanged(ListSelectionEvent arg0) {
                        // TODO Auto-generated method stub
                        getContentPane().setBackground(colors[list.getSelectedIndex()]);
                  }
            });
      }
}
Result:

Multiple Selection List Example:
public class someClass extends JFrame{
      JList leftlist;
      JList rightlist;
      JButton movebutton;
     
      static String[] food = {"bacon", "wings", "ham", "beef", "orange","banana"};
     
      public someClass(){
            super("Title");
            setLayout(new FlowLayout());
           
            leftlist = new JList(food);
            leftlist.setVisibleRowCount(4);
leftlist.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
            add(new JScrollPane(leftlist));
            movebutton = new JButton("Move--->");
            movebutton.addActionListener(new ActionListener() {
                 
                  @Override
                  public void actionPerformed(ActionEvent arg0) {
                        // TODO Auto-generated method stub
                        rightlist.setListData(leftlist.getSelectedValues());
                  }
            });
           
            add(movebutton);
            rightlist = new JList();
            rightlist.setVisibleRowCount(3);
            rightlist.setFixedCellWidth(100);
            rightlist.setFixedCellHeight(15);
rightlist.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
            add(new JScrollPane(rightlist));
           
      }
}

Result:
Simple Adapter Class Example
MouseAdapter Class:
public class Gui extends JFrame {

      private String details;
      private JLabel statusbar;
     
      public Gui(){
            super("Title");
           
            statusbar = new JLabel("This is default");
            add(statusbar, BorderLayout.SOUTH);
            addMouseListener(new MouseClass());
      }
     
      private class MouseClass extends MouseAdapter{
            public void mouseClicked(MouseEvent ev){
            details = String.format("You clicked %d", ev.getClickCount());
                  if(ev.isMetaDown())
                        details +=" with right mouse button";
                  else if(ev.isAltDown())
                        details +=" either center mouse button";
                  else details +=" with left mouse button";
                 
                  statusbar.setText(details);  
            }
      }
}
Main class:
      public static void main(String[] args) {
            // TODO Auto-generated method stub
            Gui go = new Gui();
            go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            go.setSize(300, 200);
            go.setVisible(true);
      }
Result:

FlowLayout:
public class someClass extends JFrame{
      JButton a;
      JButton b;
      JButton c;
      FlowLayout layout;
      Container cont;
     
      public someClass(){
            super("Layout tutorial");
            layout = new FlowLayout();
            cont = getContentPane();
            setLayout(layout);
            a = new JButton("Left");
            add(a);
            a.addActionListener(new ActionListener() {
                 
                  @Override
                  public void actionPerformed(ActionEvent arg0) {
                        // TODO Auto-generated method stub
                        layout.setAlignment(FlowLayout.LEFT);
                        layout.layoutContainer(cont);
                  }
            });
            // center
            b = new JButton("Center");
            add(b);
            b.addActionListener(new ActionListener() {
                 
                  @Override
                  public void actionPerformed(ActionEvent arg0) {
                        // TODO Auto-generated method stub
                        layout.setAlignment(FlowLayout.CENTER);
                        layout.layoutContainer(cont);
                  }
            });
            // right
            c = new JButton("Right");
            add(c);
            c.addActionListener(new ActionListener() {
                 
                  @Override
                  public void actionPerformed(ActionEvent arg0) {
                        // TODO Auto-generated method stub
                        layout.setAlignment(FlowLayout.RIGHT);
                        layout.layoutContainer(cont);
                  }
            });
      }
     
}
Main class:
      public static void main(String[] args) {
            // TODO Auto-generated method stub
            someClass sc = new someClass();
            sc.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            sc.setSize(300, 100);
            sc.setVisible(true);
      }

Drawing Graphics:
public class someClass extends JPanel{
      public void paintComponent(Graphics g){
            super.paintComponent(g);
            this.setBackground(Color.WHITE);
            g.setColor(Color.WHITE);
            g.drawLine(10, 25, 200, 45);
            g.setColor(Color.RED);
            g.drawRect(10, 55, 100, 30);
            g.setColor(Color.GREEN);
            g.fillOval(10, 95, 100, 30);
            g.setColor(Color.ORANGE);
            g.fill3DRect(10, 160, 100, 50, true);
            g.setColor(Color.BLACK);
            g.drawString("This is string", 15, 150);
           
      }
}
Main class:
      public static void main(String[] args) {
            // TODO Auto-generated method stub
            JFrame jf = new JFrame("Title");
            jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            someClass sc = new someClass();
            sc.setBackground(Color.WHITE);
            jf.add(sc);
            jf.setSize(500, 300);
            jf.setVisible(true);
      }
Result: