Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Zoom In Function help!!!!!

 
Steven Gibbs
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there. I am creating a simple image editor in Netbeans 4.1. I have a Frame which has a panel listing options to the user such as Zoom In, Zoom Out, Set the picture to black and white etc. I then have a panel where images are loaded to and edited. I am in the process of making the Zoom In function work. I have set up the code in the frame and have been given the code for the panel with gaps that we need to fill in. My problem is that I can't get it to work. Below is the code

public void zoomIn() {
int row, column;
int r = 0, g = 1, b = 2; // To access arrays easily
int tLeft = 0, tRight = 4;
int mLeft = 0, mMid = 4, mRight = 8;
int cMod, cTemp;
// The counter for the actual array element, with RGBA as 4 entries
int block;
// One block of pixels to be expanded at a time
int rowTemp, rowModified;
int wmaxT, wmaxM;

if(zoomLevel > rowModified) return; // Cannot zoom any further
gTempBuffer.drawImage(myModifiedBuffer, 0, 0, this);
gModifiedBuffer.setColor(Color.white);
gModifiedBuffer.fillRect(0, 0, dPanel.width, dPanel.height);

hmax = myHeight[zoomLevel + 1]; // The zoomed height
if(hmax > dPanel.height) {// Keep to panel height
hmax = dPanel.height - dPanel.height / 3;
// Keep zoomed image to panel height
} else {
hmax = myHeight[zoomLevel];
}

wmaxT = myWidth[zoomLevel + 1];
if(wmaxT > dPanel.width) {
wmaxT = dPanel.width - dPanel.width/ 3;
} else {
wmaxT = myWidth[zoomLevel];
}

wmaxM = Math.min(myWidth[zoomLevel + 1], dPanel.width);

myTempRaster = myTempBuffer.getRaster();
myModifiedRaster = myModifiedBuffer.getRaster();
for( row = 0; row < (hmax / 2) - 1; row++){ // -1 to keep in range
rowTemp = row * 2; // 2 rows per pass
rowModified = row * 3; // 3 rows per pass

myTempRaster.getPixels(0, rowTemp, wmaxT, 1, myTempRasterArrayTop); // Top row to top array
myTempRaster.getPixels(0, rowTemp + 1, wmaxT, 1, myTempRasterArrayBot); // Bottom row to bot array
myModifiedRaster.getPixels(0, rowModified, wmaxM, 1, myModifiedRasterArrayTop); // Top row to top array
myModifiedRaster.getPixels(0, rowModified + 1, wmaxM, 1, myModifiedRasterArrayMid); // Bottom row to bot array
myModifiedRaster.getPixels(0, rowModified + 2, wmaxM, 1, myModifiedRasterArrayBot);

for(block = 0; block < wmaxT / 2; block++) { // Block is a pair of pixels
cTemp = block * 2 * 4;
// block counts 4 rgba entries to the array and 2 pixels
cMod = block * 3 * 4; // Modified block is 3 pixels * 4(rgba)

for(red = 0; red < 3; red++) {
myModifiedRasterArrayTop[cMod + red + mLeft] // Top left
= myTempRasterArrayTop[cTemp + red + tLeft];

myModifiedRasterArrayTop[cMod + red + mMid] = 0; // top middle

myModifiedRasterArrayTop[cMod + red + mRight] = // top right
myTempRasterArrayTop[cTemp + red + tRight];

myModifiedRasterArrayMid[cMod + red + mLeft] = 0; // middle left


myModifiedRasterArrayMid[cMod + red + mRight] = 0; // middle right

myModifiedRasterArrayBot[cMod + red + mLeft] = // bottom left
myTempRasterArrayBot[cTemp + red + tLeft];

myModifiedRasterArrayBot[cMod + red + mMid] = 0; // bottom middle

myModifiedRasterArrayBot[cMod + red + mRight] = // bottom right
myTempRasterArrayBot[cTemp + red + tRight];

myModifiedRasterArrayMid[cMod + red + mMid] = 0; // middle middle
}
}
myModifiedRaster.setPixels(0, rowModified, wmaxM, 1,
myModifiedRasterArrayTop);

myModifiedRaster.setPixels(0, rowModified + 1, wmaxM, 1,
myModifiedRasterArrayMid);

myModifiedRaster.setPixels(0, rowModified + 2, wmaxM, 1,
myModifiedRasterArrayBot);
}
zoomLevel++;
paintComponent(this.getGraphics());
}

Any Ideas?
Many thanks in advance for any responses i get
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you expect your code to do? What *does* it do instead?
 
Steven Gibbs
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It just needs to enlarge the picture and make it bigger when I zoom in. in the description it says we need to declare myTempRaster and myModifiedRaster, myTempRasterArrayTop[] etc but i'm not sure how to do this. If i could get that bit sorted I could probably work it out from there. Also i'm not sure if - if(zoomLevel > rowModified) return is correct as before it just had 3 question marks in place of rowModified which is 1 of the blanks we have to fill in
 
Joe Ess
Bartender
Posts: 9256
9
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Steven Gibbs:
the description it says we need to declare myTempRaster and myModifiedRaster,


Where is this "description" you speak of? I think you still are leaving some information out of your post.
I've used the getScaledInstance() method of java.awt.Image to scale images up and down (i.e. zoom in and out). Worked fine for my purposes.
 
Steven Gibbs
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok all i need to know is how to declare myTempRaster, myModifiedRaster, myTempRasterArrayTop[] etc but i'm not sure how to do this.
 
Joe Ess
Bartender
Posts: 9256
9
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What type is myTempBuffer? I don't see it declared in the code you've posted.
What type does myTempBuffer.getRaster() return?
Like I said, you're leaving information out that we need in order to answer your question.
 
Steven Gibbs
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry bout this joe but lab notes are not very clear. this is what it says:

So, what is this doing? The clue lies in the variable names:

Modified Array references are to the modified buffer
Temp Array references are to the temp buffer
Array Top, Mid, Bot refer to the top, middle and bottom rows of the pixel square being zoomed
Left, right, mid etc. refer to positions on the pixel square which is being zoomed (see below)
Red is a variable which stands for red, green, blue (remember the arrays hold RGB as 3 different sequential entries, so we must process each in turn).
Row is a variable which counts off the rows of the array, but 2 at a time for temp and 3 at a time for modified
rowTemp and rowModified are the rows in the corresponding picture, one going 2 rows per step and the other 3 rows per step.
All the central code simply copies:
Temp (top right) to Modified (top right)
Modified (top middle) from (Temp (top left) + Temp (top right)) / 2
Modified (middle middle) from (Modified (top middle) + Modified (bottom middle)) / 2
All the bits at the beginning are trying to keep the zoomed image within the bounds of the panel.


This is a simple zoom method and the results are not very good. However, better zoom is complex
 
Joe Ess
Bartender
Posts: 9256
9
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, so this is homework.
So is myTempBuffer declared anywhere else in the code you were given? If so, what type is it? If it has not been declared, I'd say you have to speak with your instructor.
 
Steven Gibbs
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is what I have in my code related to Temp Buffer:

private BufferedImage myTempBuffer;
private Graphics2D gTempBuffer;

myTempBuffer = new BufferedImage(dPanel.width, dPanel.height,
BufferedImage.TYPE_INT_ARGB_PRE);
gTempBuffer = (Graphics2D) myTempBuffer.getGraphics();
gTempBuffer.setColor(Color.white);
gTempBuffer.fillRect(0, 0, dPanel.width, dPanel.height);
 
Joe Ess
Bartender
Posts: 9256
9
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So tempBuffer is of type BufferedImage. You look at the API documentation for BufferedImage and look at the return type for getRaster(). That's the type you have to declare myTempRaster to be.
 
Steven Gibbs
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok i think i got it working apart form this bit from the code in my first post:



originally it read:



It dosen't seem to like rowModified. From what you can see of the code do you have any ideas?
 
Joe Ess
Bartender
Posts: 9256
9
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It dosen't seem to like rowModified. From what you can see of the code do you have any ideas?

What doesn't like rowModified? What doesn't it like about row modified?
 
Steven Gibbs
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when it compiles it says it has not be initialised
 
Joe Ess
Bartender
Posts: 9256
9
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right. To prevent developers from being stupid, the Java compiler insists that variables be initalized before they are used. rowModified is a poor candidate to replace "???" for two reasons:
1. it is not initalized until 25 lines after you have used it
2. I don't see the row modified having anything to do with the zoom level.
Now, is there a better candidate in your code for comparing to the zoom level to see if we can zoom further?
 
Steven Gibbs
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
would it be hmax?
 
Joe Ess
Bartender
Posts: 9256
9
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now you are just guessing. hmax isn't initalized until after you zoomLevel.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic