Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package odcinek_i_wypelnienie;
- import java.awt.Color;
- import java.awt.Graphics;
- import java.awt.Graphics2D;
- import java.awt.Point;
- import java.awt.event.MouseEvent;
- import java.awt.event.MouseListener;
- import java.awt.event.MouseMotionListener;
- import java.awt.image.BufferedImage;
- import java.io.File;
- import java.io.IOException;
- import java.util.LinkedList;
- import java.util.Queue;
- import javax.imageio.ImageIO;
- import javax.swing.JPanel;
- /**
- *
- * @author Mateusz
- */
- public class myPanel extends JPanel{
- private BufferedImage img;
- private Point p1, p2;
- private tryb tryb_rysowania = tryb.LINE;
- private Color color;
- public myPanel(){
- super();
- p1 = new Point();
- p2 = new Point();
- color = new Color(Color.BLACK.getRGB());
- this.init_points();
- this.setBackground(Color.GRAY);
- img = new BufferedImage(600, 550, BufferedImage.TYPE_INT_RGB);
- for(int x=0; x < img.getWidth(); x++)
- for(int y=0; y < img.getHeight(); y++){
- img.setRGB(x, y, Color.WHITE.getRGB());
- }
- addMouseMotionListener(new MouseMotionListener() {
- @Override
- public void mouseDragged(MouseEvent e) {
- }
- @Override
- public void mouseMoved(MouseEvent e) {
- Global.mouse_X.setText(e.getX() + "");
- Global.mouse_Y.setText(e.getY() + "");
- Global.mouse_X_value = e.getX();
- Global.mouse_Y_value = e.getY();
- repaint();
- }
- });
- addMouseListener(new MouseListener() {
- @Override
- public void mouseClicked(MouseEvent e) {}
- @Override
- public void mouseReleased(MouseEvent e) {}
- @Override
- public void mouseEntered(MouseEvent e) {}
- @Override
- public void mouseExited(MouseEvent e) {}
- @Override
- public void mousePressed(MouseEvent e) {
- if(tryb_rysowania == tryb.LINE){
- if(p1.x == -1)
- p1.setLocation(e.getX(), e.getY());
- else
- p2.setLocation(e.getX(), e.getY());
- try_draw_line();
- }
- else{
- p1.setLocation(e.getX(), e.getY());
- fill(p1);
- //floodFill(p1, new Color(img.getRGB(p1.x, p1.y)), color);
- init_points();
- }
- }
- });
- }
- @Override
- protected void paintComponent(Graphics g1){
- Graphics2D g = (Graphics2D)g1;
- g.drawImage(img, null, 0, 0);
- draw_line_live(g, Global.mouse_X_value, Global.mouse_Y_value);
- }
- private void init_points(){
- this.p1.setLocation(-1, -1);
- this.p2.setLocation(-1, -1);
- }
- private void try_draw_line(){
- if(p1.x == -1 || p2.x == -1)
- return;
- //line(p1.x, p1.y, p2.x, p2.y, color);
- line();
- init_points();
- repaint();
- }
- private void line(int x0, int y0, int x1, int y1, Color c) {
- int dx = Math.abs(x1 - x0);
- int dy = Math.abs(y1 - y0);
- int sx, sy, err, e2;
- if (x0 < x1)
- sx = 1;
- else
- sx = -1;
- if (y0 < y1)
- sy = 1;
- else
- sy = -1;
- err = dx - dy;
- while (true) {
- img.setRGB(x0, y0, c.getRGB());
- if (x0 == x1 && y0 == y1)
- break;
- e2 = 2 * err;
- if (e2 > -dy) {
- err = err - dy;
- x0 = x0 + sx;
- }
- if (e2 < dx) {
- err = err + dx;
- y0 = y0 + sy;
- }
- }
- }
- private void line(){//int x0, int y0, int x1, int y1, Color c) {
- int dx = Math.abs(p2.x - p1.x);
- int dy = Math.abs(p2.y - p1.y);
- int sx = -1, sy = -1, err, e2;
- if (p1.x < p2.x)
- sx = 1;
- // else
- // sx = -1;
- if (p1.y < p2.y)
- sy = 1;
- // else
- // sy = -1;
- err = dx - dy;
- while (true){
- img.setRGB(p1.x, p1.y, color.getRGB());
- if (p1.x == p2.x && p1.y == p2.y)
- return;//break;
- e2 = 2 * err;
- if (e2 > -dy){
- err = err - dy;
- p1.x += sx;
- }
- if (e2 < dx){
- err = err + dx;
- p1.y += sy;
- }
- }
- }
- public void set_tryb_rysowania(tryb a){
- this.tryb_rysowania = a;
- }
- public void set_color(Color c){
- this.color = c;
- }
- // target to wybranego pixela a replacement mam w zmiennej color
- private void fill(Point p1){ //kolor mam trzymany w zmiennej w panelu
- Color p1_color = new Color(img.getRGB(p1.x, p1.y));
- if(color.getRGB() == p1_color.getRGB())
- return; //jesli docelowy taki sam jak aktualny w tym punkcie
- Queue<Point> kolejka = new LinkedList<Point>();
- Point tmp;
- kolejka.add(p1);//new Point(p1));
- while(!kolejka.isEmpty()){
- tmp = kolejka.poll();
- if(img.getRGB(tmp.x, tmp.y) == p1_color.getRGB()){//img.getRGB(p1.x, p1.y)){
- img.setRGB(tmp.x, tmp.y, color.getRGB());
- if(tmp.x > 0)
- kolejka.add(new Point(tmp.x - 1, tmp.y));
- if(tmp.x < img.getWidth() - 1)
- kolejka.add(new Point(tmp.x + 1, tmp.y));
- if(tmp.y < img.getHeight() - 1)
- kolejka.add(new Point(tmp.x, tmp.y + 1));
- if(tmp.y > 0)
- kolejka.add(new Point(tmp.x, tmp.y - 1));
- }
- //System.out.println(kolejka.size());
- }
- //System.out.println("---");
- repaint();
- }
- private void draw_line_live(Graphics2D g, int x, int y){ // jeden pkt mam jako pole drugi przekazywany w evencie
- if(p1.x == -1 || tryb_rysowania == tryb.FILL)
- return;
- g.setColor(color);
- g.drawLine(p1.x, p1.y, x, y);
- }
- public void clear_all(){
- for(int x=0; x < img.getWidth(); x++)
- for(int y=0; y < img.getHeight(); y++){
- img.setRGB(x, y, Color.WHITE.getRGB());
- }
- init_points();
- //color = Color.BLACK;
- repaint();
- }
- public void save_to_jpg(File file) throws IOException{
- ImageIO.write(this.img, "jpg", file);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement