Advertisement
thieumao

Vẽ từng điểm từ file

Jun 20th, 2016
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.54 KB | None | 0 0
  1. package pixelcanvas;
  2.  
  3. import java.awt.Canvas;
  4. import java.awt.Color;
  5. import java.awt.Graphics;
  6. import java.io.BufferedReader;
  7. import java.io.FileReader;
  8. import java.io.IOException;
  9. import java.util.Random;
  10.  
  11. import javax.swing.JFrame;
  12.  
  13. public class PixelCanvas extends Canvas {
  14.     private static final int WIDTH = 800;
  15.     private static final int HEIGHT = 20000;
  16.     private static final Random random = new Random();
  17.  
  18.     @Override
  19.     public void paint(Graphics g) {
  20.         super.paint(g);
  21.  
  22.         try {
  23.             String sCurrentLine;
  24.             String temp;
  25.             // sua lai duong dai file pairs theo nhu tren may tinh cua ban
  26.             BufferedReader br = new BufferedReader(new FileReader("/Users/thieumao/Desktop/pairs"));
  27.             while ((sCurrentLine = br.readLine()) != null) {
  28. //                System.out.println(sCurrentLine);
  29.                 String[] array = sCurrentLine.split(" ");
  30.                 System.out.println("x="+array[0]+"y="+array[1]);
  31.                 int x = Integer.parseInt(array[0]);
  32.                 int y = Integer.parseInt(array[1]);
  33.                 g.setColor(oneColor());
  34.                 g.drawLine(x, y, x, y);
  35.             }
  36.         } catch (IOException e) {
  37.             e.printStackTrace();
  38.         }
  39.     }
  40.  
  41.     private Color oneColor() {
  42.         return new Color(0, 0, 0);
  43.     }
  44.  
  45.     public static void main(String[] args) {
  46.         JFrame frame = new JFrame();
  47.         frame.setSize(WIDTH, HEIGHT);
  48.         frame.add(new PixelCanvas());
  49.         frame.setVisible(true);
  50.     }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement