How to convert JPanel code into JApplet?
i made a graph in JPanel.now i want to call that graph in my jsp page.
for this purpose i need to convert that JPanel graph into JApplet.
does anyone knows about this...
regards
i made a graph in JPanel.now i want to call that graph in my jsp page.
for this purpose i need to convert that JPanel graph into JApplet.
does anyone knows about this...
regards
Or maybe better (if the graph is static): write a Servlet that generates an image instead of drawing on a JPanel. (Should not be too hard to rewrite. The drawing code should stay the same.) Then call that Servlet from an IMG-tag from within the JSP-page.
-Puce
thnxs for ur nice suggestions..but i dont know much about servlets...does there any other solution to this problem...
Message was edited by:
FaiyazAziz
You're off to a good start by designing you UI on a JPanel, good move! You're now free to use that on a JApplet or a JFrame. Simply instantiate your component and add it to a JApplet
generate an image from your graphic then embed it into your html code <img src="...">
example:
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.GradientPaint;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class WriteImageType {
static public void main(String args[]) throws Exception {
try {
int width = 200, height = 200;
// TYPE_INT_ARGB specifies the image format: 8-bit RGBA packed
// into integer pixels
BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D ig2 = bi.createGraphics();
Font font = new Font("TimesRoman", Font.BOLD, 20);
ig2.setFont(font);
String message = "www.java2s.com!";
FontMetrics fontMetrics = ig2.getFontMetrics();
int stringWidth = fontMetrics.stringWidth(message);
int stringHeight = fontMetrics.getAscent();
ig2.setPaint(Color.black);
ig2.drawString(message, (width - stringWidth) / 2, height / 2 + stringHeight / 4);
ImageIO.write(bi, "PNG", new File("c:\\yourImageName.PNG"));
ImageIO.write(bi, "JPEG", new File("c:\\yourImageName.JPG"));
ImageIO.write(bi, "gif", new File("c:\\yourImageName.GIF"));
ImageIO.write(bi, "BMP", new File("c:\\yourImageName.BMP"));
} catch (IOException ie) {
ie.printStackTrace();
}
}
}
> thnxs for ur nice suggestions..but i dont know much
> about servlets...does there any other solution to
> this problem...
>
Hmm, well a JSP-page is based on Servlet technology, that's why I suggest that solution. Maybe it's a good time to grab a book and learn about it, since you have a simple real task. It should not be that hard. Just make sure you set the content type correctly (eg. something like "image/gif").
Otherwise just put your panel to an JApplet and embed it in your JSP-page:
http://java.sun.com/docs/books/tutorial/uiswing/components/applet.html
-Puce