tuxmartin

renderer usecka

Oct 11th, 2012
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.97 KB | None | 0 0
  1. package ukol1;
  2.  
  3. import java.awt.Color;
  4. import java.awt.event.*;
  5.  
  6. import fim.utils.Application;
  7. import fim.utils.Console;
  8.  
  9. @SuppressWarnings("serial")
  10. public class Pixel extends Application {
  11.     private int x, y;
  12.    
  13.     private Renderer r = new Renderer(img);
  14.  
  15.     public void start() {
  16.         img = out.getCanvas().getImage();
  17.         out.switchView(Console.VIEW_GRAPH);
  18.         r.drawPixel(10, 20, 0xff0000);
  19.  
  20.         out.getCanvas().addMouseListener(new MouseAdapter() {
  21.             public void mousePressed(MouseEvent ev) {
  22.                 x = ev.getX();
  23.                 y = ev.getY();
  24.             }
  25.  
  26.             public void mouseReleased(MouseEvent ev) {
  27.                 r.drawLine(x, y, ev.getX(), ev.getY(), 0x00FF00);
  28.                 out.repaint();
  29.             }
  30.         });
  31.  
  32.     }
  33.  
  34.     public static void main(String[] args) {
  35.         new Pixel().start();
  36.     }
  37.  
  38. }
  39.  
  40. /* --------------------------------------------------------- */
  41.  
  42. package ukol1;
  43.  
  44. import java.awt.image.BufferedImage;
  45.  
  46. public class Renderer {
  47.     private BufferedImage img;
  48.    
  49.     public Renderer(BufferedImage img) {
  50.         this.img = img;
  51.     }
  52.    
  53.     public void drawPixel(int x, int y, int barva) {
  54.         if (x > 0 && x < img.getWidth()) {
  55.             if (y > 0 && y < img.getHeight()) {
  56.                 img.setRGB(x, y, barva);
  57.             }
  58.         }
  59.     }
  60.  
  61.     public void drawLine(int x1, int y1, int x2, int y2, int barva) {
  62.  
  63.         int x;
  64.         int y;
  65.         float k, q;
  66.  
  67.         if (Math.abs(x2 - x1) > Math.abs(y2 - y1)) {
  68.  
  69.             if (x1 > x2) {
  70.                 int pom = x2;
  71.                 x2 = x1;
  72.                 x1 = pom;
  73.                 pom = y2;
  74.                 y2 = y1;
  75.                 y1 = pom;
  76.             }
  77.  
  78.             int dx = x2 - x1;
  79.             int dy = y2 - y1;
  80.  
  81.             k = (y1 - y2) / (float) (y2 - y1);
  82.             q = y1 - k * x1;
  83.  
  84.             for (x = x1; x <= x2; x++) {
  85.                 y = Math.round(k * x + q);
  86.                 drawPixel(x, y, 0x00FF00);
  87.             }
  88.  
  89.         } else {
  90.            
  91.             if (y1 > y2) {
  92.                 int pom = x2;
  93.                 x2 = x1;
  94.                 x1 = pom;
  95.                 pom = y2;
  96.                 y2 = y1;
  97.                 y1 = pom;
  98.             }          
  99.             k = (float)(x1 - x2) / (y1 - y2);
  100.             q = x2 - k * y2;
  101.            
  102.             for (y = y1; y <= y2; y++) {
  103.                 x = Math.round(k * y + q);     
  104.                 drawPixel(x, y, 0x00FF00);
  105.             }      
  106.         }
  107.  
  108.  
  109.     }
  110.  
  111. }
Advertisement
Add Comment
Please, Sign In to add comment