Simple DrawImage HELP!
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);
}
}
}

