<%@ page contentType="text/html; charset=GBK" %><%@page import="java.io.*"%>
<%@page import="javax.servlet.*"%>
<%@page import="javax.servlet.http.*"%><%@page import="java.awt.*"%>
<%@page import="java.awt.image.*"%>
<%@page import="java.awt.geom.*"%>
<%@page import="com.sun.image.codec.jpeg.*"%><html>
<head>
<title>
Draw
</title>
</head>
<body bgcolor="#ffffff">
<%
int WIDTH=500;
int HEIGHT=600; try {
response.setContentType("image/jpeg"); ServletOutputStream out1 = response.getOutputStream(); // create a proper size Frame object
BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB); Graphics2D g2 = image.createGraphics(); // draw the graph onto the image
g2.setColor(Color.white);
g2.fillRect(0,0,WIDTH,HEIGHT);
g2.setColor(Color.red);
g2.drawRect(0,0,WIDTH-1,HEIGHT-1); float x,y;
double a; g2.setColor(Color.red); g2.translate(200,200);
GeneralPath path=new GeneralPath(); for(x=-180;x<=180;x+=1)
{
a=Math.sin(x*Math. PI/180);
y=(float)(40*a);
if(x==-180)
path.moveTo(x,y);
else
path.lineTo(x,y);
} g2.draw(path); g2.setColor(Color.blue);
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON); GeneralPath path2=new GeneralPath(); for(x=-180;x<=180;x+=1)
{
a=Math.sin(x*Math. PI/180);
y=(float)(40*a);
if(x==-180)
path2.moveTo(x,y);
else
path2.lineTo(x,y);
}
float[] dashPattern= { 10, 10, 10, 10, 10, 10, 30, 10, 30};
BasicStroke stroke=new BasicStroke(10.0f,BasicStroke.CAP_BUTT,
BasicStroke.JOIN_MITER, 10.0F /* miter limit */,
dashPattern, 0 /* dash phase */
);
g2.setStroke(stroke); g2.rotate(-Math.PI/2);
g2.draw(path2);
g2.rotate(Math.PI/2);
g2.draw(path); g2.drawString("This is the Test!",100,100); g2.rotate(-Math.PI/2,100,100);
g2.drawString("This is the Test!",100,100);
g2.rotate(Math.PI/2,100,100); /*
File f=new File("E:\\MyProject\\Java\\jbproject\\Standard\\test.jpg");
BufferedImage image1 = javax.imageio.ImageIO.read(f);
g2.drawImage(image1,null,0,0); int rule = AlphaComposite.SRC_OVER;
float alpha = 0.5f;
g2.setComposite(AlphaComposite.getInstance(rule, alpha)); File f2=new File("E:\\MyProject\\Java\\jbproject\\Standard\\test2.jpg");
BufferedImage image2 = javax.imageio.ImageIO.read(f2);
g2.drawImage(image2,null,0,0);
*/
// encode the output back to the client
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out1);
encoder.encode(image);
out1.flush(); } catch (Exception e) {
e.printStackTrace();
} %></body>
</html>
<%@page import="javax.servlet.*"%>
<%@page import="javax.servlet.http.*"%><%@page import="java.awt.*"%>
<%@page import="java.awt.image.*"%>
<%@page import="java.awt.geom.*"%>
<%@page import="com.sun.image.codec.jpeg.*"%><html>
<head>
<title>
Draw
</title>
</head>
<body bgcolor="#ffffff">
<%
int WIDTH=500;
int HEIGHT=600; try {
response.setContentType("image/jpeg"); ServletOutputStream out1 = response.getOutputStream(); // create a proper size Frame object
BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB); Graphics2D g2 = image.createGraphics(); // draw the graph onto the image
g2.setColor(Color.white);
g2.fillRect(0,0,WIDTH,HEIGHT);
g2.setColor(Color.red);
g2.drawRect(0,0,WIDTH-1,HEIGHT-1); float x,y;
double a; g2.setColor(Color.red); g2.translate(200,200);
GeneralPath path=new GeneralPath(); for(x=-180;x<=180;x+=1)
{
a=Math.sin(x*Math. PI/180);
y=(float)(40*a);
if(x==-180)
path.moveTo(x,y);
else
path.lineTo(x,y);
} g2.draw(path); g2.setColor(Color.blue);
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON); GeneralPath path2=new GeneralPath(); for(x=-180;x<=180;x+=1)
{
a=Math.sin(x*Math. PI/180);
y=(float)(40*a);
if(x==-180)
path2.moveTo(x,y);
else
path2.lineTo(x,y);
}
float[] dashPattern= { 10, 10, 10, 10, 10, 10, 30, 10, 30};
BasicStroke stroke=new BasicStroke(10.0f,BasicStroke.CAP_BUTT,
BasicStroke.JOIN_MITER, 10.0F /* miter limit */,
dashPattern, 0 /* dash phase */
);
g2.setStroke(stroke); g2.rotate(-Math.PI/2);
g2.draw(path2);
g2.rotate(Math.PI/2);
g2.draw(path); g2.drawString("This is the Test!",100,100); g2.rotate(-Math.PI/2,100,100);
g2.drawString("This is the Test!",100,100);
g2.rotate(Math.PI/2,100,100); /*
File f=new File("E:\\MyProject\\Java\\jbproject\\Standard\\test.jpg");
BufferedImage image1 = javax.imageio.ImageIO.read(f);
g2.drawImage(image1,null,0,0); int rule = AlphaComposite.SRC_OVER;
float alpha = 0.5f;
g2.setComposite(AlphaComposite.getInstance(rule, alpha)); File f2=new File("E:\\MyProject\\Java\\jbproject\\Standard\\test2.jpg");
BufferedImage image2 = javax.imageio.ImageIO.read(f2);
g2.drawImage(image2,null,0,0);
*/
// encode the output back to the client
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out1);
encoder.encode(image);
out1.flush(); } catch (Exception e) {
e.printStackTrace();
} %></body>
</html>
java.awt.Graphics;
java.awt.Graphics2D;
java.awt.Geom.*;(里面都是2D图形类,包括矩形,直线,多边形,椭圆等)先建一个JFrame和JPanel,在JPanel里面重载paint() 或者paintComponent()方法,再调用Graphics2D的draw(Shape s)应该就可以画了,具体实现不难,很多书上都有啊,想Core java
卷1,2 至于生成jpg,忘了,查查书再说吧,偶是新手。
可以把中间对Graphics的很多操作删除掉,那些有的是画正弦函数和字符串旋转灯的操作,没有用的。原理就是生成Jpg文件,然后传到客户端。