http://www.acme.com/java/software/Acme.JPM.Encoders.GifEncoder.html 에서 다운받은 Acme.tar.gz을 알집이나 윈집을 이용해서 압축을 풉니다. 필요한 파일이, Acme 라는 디렉토리 안에 JPM 디렉토리가 있고 그 안에는 Encoders 라는 디렉토리가 있고 그 안에는 class 파일이 두개 있습니다. Acme 디렉토리를 WEB-INFclasses 아래로 이동합니다. 어딘지 아시죠? 아니면 제가 첨부한 Acme.jar 파일을 c:jdk1.3jrelibext 에 복사시켜 놓아도 됩니다. 이제 설치는 끝났습니다. 소스를 보죠. imageCall.html <html> <body bgcolor="#000000"> <img src="HelloGraphics.jsp"> </body> </html> HelloGraphics.jsp <%@ page import="java.io.*, java.awt.*, Acme.JPM.Encoders.GifEncoder" %><% &nbsr; Frame frame = null; Graphics g = null; try { // Create an unshown frame frame = new Frame(); frame.addNotify(); // Get a graphics region, using the Frame Image image = frame.createImage(200, 300); // image size width 200, height 300 g = image.getGraphics(); g.setFont(new Font("Serif", Font.ITALIC, 50)); g.drawString("JSP", 10, 50); g.drawString("Image!", 10, 80); g.setColor(new Color(0x0080AA)); g.setFont(new Font("Garamond", Font.PLAIN, 18)); g.drawString("www.okjsp.pe.kr", 60, 280); // Encode the off screen image into a GIF and send it to the client response.setContentType("image/gif"); GifEncoder encoder = new GifEncoder(image, response.getOutputStream()); encoder.encode(); } finally { // Clean up resources if (g != null) g.dispose(); if (frame != null) frame.removeNotify(); } %>
HelloGraphics.jsp 파일은 text/html 을 생성해 내는 것이 아니라, image/gif 형식의 파일을 생성해 냅니다. 그래서 img src="HelloGraphics.jsp" 처럼 호출할 수 있는 것이죠. 여기에 관해서 관련자료들을 더 찾아보시기 바랍니다. jpeg 생성하는 라이브러리도 있고, 다양하게 응용할 수 있겠죠. 요즘 유행하는 avatar 도 만들 수 있겠죠. 마지막으로 막대그래프 하나 그리고 끝내도록 하겠습니다. 원리는 이렇습니다. 숫자들을 배열에 놓고 맥시멈 값을 구합니다. 이미지 크기에 맞춰서 비율을 결정합니다. 배열의 length 로 이미지 폭을 나눕니다. 그리고 그려주면 됩니다. 난수를 10 개 만들어서 동적으로 이미지를 생성해보도록 하겠습니다. HelloGraphics.jsp <%@ page import="java.util.*, java.io.*, java.awt.*, Acme.JPM.Encoders.GifEncoder" %><%
// 계산 // 난수 발생 double [] num = new double[10]; for(int i=0;i<num.length; i++) { num[i] = Math.random() * 100; } // 최대값 double max=num[0] ; for(int i=1;i<num.length;i++) { if(max<num[i]) max=num[i]; } int margin = 10; float x_pace = (300-margin*2) / (float)10; float y_height = (float)(200-margin*2); double ratio = y_height / max; Frame frame = null; Graphics g = null;
try { // Create an unshown frame frame = new Frame(); frame.addNotify(); // Get a graphics region, using the Frame Image image = frame.createImage(300, 200); // image size width 300, height 200 g = image.getGraphics(); g.setColor(new Color(0x0040FF)); g.setFont(new Font("System", Font.PLAIN, 11)); g.drawString("www.okjsp.pe.kr", 210, 198); g.drawString("Max:"+max, 10, 198); // draw Bars int xx = 0; int yy = 0; g.setColor(new Color(0x000000)); g.drawRect(0,0,299,199); for (int i=0; i<num.length; i++) { xx = (int)(i * x_pace) + margin; yy = (int)(num[i] * ratio); yy = 200 - (yy + margin); g.drawRect(xx, yy, (int)x_pace-3, 188 - yy); g.drawString(""+(int)num[i], xx+9, yy); } // Encode the off screen image into a GIF and send it to the client response.setContentType("image/gif"); GifEncoder encoder = new GifEncoder(image, response.getOutputStream()); encoder.encode(); } finally { // Clean up resources if (g != null) g.dispose(); if (frame != null) frame.removeNotify(); } %> 서블릿으로 개발하는 것과 크게 차이는 없습니다만 서블릿 소스보다는 부담이 덜 가네요. 아직 내공이 얕기 때문이겠죠. 읽어주셔서 감사합니다. 좋은 하루 되십시오. --------------------------------- http://okjsp.pe.kr Tags: awt image 이미지
|