Advertisement
Guest User

Untitled

a guest
Apr 24th, 2014
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.68 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6.  
  7. package odcinek_i_wypelnienie;
  8.  
  9. import java.awt.Color;
  10. import java.awt.Graphics;
  11. import java.awt.Graphics2D;
  12. import java.awt.Point;
  13. import java.awt.event.MouseEvent;
  14. import java.awt.event.MouseListener;
  15. import java.awt.event.MouseMotionListener;
  16. import java.awt.image.BufferedImage;
  17. import java.io.File;
  18. import java.io.IOException;
  19. import java.util.LinkedList;
  20. import java.util.Queue;
  21. import javax.imageio.ImageIO;
  22. import javax.swing.JPanel;
  23.  
  24. /**
  25.  *
  26.  * @author Mateusz
  27.  */
  28. public class myPanel extends JPanel{
  29.     private BufferedImage img;
  30.     private Point p1, p2;
  31.     private tryb tryb_rysowania = tryb.LINE;
  32.     private Color color;
  33.     public myPanel(){
  34.         super();
  35.         p1 = new Point();
  36.         p2 = new Point();
  37.         color = new Color(Color.BLACK.getRGB());
  38.         this.init_points();
  39.        
  40.         this.setBackground(Color.GRAY);
  41.         img = new BufferedImage(600, 550, BufferedImage.TYPE_INT_RGB);
  42.        
  43.         for(int x=0; x < img.getWidth(); x++)
  44.             for(int y=0; y < img.getHeight(); y++){
  45.                 img.setRGB(x, y, Color.WHITE.getRGB());
  46.             }
  47.        
  48.         addMouseMotionListener(new MouseMotionListener() {
  49.  
  50.             @Override
  51.             public void mouseDragged(MouseEvent e) {
  52.          
  53.             }
  54.  
  55.             @Override
  56.             public void mouseMoved(MouseEvent e) {
  57.                 Global.mouse_X.setText(e.getX() + "");
  58.                 Global.mouse_Y.setText(e.getY() + "");
  59.                
  60.                 Global.mouse_X_value = e.getX();
  61.                 Global.mouse_Y_value = e.getY();
  62.                 repaint();
  63.             }
  64.         });
  65.        
  66.         addMouseListener(new MouseListener() {
  67.  
  68.             @Override
  69.             public void mouseClicked(MouseEvent e) {}
  70.  
  71.             @Override
  72.             public void mouseReleased(MouseEvent e) {}
  73.  
  74.             @Override
  75.             public void mouseEntered(MouseEvent e) {}
  76.  
  77.             @Override
  78.             public void mouseExited(MouseEvent e) {}
  79.            
  80.             @Override
  81.             public void mousePressed(MouseEvent e) {
  82.                 if(tryb_rysowania == tryb.LINE){
  83.                     if(p1.x == -1)
  84.                         p1.setLocation(e.getX(), e.getY());
  85.                     else
  86.                         p2.setLocation(e.getX(), e.getY());
  87.                     try_draw_line();
  88.                 }
  89.                 else{
  90.                     p1.setLocation(e.getX(), e.getY());
  91.                     fill(p1);
  92.                     //floodFill(p1, new Color(img.getRGB(p1.x, p1.y)), color);
  93.                     init_points();
  94.                 }
  95.             }
  96.  
  97.         });
  98.     }
  99.    
  100.     @Override
  101.     protected void paintComponent(Graphics g1){
  102.         Graphics2D g = (Graphics2D)g1;
  103.         g.drawImage(img, null, 0, 0);
  104.         draw_line_live(g, Global.mouse_X_value, Global.mouse_Y_value);
  105.     }
  106.    
  107.     private void init_points(){
  108.         this.p1.setLocation(-1, -1);
  109.         this.p2.setLocation(-1, -1);
  110.     }
  111.    
  112.     private void try_draw_line(){
  113.         if(p1.x == -1 || p2.x == -1)
  114.             return;
  115.        
  116.         //line(p1.x, p1.y, p2.x, p2.y, color);
  117.         line();
  118.         init_points();
  119.         repaint();
  120.     }
  121.    
  122.     private void line(int x0, int y0, int x1, int y1, Color c) {
  123.         int dx = Math.abs(x1 - x0);
  124.         int dy = Math.abs(y1 - y0);
  125.         int sx, sy, err, e2;
  126.         if (x0 < x1)
  127.             sx = 1;
  128.         else
  129.             sx = -1;
  130.         if (y0 < y1)
  131.             sy = 1;
  132.         else
  133.             sy = -1;
  134.         err = dx - dy;
  135.         while (true) {
  136.             img.setRGB(x0, y0, c.getRGB());
  137.             if (x0 == x1 && y0 == y1)
  138.                 break;
  139.             e2 = 2 * err;
  140.             if (e2 > -dy) {
  141.                 err = err - dy;
  142.                 x0 = x0 + sx;
  143.             }
  144.             if (e2 < dx) {
  145.                 err = err + dx;
  146.                 y0 = y0 + sy;
  147.             }
  148.         }
  149.     }
  150.    
  151.      private void line(){//int x0, int y0, int x1, int y1, Color c) {
  152.         int dx = Math.abs(p2.x - p1.x);
  153.     int dy = Math.abs(p2.y - p1.y);
  154.     int sx = -1, sy = -1, err, e2;
  155.     if (p1.x < p2.x)
  156.             sx = 1;
  157. //      else
  158. //          sx = -1;
  159.     if (p1.y < p2.y)
  160.             sy = 1;
  161. //      else
  162. //          sy = -1;
  163.     err = dx - dy;
  164.     while (true){
  165.             img.setRGB(p1.x, p1.y, color.getRGB());
  166.             if (p1.x == p2.x && p1.y == p2.y)
  167.         return;//break;
  168.             e2 = 2 * err;
  169.             if (e2 > -dy){
  170.         err = err - dy;
  171.         p1.x += sx;
  172.             }
  173.             if (e2 < dx){
  174.         err = err + dx;
  175.         p1.y += sy;
  176.             }
  177.     }
  178.     }
  179.    
  180.     public void set_tryb_rysowania(tryb a){
  181.         this.tryb_rysowania = a;
  182.     }
  183.    
  184.     public void set_color(Color c){
  185.         this.color = c;
  186.     }
  187.     // target to wybranego pixela a replacement mam w zmiennej color
  188.     private void fill(Point p1){    //kolor mam trzymany w zmiennej w panelu
  189.         Color p1_color = new Color(img.getRGB(p1.x, p1.y));
  190.         if(color.getRGB() == p1_color.getRGB())
  191.             return; //jesli docelowy taki sam jak aktualny w tym punkcie
  192.        
  193.         Queue<Point> kolejka = new LinkedList<Point>();
  194.         Point tmp;
  195.         kolejka.add(p1);//new Point(p1));
  196.        
  197.         while(!kolejka.isEmpty()){
  198.             tmp = kolejka.poll();
  199.            
  200.             if(img.getRGB(tmp.x, tmp.y) == p1_color.getRGB()){//img.getRGB(p1.x, p1.y)){
  201.                 img.setRGB(tmp.x, tmp.y, color.getRGB());
  202.                
  203.                 if(tmp.x > 0)
  204.                     kolejka.add(new Point(tmp.x - 1, tmp.y));
  205.                 if(tmp.x < img.getWidth() - 1)
  206.                     kolejka.add(new Point(tmp.x + 1, tmp.y));
  207.                 if(tmp.y < img.getHeight() - 1)
  208.                     kolejka.add(new Point(tmp.x, tmp.y + 1));
  209.                 if(tmp.y > 0)
  210.                     kolejka.add(new Point(tmp.x, tmp.y - 1));
  211.             }
  212.             //System.out.println(kolejka.size());
  213.         }
  214.         //System.out.println("---");
  215.         repaint();
  216.     }
  217.    
  218.     private void draw_line_live(Graphics2D g, int x, int y){ // jeden pkt mam jako pole drugi przekazywany w evencie
  219.         if(p1.x == -1 || tryb_rysowania == tryb.FILL)
  220.             return;
  221.         g.setColor(color);
  222.         g.drawLine(p1.x, p1.y, x, y);
  223.     }
  224.    
  225.     public void clear_all(){
  226.         for(int x=0; x < img.getWidth(); x++)
  227.             for(int y=0; y < img.getHeight(); y++){
  228.                 img.setRGB(x, y, Color.WHITE.getRGB());
  229.             }
  230.        
  231.         init_points();
  232.        
  233.         //color = Color.BLACK;
  234.        
  235.         repaint();
  236.     }
  237.    
  238.     public void save_to_jpg(File file) throws IOException{
  239.         ImageIO.write(this.img, "jpg", file);
  240.     }
  241. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement