Simple DrawImage HELP!

Hi all,
I just started programming Java and am having a problem with the current program that I am working on.

The objective of the applet:
- Display image (150 x 150) in size
- Draw rectangle over image
- Redraw the selected part within the rectangle
(There are also elements to change colors, and resize but I think I understand that)

The problem with my code is minor, everything works but the redrawn image is always the wrong dimensions. Also, i can't get the redrawn image to clear each time a new rectangle is drawn.

Here is my code! Thanks in advance for any help!

import java.applet.*;
import java.awt.*;
import java.awt.image.*;

public class imageworks extends Applet {
Scrollbar sRed;
Scrollbar sGreen;
Scrollbar sBlue;
Image img, myimg;
CheckboxGroup group1 = new CheckboxGroup();
Checkbox option1 =new Checkbox("X1", group1, false);
Checkbox option2 = new Checkbox("X2", group1, false);
Label lRed=new Label(" Red ");
Label lGreen=new Label(" Green ");
Label lBlue=new Label(" Blue ");
int pix[]=new int [150*150];
int xpos, ypos, endx, endy, linex, liney;
int px, py, w, h;
public void init() {

setBackground(Color.gray);
setLayout(null);
setSize(new Dimension(600,600));
sRed = new Scrollbar(Scrollbar.HORIZONTAL,0, 1, 0, 255);add(lRed); add(sRed);
sGreen = new Scrollbar(Scrollbar.HORIZONTAL,0, 1, 0, 255); add(lGreen);add(sGreen);
sBlue = new Scrollbar(Scrollbar.HORIZONTAL,0, 1, 0, 255);add (lBlue); add(sBlue);
add(option1);
add(option2);
sRed.setBackground(Color.red);
sGreen.setBackground(Color.green);
sBlue.setBackground(Color.blue);
lRed.setBackground(Color.red);
lGreen.setBackground(Color.green);
lBlue.setBackground(Color.blue);
option1.setBackground(Color.yellow);
option2.setBackground(Color.yellow);
sRed.setBounds(5, 400, 100, 15);
sGreen.setBounds(110,400,100,15);
sBlue.setBounds(215,400,100,15);
option1.setBounds(320,400,35,15);
option2.setBounds(360,400,35,15);
lRed.setBounds(40,375,35, 15);
lGreen.setBounds(145,375,45,15);
lBlue.setBounds(250,375,35,15);

}

public boolean mouseDown(Event evt, int x, int y)
{
xpos = x; ypos = y;
return true;
}
public boolean mouseDrag(Event evt, int x, int y)
{
endx = x;
endy = y;
return true;
}
public boolean mouseUp(Event evt, int x, int y)
{

repaint();
return true;
}
public void paint (Graphics g){
img = getImage(getDocumentBase(), "spider.jpg");
g.drawImage(img, 5, 5, this);
if (((xpos > 5 && xpos <155)&&(ypos > 5 && ypos< 155))&&((endx > 5 && endx <155)&&(endy > 5 && endy< 155))){
if (xpos > endx && ypos > endy){
g.drawRect(endx, endy, xpos - endx ,ypos - endy);
px = endx;
py = endy;
w = xpos - endx;
h = ypos - endy;
}

if (xpos < endx && ypos < endy){
g.drawRect(xpos,ypos, endx - xpos, endy-ypos );
px = xpos;
py = ypos;
w = endx-xpos;
h = endy - ypos;}

if (xpos < endx && ypos > endy){
g.drawRect(xpos,endy, endx - xpos, ypos-endy );
px = xpos;
py = endy;
w = endx - xpos;
h = ypos - endy;}

if (xpos > endx && ypos < endy){
g.drawRect(endx, ypos, xpos - endx, endy- ypos);
px = endx;
py = ypos;
w = xpos - endx;
h = endy-ypos;}
}
PixelGrabber pg=new PixelGrabber(img, xpos, ypos, endx, endy, pix, 0, endx);
try {
boolean s=false;
while(s==false)
s=pg.grabPixels();
}
catch (InterruptedException e) { }
for (int i = 0; i <150*150; i++){
pix[i] = pix [i]| sRed.getValue() << 16 |sGreen.getValue ()<<8|sBlue.getValue();}
myimg=createImage(new MemoryImageSource(endx,endy,pix,0,endx));
if (option1.getState()){
g.drawImage(myimg, 300, 5, endx,endy,this);

}
if (option2.getState()){
g.drawImage(myimg, 300, 5, endx*2,endy*2,this);
}


}

}
[4511 byte] By [yukon212] at [2007-11-20 11:36:03]
# 1 Re: Simple DrawImage HELP!
Hello and welcome to the forum. You'll want to repost your code with code tags to make it readable. You do this by editing your post, highlighting your code and pressing the "code" button. This will make your code keep its formatting and make it more readable.

A couple of pointers:

You'll want to learn about the Layout manager classes and how to use them. I recommend avoiding absolute positioning as you are trying to do. Your display cannot reposition itself well if the frame is resized.
Do not have the getImage method called within the paint override method. This override can be called by you or by the JVM many times and must be kept lean and mean. Any processing that can be done outside of this method should be done outside of the method.
You'll want to learn the java naming conventions. This includes having all classes first letters capitalized, all letters of constants capitalized, and all variables' first letters lower-case. This may not seem such a big deal, but it is for us trying to read and understand someone else's code that doesn't conform to conventions.
Consider using Swing (JApplet) and not AWT (Applet). Swing is the more recent of the two, and for my money has the better api.
Try adding super.paint(g) as the first line of your paint method override.

Good luck!

Pete
petes1234 at 2007-11-10 2:14:04 >