Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Swing / AWT / SWT
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Jeanne Boyarsky
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Rob Spoor
Devaka Cooray
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Tim Moores
Bartenders:
Mikalai Zaikin
Forum:
Swing / AWT / SWT
Help on zooming in on an image
Michael Cullen
Greenhorn
Posts: 10
posted 12 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
I'm trying to get this to zoom in on a specific piece of an image. Any tips on how I could do that from my current code?
import java.awt.*; import java.awt.Color; public class Zoom { //need to input 4-coord to make 2 points. then, calculate width & height separately . then, make image proportional to original image size. public static void main(String args[]) { Picture pic = new Picture("soccer.jpg"); Picture zoomedPic = new Picture(pic.width(), pic.height()); System.out.println("Please enter the x value of the first point: "); int x0 = StdIn.readInt(); System.out.println("Please enter the y value of the first point: "); int y0 = StdIn.readInt(); System.out.println("Please enter the x value of the second point: "); int x1 = StdIn.readInt(); System.out.println("Please enter the y value of the second point: "); int y1 = StdIn.readInt(); waitForClick(); pic.show(); // zoomedPic.show(); // This picture is filled with black pixels Zoom(x0, x1, y0, y1, pic, zoomedPic); // zoomedPic2.show(); } public static void Zoom(int x0, int x1, int y0, int y1, Picture pic, Picture zoomedPic) { int deltax = Math.abs(x0 - x1); int deltay = Math.abs(y0 - y1); int scaleOfX = (int)(pic.width() / deltax); // int scaleOfY = (int)(pic.height() / deltay ); //calculates ratios of width and height compared to original. Picture zoomedPic2 = new Picture(deltax, deltay); for(int k = 0; k < pic.width(); k++){ for(int m = 0; m < pic.height(); m++) { Color zoomColor = pic.get(k, m); for(int i = 0; i < pic.width(); i+=scaleOfX){ for(int j = 0; j < pic.height(); j+=scaleOfY){ zoomedPic.set(i, j , zoomColor); for(int p = 0; p < zoomedPic.width(); p++){ for(int q = 0; q < zoomedPic.height(); q++){// new color from previous array. Color zoomedColor = zoomedPic.get(i, j); zoomedPic2.set(p, q, zoomedColor); } } } } } } zoomedPic2.show(); } public static void waitForClick() { boolean waiting = true; while(waiting) { if (StdDraw.mousePressed()) waiting = false; } } }
Campbell Ritchie
Marshal
Posts: 79178
377
posted 12 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
I think this
thread
would fit better in our GUIs forum. Moving.
Consider Paul's
rocket mass heater
.
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Moving Shapes between two Points
Recursion SnowFlake ?
Dx Ball Game
Moving clickedShapes and only clickedShapes on a panel
Zooming in and out - cursor not in the right place
More...