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:



Wednesday, December 9, 2015

Java Basic 1

1. Variable Length Arguments:
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(avg(5,2,3,5,6,7));
System.out.println(avg(10,11,21,33));

}

public static int avg(int...numbers){
int t = 0;
for(int x: numbers)
t+=x;
return t/numbers.length;
}

2. Enumeration and EnumSet range:
public enum someClass {
bucky("nice", "22"),
kelsey("cutie", "10"),
julia("bigmistake", "12"),
bataa("cool","25"),
azaa("good", "15");

final String desc;
final String year;

someClass(String de, String b){
desc = de;
year = b;
}
public String getDesc(){
return desc;
}
public String getYear(){
return year;
}

}

mainclass:
public static void main(String[] args) {
// TODO Auto-generated method stub

for(someClass c: EnumSet.range(someClass.kelsey, someClass.bataa))
//for(someClass c: someClass.values())

System.out.printf("%s\t%s\t%s\n", c, c.desc, c.year);

}
3. Static Example:
public class someClass {
String fname;
String lname;
static int members=0;

someClass(String f, String l){
fname = f;
lname = l;
members++;
System.out.printf("\nNew Member firstname: %s, lastname: %s, Id: %d", fname, lname, members);
}
public String getFname(){
return fname;
}
public String getLname(){
return lname;
}
public int getMembers(){
return members;
}
}

mainClass:
public static void main(String[] args) {
// TODO Auto-generated method stub
someClass sc1 = new someClass("Carmelo", "Anthony");
someClass sc2 = new someClass("Lebron", "James");
someClass sc3 = new someClass("Chris", "Paul");

System.out.println("\n" + sc1.getMembers());

}


Array Holding Many Objects:
 public class someClassList {
     
      someClass[] sc = new someClass[5];
      int i = 0;
     
      public void add(someClass s){
            if(i<sc.length)
            {
                  sc[i] = s;
                  System.out.println("some class added by index: " + i);
                  i++;
                  }
            }

}
B.  public class OtherClass extends someClass{

}
C.  public class someClass{

}    
Main class:
public class func1{
     
      public static void main(String[] args) {
            // TODO Auto-generated method stub
            someClassList scl = new someClassList();
            OtherClass os = new OtherClass();
            someClass sc = new someClass();
            scl.add(os);
            scl.add(sc);
           
      }
}

Simple Polymorphic:
public class someClass{
     
      public void noise(){
            System.out.println("Base class noise");
      }

}
public class OtherClass extends someClass{
      public void noise(){
            System.out.println("OtherCLass noise");
      }
}
Main Class:
      public static void main(String[] args) {
            // TODO Auto-generated method stub
            OtherClass os = new OtherClass();
            os.noise();
            someClass sc = new someClass();
            sc.noise();
      }


File
Creating Files:
      public static void main(String[] args) {
            // TODO Auto-generated method stub
            final Formatter x;
            try{
                  x = new Formatter("javatutorial.txt");
                  System.out.println("You created a file!");
                 
            }catch(Exception ex){
                  System.out.println(ex.getMessage());
            }
      }
Writing to Files:
public class someClass{
     
      private Formatter x;
      public void openFile(){
            try{
                  x = new Formatter("javaa.txt");    
            } catch (Exception ex){
                  System.out.println(ex.getMessage());
            }
      }
      public void addRecords(){
            x.format("%s%s%s", "15", "Carmelo", "Anthony");
      }
      public void closeFile(){
            x.close();
      }
}
Main class:
      public static void main(String[] args) {
            // TODO Auto-generated method stub
            someClass sc = new someClass();
            sc.openFile();
            sc.addRecords();
            sc.closeFile();
      }
Reading to Files:
 public class someClass{
      private Scanner x;
     
      public void openFile(){
            try{
                  x = new Scanner(new File("javaa.txt"));
            } catch (Exception ex){
                  System.out.println(ex.getMessage());
            }
      }
     
      public void readFile(){
            String a, b, c;
            while(x.hasNext()){
                  a = x.next();
                  b = x.next();
                  c = x.next();
                  System.out.printf("%s %s %s\n", a, b, c);
            }
      }
     
      public void closeFile(){
            x.close();
      }
}
Main class:
      public static void main(String[] args) {
            // TODO Auto-generated method stub
            someClass sc = new someClass();
            sc.openFile();
            sc.readFile();
            sc.closeFile();

      } 

Видео линк:https://www.youtube.com/playlist?list=PLFE2CE09D83EE3E28