Advertisement
Guest User

JAVA2019

a guest
Jan 15th, 2019
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 23.72 KB | None | 0 0
  1. //Test1
  2. public class Test_P1
  3. {
  4.     public static void main(String[] args)
  5.     {
  6.         long startTime = System.currentTimeMillis();
  7.  
  8.        
  9.         Canvas ob1 = new Canvas("Compare ComboBox");
  10.         long endTime   = System.currentTimeMillis();
  11.         long totalTime = endTime - startTime;
  12.         System.out.println((float)totalTime/1000);
  13.     }
  14. }
  15. //==
  16.  
  17. import javax.swing.*;
  18. import java.awt.*;
  19.  
  20.  
  21. public class CustomPanel extends JPanel
  22. {
  23.     public int values[] = new int[4];
  24.  
  25.     public void selectedValues( int values[])
  26.     {
  27.        
  28.         this.values = values;
  29.        
  30.     }
  31.    
  32.     public void paintComponent(Graphics g)
  33.     {
  34.         for (int i = 0; i < 4; i++)
  35.         {
  36.             g.setColor(values[i] == 0 ? Color.GRAY : Color.RED);
  37.             g.fillRect(50+ i * 50, 100, 20, 20);
  38.            
  39.         }
  40.     }
  41. }
  42. //==
  43. import javax.swing.*;
  44. import java.awt.*;
  45. import java.awt.event.*;
  46.  
  47.  
  48. //@SuppressWarnings({ "rawtypes", "serial" , "unchecked"})
  49. public class Canvas extends JFrame implements ItemListener
  50. {
  51.     JComboBox JCB1,JCB2,JCB3,JCB4;
  52.     JPanel Panel;
  53.     JLabel TimeLabel;
  54.    
  55.     public int values[] = new int[4];
  56.     CustomPanel Paint = new CustomPanel();
  57.    
  58.     public Canvas(String title)
  59.     {
  60.        
  61.         Panel = new JPanel();
  62.        
  63.         this.setSize(300,300);
  64.         this.setLayout(new FlowLayout());
  65.        
  66.        
  67.        
  68.         JCB1=new JComboBox();
  69.         for(int i=0;i<=1;i++)
  70.             JCB1.addItem(i);
  71.         JCB1.addItemListener(this);
  72.        
  73.         JCB2=new JComboBox();
  74.         for(int i=0;i<=1;i++)
  75.             JCB2.addItem(i);
  76.         JCB2.addItemListener(this);
  77.        
  78.         JCB3=new JComboBox();
  79.         for(int i=0;i<=1;i++)
  80.             JCB3.addItem(i);
  81.         JCB3.addItemListener(this);
  82.        
  83.         JCB3=new JComboBox();
  84.         for(int i=0;i<=1;i++)
  85.             JCB3.addItem(i);
  86.         JCB3.addItemListener(this);
  87.        
  88.         JCB4=new JComboBox();
  89.         for(int i=0;i<=1;i++)
  90.             JCB4.addItem(i);
  91.         JCB4.addItemListener(this);
  92.        
  93.        
  94.         Panel.add(JCB1);
  95.         Panel.add(JCB2);
  96.         Panel.add(JCB3);
  97.         Panel.add(JCB4);
  98.         this.add(Panel);
  99.         this.add(Paint);
  100.        
  101.         this.setVisible(true);
  102.         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  103.     }
  104.    
  105.     public void itemStateChanged(ItemEvent ev)
  106.     {   values[0] = (int)JCB1.getSelectedItem();
  107.         values[1] = (int)JCB2.getSelectedItem();
  108.         values[2] = (int)JCB3.getSelectedItem();
  109.         values[3] = (int)JCB4.getSelectedItem();
  110.         Paint.selectedValues(values);
  111.        
  112.         Paint.paintComponent(this.getGraphics());
  113.         //Paint.repaint();
  114.        
  115.     }
  116. }
  117. //==============================================================================
  118.  
  119. //Test2
  120. public class Test_P2
  121. {
  122.     public static void main(String[] args)
  123.     {      
  124.         Canvas ob1 = new Canvas("Compare ComboBox");
  125.  
  126.     }
  127. }
  128. //==
  129. import javax.swing.*;
  130. import java.awt.*;
  131. import java.awt.event.*;
  132.  
  133.  
  134.  
  135. public class Canvas extends JFrame implements KeyListener
  136. {
  137.     JTextField text;
  138.     JPanel Panel;
  139.     JButton wButton;
  140.     String readLine;
  141.    
  142.     InputOutput io = new InputOutput();
  143.     CustomPanel Paint = new CustomPanel();
  144.    
  145.     public Canvas(String title)
  146.     {
  147.         this.setSize(400,400);
  148.         this.setLayout(new FlowLayout());
  149.        
  150.         wButton = new JButton("Scrie");
  151.         Panel = new JPanel();
  152.         text = new JTextField(20);
  153.         text.addKeyListener(this);
  154.         wButton.addActionListener(new ActionListener() {
  155.             @Override
  156.             public void actionPerformed(ActionEvent e) {
  157.                 io.setText(text.getText());
  158.                 io.writer();
  159.                
  160.             }
  161.         });
  162.        
  163.         Panel.add(text);
  164.         Panel.add(wButton);
  165.         this.add(Panel);
  166.         this.add(Paint);
  167.                
  168.         this.setVisible(true);
  169.         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  170.        
  171.     }
  172.  
  173.     @Override
  174.     public void keyTyped(KeyEvent e) {
  175.         // TODO Auto-generated method stub
  176.        
  177.     }
  178.  
  179.     @Override
  180.     public void keyPressed(KeyEvent e) {
  181.         // TODO Auto-generated method stub
  182.          Paint.setValues(e.getKeyChar());
  183.          Paint.paintComponent(this.getGraphics());
  184.     }
  185.  
  186.     @Override
  187.     public void keyReleased(KeyEvent e) {
  188.         // TODO Auto-generated method stub
  189.        
  190.         //Paint.paint(arg0.getKeyChar();
  191.     }
  192. }
  193. //==
  194. import javax.swing.*;
  195. import java.awt.*;
  196. import java.util.Random;
  197.  
  198.  
  199. public class CustomPanel extends JPanel
  200. {
  201.     Random rnX = new Random();
  202.     Random rnY = new Random();
  203.     int rndX;
  204.     int rndY;
  205.     CustomPanel(){
  206.         revalidate();
  207.        
  208.     }
  209.     public String c = " ";
  210.  
  211.     public void setValues(char c)
  212.     {
  213.         this.c = new StringBuilder().append(c).toString();        
  214.     }
  215.    
  216.     public void paintComponent(Graphics g)
  217.     {
  218.  
  219.         rndX =  rnX.nextInt(400);
  220.         rndY =  rnY.nextInt(290)+100;
  221.  
  222.        
  223.         g.drawString(c,rndX,rndY);
  224.        
  225.     }
  226. }
  227. //===
  228.  
  229. import java.io .* ;
  230. public class InputOutput{
  231.    
  232.     String str;
  233.     public void writer(){
  234.         try {
  235.        BufferedWriter out = new BufferedWriter(new FileWriter("teste.txt"));
  236.             out.write(str);
  237.             System.out.println("DONE");
  238.             out.close();
  239.         }
  240.         catch(IOException e){
  241.             System.err.println(e.getMessage ());}
  242.  
  243.     }
  244.     public void setText(String str) {
  245.         this.str = str;
  246.     }
  247. }
  248. //=============================================================================
  249.  
  250. //Test3
  251.  
  252. public class Test_P3 {
  253.     public static void main(String[] args) throws Exception
  254.     {      
  255.         Canvas ob1 = new Canvas("Compare ComboBox");
  256.  
  257.     }
  258.  
  259. }
  260. //===
  261. import javax.swing.*;
  262. import java.awt.*;
  263. import java.awt.event.*;
  264.  
  265.  
  266.  
  267. public class Canvas extends JFrame implements ActionListener
  268. {
  269.     JTextField text;
  270.     JPanel Panel;
  271.     JButton wButton;
  272.     String readLine ="";
  273.     JTextField[] jText;
  274.     JLabel[] jDesc;
  275.     JLabel[] jVal;
  276.     JButton vButton = new JButton("Verifica");
  277.     InputOutput io = new InputOutput();
  278.     public int strLen = 0;
  279.    
  280.     public Canvas(String title) throws Exception
  281.     {
  282.         this.setSize(400,400);
  283.         this.setLayout(new FlowLayout());
  284.         readLine = io.reader();
  285.         strLen = readLine.length();
  286.         jDesc = new JLabel[readLine.length()];
  287.         jVal = new JLabel[readLine.length()];
  288.         jText = new JTextField[readLine.length()];
  289.         Panel = new JPanel();
  290.        
  291.         GridLayout grid = new GridLayout(readLine.length()+1, 3, 10, 10);
  292.         Panel.setLayout(grid);
  293.  
  294.        
  295.         for(int i=0; i<readLine.length(); i++) {
  296.             jDesc[i] = new JLabel();
  297.             Panel.add(jDesc[i]);
  298.             jText[i] = new JTextField(1);
  299.             Panel.add(jText[i]);
  300.             jVal[i] = new JLabel();
  301.             Panel.add(jVal[i]);
  302.             jDesc[i].setText(" "+(i+1));
  303.             jVal[i].setText("");
  304.         }
  305.         vButton.addActionListener(this);
  306.         Panel.add(vButton);
  307.         this.add(Panel);
  308.                
  309.         this.setVisible(true);
  310.         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  311.          
  312.     }
  313.     @Override
  314.     public void actionPerformed(ActionEvent e) {
  315.         for(int i=0; i<readLine.length(); i++) {
  316.             char c = jText[i].getText().charAt(0);
  317.             if(Character.isDigit(c)) {
  318.                 if(readLine.charAt(i)< c) {
  319.                     jVal[i].setText(">");
  320.                 }
  321.                 else if(readLine.charAt(i)>c) {
  322.                     jVal[i].setText("<");
  323.                 }
  324.                 else {
  325.                     jVal[i].setText("=");
  326.                 }
  327.             }
  328.             else {
  329.                 jVal[i].setText("?");
  330.             }
  331.            
  332.         }
  333.     }
  334. }
  335. //===
  336.  
  337. import java.io .* ;
  338. public class InputOutput {
  339.    
  340.     String str;
  341.     String readLine ="";
  342.     String file="read.txt";
  343.     public String reader()throws Exception {
  344.         BufferedReader br = new BufferedReader(new FileReader(file));
  345.                while ((str = br.readLine()) != null) {
  346.                   System.out.println(str);
  347.                   readLine += str;
  348.                }
  349.         return readLine;
  350.     }
  351.     public void setText(String str) {
  352.         this.str = str;
  353.     }
  354. }
  355.  
  356. //==============================================================================
  357.  
  358. //Test4
  359.  
  360. public class Test_P4 {
  361.     public static void main(String[] args) throws Exception
  362.     {      
  363.         Canvas ob1 = new Canvas("Compare ComboBox");
  364.  
  365.     }
  366.  
  367. }
  368. //==
  369. import javax.swing.*;
  370. import java.awt.*;
  371. import java.awt.event.*;
  372.  
  373.  
  374.  
  375. public class Canvas extends JFrame
  376. {
  377.     JTextField textA;
  378.     JTextField textB;
  379.     JButton buttonDo;
  380.     JPanel Panel;
  381.     JFrame window;
  382.  
  383.     CustomPanel Paint = new CustomPanel();
  384.    
  385.     public Canvas(String title){
  386.         window = new JFrame();
  387.         window.setSize(400,400);
  388.         window.setLayout(new FlowLayout());
  389.        
  390.         buttonDo = new JButton("Afiseaza");
  391.         Panel = new JPanel();
  392.         Panel.setBackground(Color.BLUE);
  393.         textA = new JTextField(10);
  394.         textB = new JTextField(10);
  395.         buttonDo.addActionListener(new ActionListener() {
  396.             @Override
  397.             public void actionPerformed(ActionEvent e) {
  398.                 float t1 = Float.parseFloat(textA.getText());
  399.                 float t2 = Float.parseFloat(textB.getText());
  400.                 int suma = Math.round(t1+t2);
  401.                 Paint.setValues(suma, window.getWidth(), window.getHeight());
  402.                 Paint.paintComponent(window.getGraphics());
  403.  
  404.             }
  405.         });
  406.        
  407.         Panel.add(textA);
  408.         Panel.add(textB);
  409.         Panel.add(buttonDo);
  410.         window.add(Panel);
  411.         window.getContentPane().add(Paint);
  412.         //window.add(Paint);
  413.                
  414.         window.setVisible(true);
  415.         window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  416.        
  417.     }
  418. }
  419. //==
  420. import javax.swing.*;
  421. import java.awt.*;
  422. import java.util.Random;
  423.  
  424.  
  425. public class CustomPanel extends JPanel
  426. {
  427.     Random rnR = new Random();
  428.     Random rnG = new Random();
  429.     Random rnB = new Random();
  430.     Random radius = new Random();
  431.     Random rX = new Random();
  432.     Random rY = new Random();
  433.    
  434.     int suma;
  435.     int w;
  436.     int h;
  437.     int r;
  438.     int x;
  439.     int y;
  440.      
  441.     int R,G,B;
  442.    
  443.     CustomPanel(){
  444.  
  445.        
  446.     }
  447.     public String c = " ";
  448.  
  449.     public void setValues(int suma, int w, int h)
  450.     {
  451.         this.suma = suma;
  452.         this.w =  w;  
  453.         this.h =  h;
  454.     }
  455.    
  456.     public void paintComponent(Graphics g)
  457.     {
  458.         for(int i=0; i<suma ; i++) {
  459.         R =  rnR.nextInt(255);
  460.         G =  rnG.nextInt(255);
  461.         B =  rnB.nextInt(255);
  462.         r =  radius.nextInt((100-50)+1)+50;
  463.         x =  rX.nextInt((w - r) + 1);
  464.         y =  rY.nextInt((h - r) + 1);
  465.        
  466.         g.setColor(new Color(R,G,B));
  467.         //g.drawOval(x, y, r, r);
  468.         g.fillOval(x, y, r, r);
  469.         }
  470.                
  471.     }
  472. }
  473. //==============================================================================
  474.  
  475. import java.util.List;
  476. import java.io.BufferedReader;
  477. import java.io.FileReader;
  478. import java.io.FileWriter;
  479. import java.io.IOException;
  480. import java.util.ArrayList;
  481. import java.util.Arrays;
  482.  
  483. /*
  484. =================================P3=========================================
  485. Author      : Dobie Flaviu
  486. Version     : 1.0
  487. Copyright   : Created by Flaviu Dobie
  488. Description : LAB_10
  489. Problema    : 3
  490. ============================================================================
  491.  
  492. 3.  Se dă un fiÈ™ier *.csv ce conÈ›ine următoarele câmpuri separate prin simbolul
  493.     /: nume, prenume, număr de telefon, data naÈ™terii, link la profilul de Facebook.
  494.     Să se citească informaÈ›ia din fiÈ™ier È™i să se genereze noi fiÈ™iere (individuale)
  495.     ce conÈ›in doar persoanele cu următoarele caracteristici: persoanele născute în
  496.     luna decembrie, persoane ale căror numere de telefon sunt externe României sau
  497.     au număr de telefon fix, persoane cu numele Andrei sau Nicolae È™i persoane ale căror
  498.     link-uri de la profilul de Facebook nu au fost customizate (conÈ›in un È™ir
  499.     aleator de numere la finalul acestuia).
  500.     Am folosit caracterul , pt despartire in csv!
  501.  */
  502. public class L10_P3 {
  503.     public static void main(String[] args) {
  504.         List<User> selectedUser;
  505.         WriteCSV w = new WriteCSV();
  506.         ReadCSV r = new ReadCSV();
  507.         selectedUser = r.readCsv();
  508.         if(selectedUser!= null) {
  509.             w.writeCSVSelected(selectedUser);
  510.         }
  511.     }
  512. }
  513. //nume, prenume, număr de telefon, data nașterii, link la profilul de Facebook.
  514. class User {
  515.     private String nume;
  516.     private String prenume;
  517.     private String telefon;
  518.     private String dataN;
  519.     private String facebook;
  520.    
  521.     public User() {
  522.     }
  523.  
  524.     public User(String nume, String prenume, String telefon, String dataN,
  525.             String facebook) {
  526.         this.nume = nume;
  527.         this.prenume = prenume;
  528.         this.dataN = dataN;
  529.         this.telefon = telefon;
  530.         this.facebook = facebook;
  531.     }
  532.  
  533.     public String getNume() {
  534.         return nume;
  535.     }
  536.  
  537.     public void setNume(String nume) {
  538.         this.nume = nume;
  539.     }
  540.     public String getPrenume() {
  541.         return prenume;
  542.     }
  543.  
  544.     public void setPrenume(String prenume) {
  545.         this.prenume = prenume;
  546.     }
  547.  
  548.     public String getTelefon() {
  549.         return telefon;
  550.     }
  551.  
  552.     public void setTelefon(String telefon) {
  553.         this.telefon = telefon;
  554.     }
  555.    
  556.     public String getDataN() {
  557.         return dataN;
  558.     }
  559.  
  560.     public void setDataN(String dataN) {
  561.         this.dataN = dataN;
  562.     }
  563.     public String getFacebook() {
  564.         return facebook;
  565.     }
  566.  
  567.     public void setFacebook(String facebook) {
  568.         this.facebook = facebook;
  569.     }
  570.  
  571.  
  572.     @Override
  573.     public String toString() {
  574.         return "User: " +nume + ", " + prenume + ", " + telefon + ", " + dataN + ", "+ facebook;
  575.     }
  576. }
  577.  
  578. class WriteCSV {
  579.      
  580.     private static final String CSV_HEADER = "Nume, Prenume, Telefon, Data nasterii, Facebook";
  581.  
  582.     WriteCSV() {
  583.  
  584. List<User> u = Arrays.asList(
  585.                 new User("Dobie", "Flaviu", "0755778333", "10/12/1996","a"),
  586.                 new User("Cepuc", "Artur", "+393804348283", "1/1/1997","a"),
  587.                 new User("Leahu", "Marius", "07657435633", "10/1/1997","a"),
  588.                 new User("Dragnea", "Nicolae", "0755778333", "17/10/1960","a"),
  589.                 new User("Sfantu", "Andrei", "+393804348283", "1/5/1997","nu"),
  590.                 new User("Dancila", "Veorica", "02616666666", "10/1/1910","a")
  591.                 );
  592.  
  593. FileWriter fileWriter = null;
  594.  
  595.         try {
  596.             fileWriter = new FileWriter("L10_P3.csv");
  597.  
  598.             fileWriter.append(CSV_HEADER);
  599.             fileWriter.append('\n');
  600.  
  601.             for (User user : u) {
  602.                 fileWriter.append(user.getNume());
  603.                 fileWriter.append(',');
  604.                 fileWriter.append(user.getPrenume());
  605.                 fileWriter.append(',');
  606.                 fileWriter.append(user.getTelefon());
  607.                 fileWriter.append(',');
  608.                 fileWriter.append(user.getDataN());
  609.                 fileWriter.append(',');
  610.                 fileWriter.append(user.getFacebook());
  611.                 fileWriter.append('\n');
  612.             }
  613.  
  614.             System.out.println("Write CSV successfully!");
  615.         } catch (Exception e) {
  616.             System.out.println("Writing CSV error!");
  617.             e.printStackTrace();
  618.         } finally {
  619.             try {
  620.                 fileWriter.flush();
  621.                 fileWriter.close();
  622.             } catch (IOException e) {
  623.                 System.out.println("Flushing/closing error!");
  624.                 e.printStackTrace();
  625.             }
  626.         }
  627.     }
  628.     public void writeCSVSelected(List<User> u) {
  629.          
  630.         FileWriter fileWriter = null;
  631.  
  632.         try {
  633.             fileWriter = new FileWriter("L10_P3_select.csv");
  634.  
  635.             fileWriter.append(CSV_HEADER);
  636.             fileWriter.append('\n');
  637.  
  638.             for (User user : u) {
  639.                 fileWriter.append(user.getNume());
  640.                 fileWriter.append(',');
  641.                 fileWriter.append(user.getPrenume());
  642.                 fileWriter.append(',');
  643.                 fileWriter.append(user.getTelefon());
  644.                 fileWriter.append(',');
  645.                 fileWriter.append(user.getDataN());
  646.                 fileWriter.append(',');
  647.                 fileWriter.append(user.getFacebook());
  648.                 fileWriter.append(',');
  649.                 fileWriter.append('\n');
  650.             }
  651.  
  652.             System.out.println("Write CSV successfully!");
  653.         } catch (Exception e) {
  654.             System.out.println("Writing CSV error!");
  655.             e.printStackTrace();
  656.         } finally {
  657.             try {
  658.                 fileWriter.flush();
  659.                 fileWriter.close();
  660.             } catch (IOException e) {
  661.                 System.out.println("Flushing/closing error!");
  662.                 e.printStackTrace();
  663.             }
  664.         }
  665.     }
  666. }
  667.  
  668. class ReadCSV {
  669.  
  670.     ReadCSV(){}
  671.     public List<User> readCsv() {
  672.         BufferedReader fileReader = null;
  673.         try {
  674.             List<User> u = new ArrayList<User>();
  675.             List<User> selectedUser = new ArrayList<User>();
  676.             String line = "";
  677.             fileReader = new BufferedReader(new FileReader("L10_P3.csv"));
  678.  
  679.             // Read CSV header
  680.             fileReader.readLine();
  681.  
  682.             // Read customer data line by line
  683.             while ((line = fileReader.readLine()) != null) {
  684.                 String[] tokens = line.split(",");
  685.                 if (tokens.length > 0) {
  686.                     User user = new User(
  687.                             tokens[0],
  688.                             tokens[1],
  689.                             tokens[2],
  690.                             tokens[3],
  691.                             tokens[4]);
  692.                    
  693.     u.add(user);
  694.     String [] data = tokens[3].split("/");
  695.  
  696.     if(tokens[1].compareToIgnoreCase("Andrei")== 0 ||tokens[1].compareToIgnoreCase("Nicolae")== 0 ) {
  697.             selectedUser.add(user);
  698.     }
  699.     else if(tokens[2].charAt(0)=='+' && tokens[2].charAt(1)!='4' && tokens[3].charAt(1)!='0') {
  700.             selectedUser.add(user);
  701.     }
  702.     else if(tokens[2].charAt(0)=='0' && tokens[2].charAt(1)=='0' && tokens[3].charAt(1)!='4') {
  703.             selectedUser.add(user);
  704.     }
  705.     else if(tokens[2].charAt(0)=='0' && ((int)(tokens[2].charAt(1))<7)){
  706.             selectedUser.add(user);
  707.     }
  708.     else if(tokens[4].length()>100){
  709.             selectedUser.add(user);
  710.     }
  711.     else if(data[1].compareTo("12")==0) {
  712.             selectedUser.add(user);
  713.     }
  714.                 }
  715.             }
  716.             if(!selectedUser.isEmpty()) {
  717.                 return selectedUser;
  718.             }
  719.  
  720.             for (User user : u) {
  721.                 System.out.println(user);
  722.             }
  723.  
  724.         } catch (Exception e) {
  725.             System.out.println("Reading CSV Error!");
  726.             e.printStackTrace();
  727.         } finally {
  728.             try {
  729.                 fileReader.close();
  730.             } catch (IOException e) {
  731.                 System.out.println("Closing fileReader Error!");
  732.                 e.printStackTrace();
  733.             }
  734.         }
  735.         return null;
  736.     }
  737. }
  738. //==============================================================================
  739. //BONUS
  740.  
  741. // Un  Cerc in mijloc, slidebar pt x si y
  742.  
  743.  
  744. public class L9_Bonus {
  745.  
  746.     public static void main(String[] args)
  747.     {
  748.         Sample2Frame frame = new Sample2Frame();
  749.         frame.setVisible(true);
  750.     }
  751.  
  752. }
  753.  
  754.  
  755.  
  756. import java.awt.BorderLayout;
  757.  
  758. import java.awt.event.ActionEvent;
  759. import java.awt.event.ActionListener;
  760.  
  761. import javax.swing.JFrame;
  762.  
  763. import javax.swing.JPanel;
  764. import javax.swing.JSlider;
  765. import javax.swing.event.ChangeEvent;
  766. import javax.swing.event.ChangeListener;
  767.  
  768. import java.awt.Color;
  769. import java.awt.Graphics;
  770.  
  771. class Sample2Frame extends JFrame {
  772.    
  773.     private JPanel jPanel1;
  774.     private JSlider SliderX;
  775.     private JSlider SliderY;
  776.    
  777.     private Sample2Panel jPanel2;
  778.    
  779.     private int r = 0;
  780.     private int g = 0;
  781.     private int b = 0;
  782.     private float x = 0;
  783.     private float y = 0;
  784.  
  785.     public Sample2Frame()
  786.     {
  787.         super();
  788.         initGUI();
  789.     }
  790.    
  791.     private void initGUI()
  792.     {
  793.         try
  794.         {
  795.            
  796.             setLayout(new BorderLayout());
  797.                
  798.                
  799.             jPanel1 = new JPanel();
  800.             this.getContentPane().add(jPanel1, BorderLayout.SOUTH);
  801.             jPanel1.setPreferredSize(new java.awt.Dimension(100, 100));
  802.             jPanel1.setLocation(200, 200);
  803.            
  804.             SliderX = new JSlider(JSlider.HORIZONTAL,0,100,50);
  805.             SliderX.setPaintLabels(true);
  806.             SliderX.setMinorTickSpacing(5);
  807.             SliderX.setMajorTickSpacing(10);
  808.             SliderX.setPaintTicks(true);
  809.             SliderY = new JSlider(JSlider.HORIZONTAL,0,100,50);
  810.             SliderY.setPaintLabels(true);
  811.             SliderY.setMinorTickSpacing(5);
  812.             SliderY.setMajorTickSpacing(10);
  813.             SliderY.setPaintTicks(true);
  814.            
  815.             SliderX.addChangeListener(new ChangeListener()
  816.             {
  817.                 @Override
  818.                 public void stateChanged(ChangeEvent ce)
  819.                 {
  820.                     x = SliderX.getValue();
  821.                     redeseneaza();
  822.                 }
  823.  
  824.             });
  825.             SliderY.addChangeListener(new ChangeListener()
  826.             {
  827.                 @Override
  828.                 public void stateChanged(ChangeEvent ce)
  829.                 {
  830.                     y = SliderY.getValue();
  831.                     redeseneaza();
  832.                 }
  833.  
  834.             });
  835.             jPanel1.add(SliderX);
  836.             jPanel1.add(SliderY);
  837.            
  838.              
  839.                
  840.             jPanel2 = new Sample2Panel();
  841.             this.getContentPane().add(jPanel2, BorderLayout.CENTER);
  842.             jPanel2.setPreferredSize(new java.awt.Dimension(200, 200));
  843.                
  844.             jPanel2.seteazaCuloare(10, 10, 10);
  845.             jPanel2.repaint();
  846.            
  847.             setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        
  848.             pack();
  849.             setSize(400, 400);
  850.        
  851.         }
  852.         catch (Exception e)
  853.         {
  854.             e.printStackTrace();    
  855.         }
  856.     }
  857.  void redeseneaza()
  858.     {
  859.          
  860.         jPanel2.set_x((float)x);
  861.         jPanel2.set_y((float)y);
  862.         jPanel2.repaint();
  863.     }
  864. }
  865.  
  866.  
  867. import javax.swing.JPanel;
  868.  
  869. import java.awt.Color;
  870. import java.awt.Graphics;
  871.  
  872. class Sample2Panel extends JPanel
  873. {
  874.    
  875.     private int albastru;
  876.     private int verde;
  877.     private int rosu;
  878.     private float x = 1;
  879.     private float y = 1;
  880.    
  881.     public void seteazaCuloare(int rosu, int verde, int albastru){
  882.         this.rosu = rosu;
  883.         this.verde = verde;
  884.         this.albastru = albastru;
  885.     }
  886.    
  887.     public void set_x(float x)
  888.     {
  889.         this.x = x;
  890.     }
  891.     public void set_y(float y)
  892.     {
  893.         this.y = y;
  894.     }
  895.    
  896.     public void paint(Graphics g)
  897.     {
  898.         super.paint(g);
  899.        
  900.         g.setColor(new Color(rosu, verde, albastru));        
  901.        
  902.         g.fillOval(getWidth()/2-50 + (int)x , getWidth()/2-50 + (int)y, 50, 50);
  903.        }
  904. }
  905.  
  906. //==============================================================================
  907. // Test 5
  908.  
  909. public class Test_P5 {
  910.     public static void main(String[] args) throws Exception
  911.     {      
  912.         Canvas ob1 = new Canvas("Compare ComboBox");
  913.  
  914.     }
  915.  
  916. }
  917. //////////
  918. import javax.swing.*;
  919. import java.awt.*;
  920. import java.awt.event.*;
  921. import java.io.IOException;
  922.  
  923.  
  924.  
  925. public class Canvas extends JFrame
  926. {
  927.     JTextField textA;
  928.     JTextField textB;
  929.     JButton buttonDo;
  930.     JPanel Panel;
  931.     JFrame window;
  932.    
  933.     InputOutput io = new InputOutput();
  934.     CustomPanel Paint = new CustomPanel();
  935.    
  936.     public Canvas(String title){
  937.        
  938.         window = new JFrame();
  939.         window.setSize(400,400);
  940.         window.setLayout(new FlowLayout());
  941.        
  942.         buttonDo = new JButton("Afiseaza");
  943.         Panel = new JPanel();
  944.         Panel.setBackground(Color.BLUE);
  945.         textA = new JTextField(10);
  946.         textB = new JTextField(10);
  947.        
  948.        
  949.         Panel.add(textA);
  950.         Panel.add(textB);
  951.         window.add(Panel);
  952.         window.getContentPane().add(Paint);
  953.         window.addMouseListener(new MouseAdapter() {
  954.             public void mousePressed(MouseEvent me) {
  955.               try {
  956.                 io.writeSmallBinaryFile(textA.getText().getBytes(),"binFile.bin");
  957.             } catch (IOException e) {
  958.                 // TODO Auto-generated catch block
  959.                 e.printStackTrace();
  960.             }
  961.              
  962.               try {
  963.                 io.readSmallBinaryFile("binFile.bin");
  964.             } catch (IOException e) {
  965.                 // TODO Auto-generated catch block
  966.                 e.printStackTrace();
  967.             }
  968.             }
  969.           });
  970.         //window.add(Paint);
  971.                
  972.         window.setVisible(true);
  973.         window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  974.        
  975.     }
  976. }
  977. ///
  978.  
  979. import java.io .* ;
  980. import java.nio.file.Files;
  981. import java.nio.file.Path;
  982. import java.nio.file.Paths;
  983. public class InputOutput {
  984.    
  985.  
  986.     void readSmallBinaryFile(String fileName) throws IOException {
  987.        
  988.         Path path = Paths.get(fileName);
  989.         System.out.println(Files.readAllBytes(path));
  990.     }
  991.      
  992.     void writeSmallBinaryFile(byte[] bytes, String fileName) throws IOException {
  993.         Path path = Paths.get(fileName);
  994.         Files.write(path, bytes); //creates, overwrites
  995.       }
  996. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement