在java的image图片类中画线怎么画
详细:有一个方法drawLineOnImage(Imageimage){<--在该图片上画一条线-->本人代码如下:Graphics2Dg=Graphics2Dimage...
详细:有一个方法drawLineOnImage(Image image){
<--在该图片上画一条线-->
本人代码如下:
Graphics2D g = Graphics2Dimage.getGraphics();
g.drawLine(0,0,100,100);
}
出错,求解决方案。我知道错了,怎么纠正 展开
<--在该图片上画一条线-->
本人代码如下:
Graphics2D g = Graphics2Dimage.getGraphics();
g.drawLine(0,0,100,100);
}
出错,求解决方案。我知道错了,怎么纠正 展开
4个回答
展开全部
import java.awt.*;
class MyCanvas extends Canvas{
public void paint(Graphics g){
g.setFont(new Font("华文行楷",Font.PLAIN,20));
g.setColor(Color.RED);
g.drawString("绘图测试!",20,20);
g.drawLine(30,60,100,60);
g.setColor(Color.BLUE);
g.draw3DRect(60,50,70,30,false);
g.setColor(Color.BLUE);
g.fillArc(0,60,65,65,40,80);
g.setFont(new Font("华文彩云",Font.PLAIN,30));
g.setColor(Color.PINK);
g.drawString("彩云之南",20,120);
}
}
public class AWTDrawing {
public static void main( String args[]) {
Frame f = new Frame("AWT绘图");
MyCanvas p = new MyCanvas();
f.add(p);
f.setSize(170,170);
f.setBackground( new Color(160,220,220));
f.setVisible( true);
}
}
效果图(该网站bug315上面还有很多java相关的知识,你可以去学习一下呵呵):
博思aippt
2024-07-20 广告
2024-07-20 广告
博思AIPPT是基于ai制作PPT的智能在线工具,它提供了4种AI制作PPT的方式,包括AI生成大纲、AI直接生成PPT、文本生成PPT、AI提炼文档生成PPT,一站式集成多种AI生成PPT的方式,可满足办公用户的不同需求和使用场景。ai生...
点击进入详情页
本回答由博思aippt提供
展开全部
package com.weifeng.sys.util;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class Image extends HttpServlet {
private static final long serialVersionUID = 1L;
private String sRand;//验证码
//验证码集合
private String[] allchar = {
"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "P", "Q", "R", "S", "T", "U", "V", "W","X", "Y", "Z",
"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "m", "n", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y",
"1", "2", "3", "4", "5", "6", "7", "8", "9"};
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("image/jpeg");
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
HttpSession session = request.getSession();
//在内存中创建图象
int width = 80, height = 25;
//保存图片
BufferedImage image = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);
//保存随即产生的验证码
StringBuffer sb = new StringBuffer();
//随机数生成器
Random r = new Random();
//随机生成四个随即字符
for (int i = 0; i < 4; i++) {
sb.append(allchar[r.nextInt(allchar.length)]);
}
sRand = sb.toString();
//image = new BufferedImage(80, 40, BufferedImage.TYPE_3BYTE_BGR);
//绘图上下文
Graphics g = image.getGraphics();
// 设定背景色
Color bgColor=getRandColor(200,250);
g.fillRect(0, 0, width, height);
// 设定字体
g.setFont(new Font("Times New Roman", Font.PLAIN, 24));
//绘制验证码图片
int yzmWidth = 20, yzmHeight = 24;
for (int i = 0; i < sRand.length(); i++) {
BufferedImage bi = new BufferedImage(yzmWidth, yzmHeight, BufferedImage.TYPE_3BYTE_BGR);
Graphics bg = bi.getGraphics();
bg.setColor(bgColor);
bg.fillRect(0, 0, yzmWidth, yzmHeight);
bg.setColor(new Color(yzmHeight + r.nextInt(110), yzmHeight + r.nextInt(110), yzmHeight + r.nextInt(110)));
bg.setFont(new Font("", r.nextInt(4) + 1, r.nextInt(5) + 16));
g.setColor(new Color(yzmHeight + r.nextInt(110), yzmHeight + r.nextInt(110), yzmHeight + r.nextInt(110)));// 调用函数出来的颜色相同,可能是因为种子太接近,所以只能直接生成
bg.drawString(sRand.substring(i, i + 1), 2, yzmWidth);
g.drawImage(bi, 0 + yzmWidth * i, 0, yzmWidth, yzmHeight, null);
}
//干扰线
// int grxHeight = 25;
// for (int j = 0; j <50; j++) {
// int x = r.nextInt(40);
// int y = r.nextInt(grxHeight);
// int xl = r.nextInt(80);
// int yl = r.nextInt(grxHeight);
// g.setColor(new Color(10 + r.nextInt(90), 10 + r.nextInt(90), 10 + r.nextInt(90),r.nextInt(80)));
// g.drawLine(x, y, xl, yl);
// }
//干扰线
// for (int j = 0; j <100; j++) {
// int x = r.nextInt(60);
// int y = r.nextInt(grxHeight);
// g.setColor(new Color(10 + r.nextInt(90), 10 + r.nextInt(90), 10 + r.nextInt(90),100));
// g.drawLine(x, y, x, y);
// }
//画边框
g.setColor(new Color(1));
g.drawRect(0,0,width-1,height-1);
// 将认证码存入SESSION
session.setAttribute("rand", sRand);
// 图象生效
g.dispose();
ServletOutputStream responseOutputStream = response.getOutputStream();
// 输出图象到页面
ImageIO.write(image, "JPEG", responseOutputStream);
// 以下关闭输入流!
responseOutputStream.flush();
responseOutputStream.close();
}
// 给定范围获得随机颜色
Color getRandColor(int fc, int bc) {
Random random = new Random();
if (fc > 255) fc = 255;
if (bc > 255) bc = 255;
int r = fc + random.nextInt(bc - fc);
int g = fc + random.nextInt(bc - fc);
int b = fc + random.nextInt(bc - fc);
return new Color(r, g, b);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
processRequest(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
processRequest(request, response);
}
public String getServletInfo() {
return "Short description";
}
}
中间屏蔽的那段就是画线操作
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
BufferedImage bi = new BufferedImage(image.getWidth(),image.getHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = bi.createGraphics();
g2d.drawImage(image,0,0,null);
g2d.drawLine(0,0,100,100);
g2d.dispose();
return bi;
追问
image.getWidth(),方法不存在吧
追答
忘了传参数了。
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
image.getGraphics().drawLine(x1, y1, x2, y2);
x1,y1 和 x2,y2是线条的两个顶点坐标
x1,y1 和 x2,y2是线条的两个顶点坐标
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |