i am making a GUI canvas like forum with back ground image and sub-images
( to act as buttons) and i don't know when i add action to its sub-images
How to make the action is to repaint the canvas and change part of the screen only not all the screen like what i am facing here ( i just call repaint)
i.e it draws all the screen again and make repeat some sub-image in different location which i don't want to show when i want to show it in the same area but with another color or border style .
here my code:
public class DrawImage extends Canvas implements Runnable{
// Define your variables
Image source;
Font f;
int fontheight;
int mWidth= getWidth();
int mHeight= getHeight();
private static int i=0;
private String mMessage=" ";
public DrawImage(){
try{
source=getImage("images/ma3ak.PNG") ;
}catch(IOException e){
System.out.print("Couldnot Load the image");
e.printStackTrace();
}
f= Font.getFont(Font.FACE_MONOSPACE,i, Font.SIZE_MEDIUM);//Font.getDefaultFont();
fontheight=f.getHeight();
}
public Image getImage(String path) throws IOException {
return Image.createImage(path);
}
public void run() {
}
public void paint (Graphics g){
try {
g.drawImage(source, 0, 0, Graphics.TOP | Graphics.LEFT);
g.setColor(255,10, 10);
g.setFont(f);
g.drawString("Welcome To Ma3ak Mobile ", mWidth / 2, 0, Graphics.TOP | Graphics.HCENTER);
drawSubscriptions(g);
DrawGameItem(g);
drawTarfeeh(g);
//repaint();
} catch (IOException ex) {
ex.printStackTrace();
}
}
private void drawTarfeeh(Graphics g) throws IOException {
tarfeeh = resizeImage(Image.createImage("images/tarfeeh.PNG"), mWidth , mHeight/8) ;
g.drawImage(subscriptions, 0, mHeight / 2, Graphics.TOP | Graphics.LEFT);
}
private void drawSubscriptions(Graphics g) throws IOException {
subscriptions = resizeImage(Image.createImage("images/subscriptions.png"), mWidth, mHeight / Cool;
g.drawImage(subscriptions, 0, mHeight / 6, Graphics.TOP | Graphics.LEFT);
}
public Image getImage(String path) throws IOException {
return Image.createImage(path);
}
private Image resizeImage(Image src,int screenWidth,int screenHeight) {
int srcWidth = src.getWidth();
int srcHeight = src.getHeight();
Image tmp = Image.createImage(screenWidth, srcHeight);
Graphics g = tmp.getGraphics();
int ratio = (srcWidth << 16) / screenWidth;
int pos = ratio/2;
//Horizontal Resize
for (int x = 0; x < screenWidth; x++) {
g.setClip(x, 0, 1, srcHeight);
g.drawImage(src, x - (pos >> 16), 0, Graphics.LEFT | Graphics.TOP);
pos += ratio;
}
Image resizedImage = Image.createImage(screenWidth, screenHeight);
g = resizedImage.getGraphics();
ratio = (srcHeight << 16) / screenHeight;
pos = ratio/2;
//Vertical resize
for (int y = 0; y < screenHeight; y++) {
g.setClip(0, y, screenWidth, 1);
g.drawImage(tmp, 0, y - (pos >> 16), Graphics.LEFT | Graphics.TOP);
pos += ratio;
}
return resizedImage;
}//resize image
//then i repaint it with to user actions selecting a sub_image (as Button here)

Reply With Quote


