Last active
July 5, 2020 17:51
-
-
Save theahmadzai/28182675caa8c9d6fd0ec7c12e3e7e0f to your computer and use it in GitHub Desktop.
Assignment 11 CG
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package computergraphics; | |
import javax.imageio.ImageIO; | |
import javax.swing.JButton; | |
import javax.swing.JFileChooser; | |
import javax.swing.JFrame; | |
import javax.swing.JPanel; | |
import java.awt.Color; | |
import java.awt.Point; | |
import java.awt.image.BufferedImage; | |
public class DrawingPanel extends JPanel { | |
DrawingPanel() { | |
setBackground(Color.WHITE); | |
} | |
public static void main(String[] args) { | |
JFrame frame = new JFrame("Javed - 2017-ARID-264"); | |
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
frame.setLayout(null); | |
JButton reflectButton = new JButton("Load and Clip"); | |
reflectButton.setBounds(10, 10, 200, 30); | |
frame.add(reflectButton); | |
DrawingPanel drawingPanel = new DrawingPanel(); | |
drawingPanel.setBounds(10, 50, 500, 500); | |
frame.add(drawingPanel); | |
reflectButton.addActionListener(e -> { | |
JFileChooser fileChooser = new JFileChooser(); | |
fileChooser.setCurrentDirectory(null); | |
int option = fileChooser.showOpenDialog(frame.getContentPane()); | |
if(option == JFileChooser.APPROVE_OPTION) { | |
try { | |
// Load Image | |
BufferedImage bimage = ImageIO.read(fileChooser.getSelectedFile()); | |
drawingPanel.getGraphics().drawImage(bimage, 0, 0, null); | |
// Clip | |
int xmin = 250; | |
int xmax = 450; | |
int ymin = 100; | |
int ymax = 300; | |
int width = 200; | |
int height = 200; | |
BufferedImage clippedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); | |
for(int x = xmin; x <xmax; x++) { | |
for(int y = ymin; y<ymax; y++) { | |
clippedImage.setRGB(x-xmin, y-ymin, bimage.getRGB(x, y)); | |
} | |
} | |
drawingPanel.getGraphics().drawImage(clippedImage, 0, 0, null); | |
} catch(Exception ex) { } | |
} | |
}); | |
frame.setSize(600, 600); | |
frame.setVisible(true); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package computergraphics; | |
import javax.swing.JButton; | |
import javax.swing.JFrame; | |
import javax.swing.JPanel; | |
import java.awt.Color; | |
import java.awt.EventQueue; | |
import java.awt.Graphics; | |
import java.awt.Point; | |
public class DrawingPanel extends JPanel { | |
DrawingPanel() { | |
setBackground(Color.WHITE); | |
} | |
public void paint(Graphics g) { | |
super.paint(g); | |
g.setColor(Color.RED); | |
g.drawPolygon(new int[] {200, 300, 100}, new int[] {100, 200, 200}, 3); | |
} | |
public static Point[] reflectTriangle(Point x, Point y, Point z, Point p) { | |
Point rx = new Point(p.x+(x.x-p.x), p.y+(-x.y+p.y)); | |
Point ry = new Point(p.x+(y.x-p.x), p.y+(-y.y+p.y)); | |
Point rz = new Point(p.x+(z.x-p.x), p.y+(-z.y+p.y)); | |
return new Point[] {rx, ry, rz}; | |
} | |
public static void main(String[] args) { | |
JFrame frame = new JFrame("Javed - 2017-ARID-264"); | |
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
frame.setLayout(null); | |
JButton reflectButton = new JButton("Reflect"); | |
reflectButton.setBounds(10, 10, 100, 30); | |
frame.add(reflectButton); | |
DrawingPanel drawingPanel = new DrawingPanel(); | |
drawingPanel.setBounds(10, 50, 500, 500); | |
frame.add(drawingPanel); | |
reflectButton.addActionListener(e -> { | |
Graphics g = drawingPanel.getGraphics(); | |
Point[] points = reflectTriangle( | |
new Point(200, 100), | |
new Point(300, 200), | |
new Point(100, 200), | |
new Point(220, 220) // reflection point | |
); | |
System.out.println(points[0].x + ", " + points[0].y); | |
g.drawPolygon( | |
new int[] {points[0].x,points[1].x, points[2].x}, | |
new int[] {points[0].y, points[1].y, points[2].y}, | |
3); | |
}); | |
frame.setSize(540, 600); | |
frame.setVisible(true); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package computergraphics; | |
import javax.imageio.ImageIO; | |
import javax.swing.JButton; | |
import javax.swing.JFileChooser; | |
import javax.swing.JFrame; | |
import javax.swing.JPanel; | |
import java.awt.Color; | |
import java.awt.Point; | |
import java.awt.image.BufferedImage; | |
public class DrawingPanel extends JPanel { | |
DrawingPanel() { | |
setBackground(Color.WHITE); | |
} | |
public static Point w2v(Point pw, | |
int xwMin, int xwMax, int ywMin, int ywMax, | |
int xvMin, int xvMax, int yvMin, int yvMax) { | |
int x = (int)(xvMin + (float)((pw.x - xwMin) * (float)(xvMax - xvMin) / (xwMax - xwMin))); | |
int y = (int)(yvMin + (float)((pw.y - ywMin) * (float)(yvMax - yvMin) / (ywMax - ywMin))); | |
return new Point(x,y); | |
} | |
public static void main(String[] args) { | |
JFrame frame = new JFrame("Javed - 2017-ARID-264"); | |
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
frame.setLayout(null); | |
JButton reflectButton = new JButton("Load"); | |
reflectButton.setBounds(10, 10, 100, 30); | |
frame.add(reflectButton); | |
DrawingPanel drawingPanel1 = new DrawingPanel(); | |
drawingPanel1.setBounds(10, 50, 500, 500); | |
frame.add(drawingPanel1); | |
DrawingPanel drawingPanel2 = new DrawingPanel(); | |
drawingPanel2.setBounds(520, 50, 500, 500); | |
frame.add(drawingPanel2); | |
reflectButton.addActionListener(e -> { | |
JFileChooser fileChooser = new JFileChooser(); | |
fileChooser.setCurrentDirectory(null); | |
int option = fileChooser.showOpenDialog(frame.getContentPane()); | |
if(option == JFileChooser.APPROVE_OPTION) { | |
try { | |
// Simple | |
BufferedImage bimage = ImageIO.read(fileChooser.getSelectedFile()); | |
drawingPanel1.getGraphics().drawImage(bimage, 0, 0, null); | |
// V2W | |
BufferedImage viewPort = new BufferedImage( | |
drawingPanel2.getWidth(), | |
drawingPanel2.getHeight(), | |
BufferedImage.TYPE_INT_ARGB | |
); | |
for(int x = 0; x <bimage.getWidth(); x++) { | |
for(int y = 0; y<bimage.getHeight(); y++) { | |
Point p = w2v( | |
new Point(x,y), | |
0, bimage.getWidth(), 0, bimage.getHeight(), 0, | |
drawingPanel2.getWidth(), 0, drawingPanel2.getHeight() | |
); | |
viewPort.setRGB(p.x, p.y, bimage.getRGB(x, y)); | |
} | |
} | |
drawingPanel2.getGraphics().drawImage(viewPort, 0, 0, null); | |
} catch(Exception ex) { } | |
} | |
}); | |
frame.setSize(1040, 600); | |
frame.setVisible(true); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment