import
java.applet.Applet;
import
java.awt.Color;
import
java.awt.Font;
import
java.awt.Graphics;
import
java.awt.Image;
import
java.awt.Toolkit;
public
class
AppletImage
extends
Applet {
private
Image img;
public
void
init() {
img = Toolkit.getDefaultToolkit().getImage(
"gfglogo.png"
);
}
public
void
paint(Graphics g) {
if
(img !=
null
) {
g.setColor(Color.WHITE);
g.fillRect(
0
,
0
, getWidth(), getHeight());
g.setColor(Color.BLACK);
g.drawRect(
10
,
10
, getWidth() -
20
, getHeight() -
20
);
int
widImg = img.getWidth(
this
);
int
heiImg = img.getHeight(
this
);
if
(widImg >
0
&& heiImg >
0
) {
int
x =
20
;
int
y =
20
;
int
maxWid = getWidth() -
40
;
int
maxHei = getHeight() -
40
;
int
nWid, nHei;
if
(widImg <= maxWid && heiImg <= maxHei) {
nWid = widImg;
nHei = heiImg;
}
else
{
double
wRatio = (
double
) maxWid / widImg;
double
hRatio = (
double
) maxHei / heiImg;
double
scale = Math.min(wRatio, hRatio);
nWid = (
int
) (widImg * scale);
nHei = (
int
) (heiImg * scale);
}
g.drawImage(img, x, y, nWid, nHei,
this
);
}
g.setColor(Color.BLACK);
g.setFont(
new
Font(
"SansSerif"
, Font.BOLD,
16
));
g.drawString(
"GeeksforGeeks Logo"
,
20
, getHeight() -
10
);
}
}
}