//redim1.java

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



public class redim1 extends Applet
{
  Image img,imgn;
  Graphics g;
  int dimlat,dimin;  //dimensiuni imagine img
  int lat,inalt;  //dimensiuni imgn
 
  Panel p=new Panel();
  
    public void init()
   {
     Color culoare=new Color(200,150,250);
     setBackground(culoare);
     String imaginea=getParameter("imagine");
     if (imaginea==null)
           imaginea="Andreea1.jpg" ;
    img=getImage(getCodeBase(), imaginea);
    dimlat=img.getWidth(this);
    dimin=img.getHeight(this);
    imgn=img;
//  MemoryImageSource mis;                 
    Image rimg;
    
    setLayout(new BorderLayout());
    p.add(new Button("Mareste"));
    p.add(new Button("Micsoreaza"));
    add("North",p);
  }
 
 public boolean action(Event evt, Object ob)
{
  Graphics g=getGraphics();
  //inalt=imgn.getHeight(this);
 // lat=imgn.getWidth(this);
  g.clearRect(60,100+dimin,60+lat,100+dimin+inalt);
  g.dispose();
    if (evt.target instanceof Button)
  {
    
     if (ob.equals("Mareste"))
      {
        redimImage rim1=new redimImage(imgn,1);
        imgn=rim1.rimg;
      }
    else if (ob.equals("Micsoreaza"))
      {
        redimImage rim2=new redimImage(imgn,0);
        imgn=rim2.rimg;
      }  
    repaint();
    return true;
  }
  return false;
}   
 

public void paint(Graphics g)
{
 inalt=imgn.getHeight(this);
 lat=imgn.getWidth(this);
  g.drawImage(img,60,60, this);
  g.drawImage(imgn,60,100+dimin,this);
         
}

}

//redimImage.java

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


public class redimImage extends Applet
{

//  MemoryImageSource mis;                 
  Image rimg;

  redimImage (Image source, int r_type) {
	boolean success;
        int widtho,widthn,heighto,heightn,oldi,newi;
	int[] Pixo,Pixn;
	int pixelval;
	
	PixelGrabber pg;
	widtho = source.getWidth(this);
	heighto = source.getHeight(this);

    if (r_type == 1)    // dublu
	{
		widthn = widtho * 2;
		heightn = heighto * 2;
	}
	else                            // 1/2
	{
		widthn = (int) widtho/2;
		heightn = (int) heighto/2;
	}

	Pixo = new int[widtho*heighto];     //  original
	Pixn = new int[widthn*heightn];     // destinatie

	pg = new PixelGrabber(source.getSource(), 0,0,widtho,heighto,Pixo,0,widtho);

	try
	{
		success = pg.grabPixels(0);             
	}                                                                      
	catch (Exception e)
	{
		System.out.println("Eroare!! " + e);
	}

	if (r_type == 1)  // *2
	{
		for (int y=0; y < heighto; y++)
		{
			for (int x=0; x < widtho; x++)
			{
				oldi = widtho*y+x;

				newi = (widthn*y)*2+(x*2);

				pixelval = Pixo[oldi];

				Pixn[newi] = pixelval;
				Pixn[newi+1] = pixelval;
				Pixn[newi+widthn] = pixelval;
				Pixn[newi+widthn+1] = pixelval;
			}
		}
	}
	else // (r_type == 0)  // 1/2
	{
		for (int y=0; y < heightn; y++)
		{
			for (int x=0; x < widthn; x++)
			{
				oldi = (widtho*y)*2+(x*2);

				newi = widthn*y+x;

				pixelval = Pixo[oldi];

				Pixn[newi] = pixelval;
			}
		}
	}

	rimg = createImage(new MemoryImageSource(widthn,heightn,Pixn,0,widthn));

  }


  public void paint (Graphics g1)
  {	
	g1.drawImage(rimg,250,250,this);
  }


}