By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,798 Members | 1,336 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,798 IT Pros & Developers. It's quick & easy.

drawString()

P: n/a
hi all

i need to draw a string diagonally on an applet.
the scenario is like ....
i'm writing a game, when a player wins ....i'd like to draw string
diagonally on the board saying XYZ wins the game or Game Over!!!
how do i acheive this ?
any ideas ?

amey
Jul 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"Amey Samant" <am*****@yahoo.com> wrote in message
news:66**************************@posting.google.c om...
hi all

i need to draw a string diagonally on an applet.
the scenario is like ....
i'm writing a game, when a player wins ....i'd like to draw string
diagonally on the board saying XYZ wins the game or Game Over!!!
how do i acheive this ?
any ideas ?

amey

If you just want some static text like "Game Over", make it a transparent
GIF and draw it as an Image to your game board.
Jul 17 '05 #2

P: n/a
am*****@yahoo.com (Amey Samant) wrote in message news:<66**************************@posting.google. com>...
hi all

i need to draw a string diagonally on an applet.
the scenario is like ....
i'm writing a game, when a player wins ....i'd like to draw string
diagonally on the board saying XYZ wins the game or Game Over!!!
how do i acheive this ?
any ideas ?


I know that this can be done using the Java 2D API, but I don't know
the specifics since I've never used it myself. Take a look at the
documentation:
http://java.sun.com/j2se/1.4.2/docs/...d-bookTOC.html
Jul 17 '05 #3

P: n/a
hi Karl
thanx for the pointer :)
i went through some of 2D graphics tutorial and found the solution.
if you want you can search for "AffineTransform tutorial" on sun's
site.
and then check transforming Text,Images,Shapes tutorial.
i did not understand entirely ;) (still working on it ... inner
details of AffineTransform)
neways
i wrote small test applet to see it work. i picked up bare minimum
required lines from their tutorial code.
if you are interested ... im posting the code below.
their tutorial example is just awesome & worth having a look but i
picked only minimum stuff

cheers n Merry X'mas
amey
import java.awt.*;
import java.awt.font.*;
import java.awt.geom.*;
import java.applet.*;

//<applet code=MyApplet height=300 width=400></applet>
public class MyApplet extends Applet
{
int w,h;
AffineTransform textAt,at;
Shape shape;
TextLayout textTl;

public void init()
{
setBackground(new Color(90,90,180));
at = new AffineTransform();
textAt= new AffineTransform();
textTl= new TextLayout("Game Over!!!", new Font("Helvetica", 1, 46),
new FontRenderContext(null, false, false));
textAt.translate(0, (float)textTl.getBounds().getHeight());
shape= textTl.getOutline(textAt);
}
public void paint(Graphics g)
{
Graphics2D g2 = (Graphics2D) g;
Dimension d = getSize();
w = d.width;
h = d.height;

at.setToIdentity();
at.translate(w/2, h/2);
at.rotate(Math.toRadians(315)); //315 = -45 degrees angle as 0=360

Rectangle rect = shape.getBounds();

at.translate(-(rect.width/2), -(rect.height/2));

g2.setStroke(new BasicStroke(2.0f));
g2.transform(at);
g2.setColor(Color.cyan);
g2.fill(shape);
g2.setColor(Color.darkGray);
g2.draw(shape);
}
}
Jul 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.