Skip to content

Instantly share code, notes, and snippets.

@martinsbalodis
Created December 4, 2011 20:40
Show Gist options
  • Save martinsbalodis/1431217 to your computer and use it in GitHub Desktop.
Save martinsbalodis/1431217 to your computer and use it in GitHub Desktop.
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package virsma3d;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import java.awt.geom.AffineTransform;
/**
*
* @author martins
*/
public class Virsma3D extends Frame implements MouseListener {
public Virsma3D()
{
this.addMouseListener(this);
this.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
public String image_location = "D:/svn_repositories/datorgrafika/KrasuKomponentes/src/krasukomponentes/2932c423fdfd5a29.jpg";
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Virsma3D frame = new Virsma3D();
// ielasa bildi kā argumentu
if(args.length==1) {
frame.image_location = args[0];
}
frame.pack();
frame.setSize(1000,800);
frame.setVisible(true);
}
public int draw_x = 282;
public int draw_y = 372;
public int radius = 20;
@Override
public void paint(Graphics graphics)
{
try {
// nolasa attēlu
BufferedImage image = ImageIO.read( new File( image_location ) );
int width = image.getWidth();
int height = image.getHeight();
if(this.draw_x-this.radius < 0) {this.draw_x = this.radius; System.err.println("asd");}
if(this.draw_y-this.radius < 0) this.draw_y = this.radius;
if(this.draw_x+this.radius > width) this.draw_x = width-this.radius;
if(this.draw_y+this.radius > height) this.draw_y = height-this.radius;
Graphics2D g2d = (Graphics2D) graphics;
// notīra grafilu vietu
g2d.setPaint(Color.white);
g2d.fillRect(0, 0, width*2, 1000);
// ievieto bildi
g2d.drawImage(image, null, 0,0);
// uzzīmē kvadrātu, kas tiks attēlots
graphics.drawRect(this.draw_x-this.radius, this.draw_y-this.radius, this.radius*2, this.radius*2);
int x,y;
int red_x = width+50;
int red_y = 100;
int green_x = width+50;
int green_y = 300;
int blue_x = width+50;
int blue_y = 500;
int dy = 4;
int dx = 6;
int p1_color_koef,p2_color_koef,p3_color_koef,p4_color_koef;
//grafika pamatnes
Polygon red_bottom = new Polygon();
red_bottom.addPoint(red_x, red_y);
red_bottom.addPoint(red_x+radius*2*dx, red_y);
red_bottom.addPoint(red_x+radius*2*dx+2*radius*5, red_y+radius*2*dy);
red_bottom.addPoint(red_x+2*radius*5, red_y+radius*2*dy);
g2d.setColor(Color.red);
g2d.drawPolygon(red_bottom);
Polygon green_bottom = new Polygon();
green_bottom.addPoint(green_x, green_y);
green_bottom.addPoint(green_x+radius*2*dx, green_y);
green_bottom.addPoint(green_x+radius*2*dx+2*radius*5, green_y+radius*2*dy);
green_bottom.addPoint(green_x+2*radius*5, green_y+radius*2*dy);
g2d.setColor(Color.green);
g2d.drawPolygon(green_bottom);
Polygon blue_bottom = new Polygon();
blue_bottom.addPoint(blue_x, blue_y);
blue_bottom.addPoint(blue_x+radius*2*dx, blue_y);
blue_bottom.addPoint(blue_x+radius*2*dx+2*radius*5, blue_y+radius*2*dy);
blue_bottom.addPoint(blue_x+2*radius*5, blue_y+radius*2*dy);
g2d.setColor(Color.blue);
g2d.drawPolygon(blue_bottom);
// Iegšut datus par krāsām
for(x=0;x<radius*2;x++) {
for(y=0;y<radius*2;y++) {
// punkti četrstūrim
Point p1 = new Point(x,y);
Point p2 = new Point(x+1,y);
Point p3 = new Point(x+1,y+1);
Point p4 = new Point(x,y+1);
Color point1 = new Color(image.getRGB(this.draw_x+p1.x-this.radius, this.draw_y+p1.y-this.radius));
Color point2 = new Color(image.getRGB(this.draw_x+p2.x-this.radius, this.draw_y+p2.y-this.radius));
Color point3 = new Color(image.getRGB(this.draw_x+p3.x-this.radius, this.draw_y+p3.y-this.radius));
Color point4 = new Color(image.getRGB(this.draw_x+p4.x-this.radius, this.draw_y+p4.y-this.radius));
// Sarkanais grafiks
p1_color_koef = point1.getRed()/5;
p2_color_koef = point2.getRed()/5;
p3_color_koef = point3.getRed()/5;
p4_color_koef = point4.getRed()/5;
Polygon redp = new Polygon();
redp.addPoint(red_x+(p1.x*dx)+(p1.y*5), red_y+(p1.y*dy)-p1_color_koef);
redp.addPoint(red_x+(p2.x*dx)+(p2.y*5), red_y+(p2.y*dy)-p2_color_koef);
redp.addPoint(red_x+(p3.x*dx)+(p3.y*5), red_y+(p3.y*dy)-p3_color_koef);
redp.addPoint(red_x+(p4.x*dx)+(p4.y*5), red_y+(p4.y*dy)-p4_color_koef);
g2d.setPaint(Color.white);
g2d.fillPolygon(redp);
g2d.setColor(Color.red);
g2d.drawPolygon(redp);
// Zaļais
p1_color_koef = point1.getGreen()/5;
p2_color_koef = point2.getGreen()/5;
p3_color_koef = point3.getGreen()/5;
p4_color_koef = point4.getGreen()/5;
Polygon greenp = new Polygon();
greenp.addPoint(green_x+(p1.x*dx)+(p1.y*5), green_y+(p1.y*dy)-p1_color_koef);
greenp.addPoint(green_x+(p2.x*dx)+(p2.y*5), green_y+(p2.y*dy)-p2_color_koef);
greenp.addPoint(green_x+(p3.x*dx)+(p3.y*5), green_y+(p3.y*dy)-p3_color_koef);
greenp.addPoint(green_x+(p4.x*dx)+(p4.y*5), green_y+(p4.y*dy)-p4_color_koef);
g2d.setPaint(Color.white);
g2d.fillPolygon(greenp);
g2d.setColor(Color.green);
g2d.drawPolygon(greenp);
// Zilais
p1_color_koef = point1.getBlue()/5;
p2_color_koef = point2.getBlue()/5;
p3_color_koef = point3.getBlue()/5;
p4_color_koef = point4.getBlue()/5;
Polygon bluep = new Polygon();
bluep.addPoint(blue_x+(p1.x*dx)+(p1.y*5), blue_y+(p1.y*dy)-p1_color_koef);
bluep.addPoint(blue_x+(p2.x*dx)+(p2.y*5), blue_y+(p2.y*dy)-p2_color_koef);
bluep.addPoint(blue_x+(p3.x*dx)+(p3.y*5), blue_y+(p3.y*dy)-p3_color_koef);
bluep.addPoint(blue_x+(p4.x*dx)+(p4.y*5), blue_y+(p4.y*dy)-p4_color_koef);
g2d.setPaint(Color.white);
g2d.fillPolygon(bluep);
g2d.setColor(Color.blue);
g2d.drawPolygon(bluep);
}
}
}
catch(Exception e) {
System.err.println(e.getMessage());
}
}
public void mouseClicked(MouseEvent e) {
}
/**
* Invoked when a mouse button has been pressed on a component.
*/
public void mousePressed(MouseEvent e){
}
/**
* Invoked when a mouse button has been released on a component.
*/
public void mouseReleased(MouseEvent e){
this.draw_x = e.getPoint().x;
this.draw_y = e.getPoint().y;
this.paint(this.getGraphics());
System.out.println("mouse"+this.draw_x+" "+this.draw_y);
}
/**
* Invoked when the mouse enters a component.
*/
public void mouseEntered(MouseEvent e){
}
/**
* Invoked when the mouse exits a component.
*/
public void mouseExited(MouseEvent e){
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment