Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ukol1;
- import java.awt.Color;
- import java.awt.event.*;
- import fim.utils.Application;
- import fim.utils.Console;
- @SuppressWarnings("serial")
- public class Pixel extends Application {
- private int x, y;
- private Renderer r = new Renderer(img);
- public void start() {
- img = out.getCanvas().getImage();
- out.switchView(Console.VIEW_GRAPH);
- r.drawPixel(10, 20, 0xff0000);
- out.getCanvas().addMouseListener(new MouseAdapter() {
- public void mousePressed(MouseEvent ev) {
- x = ev.getX();
- y = ev.getY();
- }
- public void mouseReleased(MouseEvent ev) {
- r.drawLine(x, y, ev.getX(), ev.getY(), 0x00FF00);
- out.repaint();
- }
- });
- }
- public static void main(String[] args) {
- new Pixel().start();
- }
- }
- /* --------------------------------------------------------- */
- package ukol1;
- import java.awt.image.BufferedImage;
- public class Renderer {
- private BufferedImage img;
- public Renderer(BufferedImage img) {
- this.img = img;
- }
- public void drawPixel(int x, int y, int barva) {
- if (x > 0 && x < img.getWidth()) {
- if (y > 0 && y < img.getHeight()) {
- img.setRGB(x, y, barva);
- }
- }
- }
- public void drawLine(int x1, int y1, int x2, int y2, int barva) {
- int x;
- int y;
- float k, q;
- if (Math.abs(x2 - x1) > Math.abs(y2 - y1)) {
- if (x1 > x2) {
- int pom = x2;
- x2 = x1;
- x1 = pom;
- pom = y2;
- y2 = y1;
- y1 = pom;
- }
- int dx = x2 - x1;
- int dy = y2 - y1;
- k = (y1 - y2) / (float) (y2 - y1);
- q = y1 - k * x1;
- for (x = x1; x <= x2; x++) {
- y = Math.round(k * x + q);
- drawPixel(x, y, 0x00FF00);
- }
- } else {
- if (y1 > y2) {
- int pom = x2;
- x2 = x1;
- x1 = pom;
- pom = y2;
- y2 = y1;
- y1 = pom;
- }
- k = (float)(x1 - x2) / (y1 - y2);
- q = x2 - k * y2;
- for (y = y1; y <= y2; y++) {
- x = Math.round(k * y + q);
- drawPixel(x, y, 0x00FF00);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment