Advertisement
PaweU

javalab11

Dec 17th, 2019
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.90 KB | None | 0 0
  1. import java.io.FileNotFoundException;
  2. import java.io.FileOutputStream;
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.net.URL;
  6. import java.util.Locale;
  7.  
  8. public class DownloadExample {
  9.  
  10. static int count = 0;
  11.  
  12. // lista plików do pobrania
  13. static String [] toDownload = {
  14. "http://home.agh.edu.pl/pszwed/wyklad-c/01-jezyk-c-intro.pdf",
  15. "http://home.agh.edu.pl/~pszwed/wyklad-c/02-jezyk-c-podstawy-skladni.pdf",
  16. "http://home.agh.edu.pl/~pszwed/wyklad-c/03-jezyk-c-instrukcje.pdf",
  17. "http://home.agh.edu.pl/~pszwed/wyklad-c/04-jezyk-c-funkcje.pdf",
  18. "http://home.agh.edu.pl/~pszwed/wyklad-c/05-jezyk-c-deklaracje-typy.pdf",
  19. "http://home.agh.edu.pl/~pszwed/wyklad-c/06-jezyk-c-wskazniki.pdf",
  20. "http://home.agh.edu.pl/~pszwed/wyklad-c/07-jezyk-c-operatory.pdf",
  21. "http://home.agh.edu.pl/~pszwed/wyklad-c/08-jezyk-c-lancuchy-znakow.pdf",
  22. "http://home.agh.edu.pl/~pszwed/wyklad-c/09-jezyk-c-struktura-programow.pdf",
  23. "http://home.agh.edu.pl/~pszwed/wyklad-c/10-jezyk-c-dynamiczna-alokacja-pamieci.pdf",
  24. "http://home.agh.edu.pl/~pszwed/wyklad-c/11-jezyk-c-biblioteka-we-wy.pdf",
  25. "http://home.agh.edu.pl/~pszwed/wyklad-c/preprocesor-make-funkcje-biblioteczne.pdf",
  26. };
  27.  
  28. static class Downloader implements Runnable{
  29. private final String url;
  30.  
  31. Downloader(String url){
  32. this.url = url;
  33. }
  34.  
  35. public void run(){
  36. String [] divided = url.split("/");
  37. String fileName = divided[divided.length - 1];
  38.  
  39. try(InputStream in = new URL(url).openStream(); FileOutputStream out = new FileOutputStream(fileName) ){
  40. for(;;){
  41. // czytaj znak z in
  42. // jeśli <0 break
  43. int sign = in.read();
  44. if (sign < 0) break;
  45.  
  46. //zapisz znak do out
  47. out.write((char)sign);
  48. }
  49. count++;
  50. } catch (FileNotFoundException e) {
  51. e.printStackTrace();
  52. } catch (IOException e) {
  53. e.printStackTrace();
  54. }
  55. System.out.println("Done:"+fileName);
  56. }
  57.  
  58. }
  59.  
  60. static void sequentialDownload(){
  61. double t1 = System.nanoTime()/1e6;
  62. for(String url:toDownload){
  63. new Downloader(url).run();
  64. }
  65. double t2 = System.nanoTime()/1e6;
  66. System.out.printf(Locale.US,"t2-t1=%f\n",t2-t1);
  67. }
  68.  
  69. static void concurrentDownload(){
  70. double t1 = System.nanoTime()/1e6;
  71. for(String url:toDownload){
  72. // uruchom Downloader jako wątek...
  73. Runnable runnable = new Downloader(url);
  74. Thread t = new Thread(runnable);
  75. t.start();
  76. }
  77. double t2 = System.nanoTime()/1e6;
  78. System.out.printf(Locale.US,"t2-t1=%f\n",t2-t1);
  79. }
  80.  
  81. static void concurrentDownload2(){
  82. double t1 = System.nanoTime()/1e6;
  83. for(String url:toDownload){
  84. // uruchom Downloader jako wątek...
  85. Runnable runnable = new Downloader(url);
  86. Thread t = new Thread(runnable);
  87. t.start();
  88. }
  89. while(count!=toDownload.length){
  90. // wait...
  91. Thread.yield();
  92. }
  93. double t2 = System.nanoTime()/1e6;
  94. System.out.printf(Locale.US,"t2-t1=%f\n",t2-t1);
  95. }
  96.  
  97.  
  98. public static void main(String[] args) {
  99. //sequentialDownload();
  100. concurrentDownload2();
  101. }
  102. }
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114. /////////////////////////////////////
  115.  
  116.  
  117.  
  118. import javax.swing.*;
  119. import java.awt.geom.Point2D;
  120. import java.time.LocalTime;
  121. import java.awt.*;
  122. import java.awt.geom.AffineTransform;
  123.  
  124. public class ClockWithGui extends JPanel {
  125.  
  126. LocalTime time = LocalTime.now();
  127.  
  128. ClockWithGui(){
  129. ClockWithGui.ClockThread clock = new ClockWithGui.ClockThread();
  130. clock.start();
  131. }
  132.  
  133. class ClockThread extends Thread{
  134.  
  135. @Override
  136. public void run() {
  137. for(;;){
  138. time = LocalTime.now();
  139. System.out.printf("%02d:%02d.%02d \n",time.getHour(),time.getMinute(),time.getSecond());
  140.  
  141. try {
  142. sleep(1000);
  143. } catch (InterruptedException e) {
  144. e.printStackTrace();
  145. }
  146. repaint();
  147. setOpaque(false);
  148. }
  149. }
  150. }
  151.  
  152. public void paintComponent(Graphics g){
  153. Graphics2D g2d=(Graphics2D)g;
  154. g2d.translate(getWidth()/2,getHeight()/2);
  155.  
  156. for(int i=1;i<13;i++){
  157. AffineTransform at = new AffineTransform();
  158. at.rotate(2*Math.PI/12*i);
  159. Point2D src = new Point2D.Float(0,-120);
  160. Point2D trg = new Point2D.Float();
  161. at.transform(src,trg);
  162. g2d.drawString(Integer.toString(i),(int)trg.getX(),(int)trg.getY());
  163. }
  164.  
  165. AffineTransform saveAT = g2d.getTransform();
  166.  
  167. BasicStroke bs = new BasicStroke(2);
  168.  
  169. // wskazowka sekundowa
  170. g2d.rotate(time.getSecond()%60*2*Math.PI/60);
  171. g2d.setStroke(bs);
  172. g2d.setColor(new Color(255,0,0));
  173. g2d.drawLine(0,0,0,-100);
  174. g2d.setTransform(saveAT);
  175.  
  176. g2d.setColor(new Color(0,0,0));
  177.  
  178. // wskazowka minutowa
  179. g2d.rotate(time.getMinute()%60*2*Math.PI/60);
  180. bs = new BasicStroke(5);
  181. g2d.setStroke(bs);
  182. g2d.drawLine(0,0,0,-80);
  183. g2d.setTransform(saveAT);
  184.  
  185. // wskazowka godzinowa
  186. g2d.rotate(time.getHour()%12*2*Math.PI/12);
  187. bs = new BasicStroke(8);
  188. g2d.setStroke(bs);
  189. g2d.drawLine(0,0,0,-50);
  190. g2d.setTransform(saveAT);
  191.  
  192.  
  193. }
  194.  
  195. public static void main(String[] args) {
  196.  
  197. new ClockWithGui();
  198.  
  199. JFrame frame = new JFrame("Clock");
  200. frame.setContentPane(new ClockWithGui());
  201. frame.setSize(700, 700);
  202. frame.setLocationRelativeTo(null);
  203. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  204. frame.setResizable(true);
  205. frame.setVisible(true);
  206.  
  207. }
  208. }
  209.  
  210.  
  211.  
  212.  
  213.  
  214.  
  215.  
  216.  
  217.  
  218. ///////////////////////
  219.  
  220.  
  221.  
  222.  
  223.  
  224.  
  225.  
  226. import java.time.LocalTime;
  227.  
  228. public class Clock extends Thread{
  229.  
  230. @Override
  231. public void run() {
  232. while (true) {
  233. LocalTime time = LocalTime.now();
  234. System.out.printf("%02d:%02d.%02d\n",
  235. time.getHour(),
  236. time.getMinute(),
  237. time.getSecond());
  238. try {
  239. sleep(1000);
  240. } catch (InterruptedException e) {
  241. e.printStackTrace();
  242. }
  243. }
  244. }
  245.  
  246. public static void main(String[] args) {
  247. new Clock().start();
  248. }
  249. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement