完整代码如下:
创新互联-专业网站定制、快速模板网站建设、高性价比滨江网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式滨江网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖滨江地区。费用合理售后完善,10多年实体公司更值得信赖。
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Toolkit;
import javax.swing.JFrame;
public class Face extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
public Face(){
setSize(500, 500);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
Dimension screenSize = Toolkit.getDefaultToolkit()
.getScreenSize();
Dimension frameSize = getSize();
setLocation((screenSize.width - frameSize.width) / 2,
(screenSize.height - frameSize.height) / 2);
setVisible(true);
}
//下面的是关键的绘图代码
public void paint(Graphics g){
//画头
g.drawOval(100, 50, 300, 400);
//画眼睛
g.drawOval(140, 150, 100, 50);
g.drawOval(260, 150, 100, 50);
//画鼻子
g.drawArc(140, 150, 100, 150, -90, 90);
g.drawArc(260, 150, 100, 150, 180, 90);
//画嘴巴
g.drawOval(170, 320, 150, 50);
}
public static void main(String args[]){
new Face();
}
}
主要是用了几个java的画图函数,如果有用的话,希望采纳
#首先在D盘写一个文件"temp.html",如下内容
!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
html
head
title图片转文本/title
meta http-equiv="content-type" content="text/html; charset=gbk"
style type="text/css"
body {
font-family: 宋体; line-height: 0.8em; letter-spacing: 0px; font-size: 8px;
}
/style
/head
body
${content}
/body
/html
#在D盘放一个图片(放小一点的)"a.jpg"
#运行如下JAVA代码:
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import javax.imageio.ImageIO;
public class Test {
/** 此处设置灰度字符,此处只用十个字符,可以设置更多 */
private static char[] cs = new char[] { '.', ',', '*', '+', '=', '', '$', '@', '#', ' ' };
public static void main(String[] args) throws IOException {
// 读取图片
BufferedImage bfedimage = ImageIO.read(new File("D:\\a.jpg"));
// 图片转字符串后的数组
char[][] css = new char[bfedimage.getWidth()][bfedimage.getHeight()];
for (int x = 0; x bfedimage.getWidth(); x++) {
for (int y = 0; y bfedimage.getHeight(); y++) {
int rgb = bfedimage.getRGB(x, y);
Color c = new Color(rgb);
// 得到灰度值
int cc = (c.getRed() + c.getGreen() + c.getBlue()) / 3;
css[x][y] = cs[(int) ((cc * 10 - 1) / 255)];
}
}
// 取得模板HTML
String temp = readFile(new File("D:\\temp.html"),"gbk");
StringBuffer sb = new StringBuffer();
// 开始拼接内容
for (int y = 0; y css[0].length; y++) {
for (int x = 0; x css.length; x++) {
sb.append(css[x][y]);
}
sb.append("\r\n");
}
System.out.println(sb.toString());
// 生成文件
String content = toHTML(sb.toString());
String filecontent = replaceStrAllNotBack(temp, "${content}", content);
writeFile(new File("D:\\content.html"), filecontent, "gbk");
}
public static String toHTML(String s) {
s = s.replaceAll("", "");
s = s.replaceAll(" ", " ");
s = s.replaceAll("", "");
s = s.replaceAll("", "");
s = s.replaceAll("\"", """);
s = s.replaceAll("\\\r\\\n", "br/");
s = s.replaceAll("\\\r", "br/");
s = s.replaceAll("\\\n", "br/");
return s;
}
public static String replaceStrAllNotBack(String str, String strSrc, String strDes) {
StringBuffer sb = new StringBuffer(str);
int index = 0;
while ((index = sb.indexOf(strSrc, index)) != -1) {
sb.replace(index, index + strSrc.length(), strDes);
index += strDes.length();
}
return sb.toString();
}
/**
* 读文件(使用默认编码)
*
* @param file
* @return 文件内容
* @throws IOException
*/
public static String readFile(File file, String charset) throws IOException {
InputStreamReader fr = new InputStreamReader(new FileInputStream(file), charset);
StringBuffer sb = new StringBuffer();
char[] bs = new char[1024];
int i = 0;
while ((i = fr.read(bs)) != -1) {
sb.append(bs, 0, i);
}
fr.close();
return sb.toString();
}
/**
* 写文件
*
* @param file
* @param string
* 字符串
* @param encoding
* 编码
* @return 文件大小
* @throws IOException
*/
public static int writeFile(File file, String string, String encoding) throws IOException {
FileOutputStream fos = new FileOutputStream(file);
try {
byte[] bs = string.getBytes(encoding);
fos.write(bs);
return bs.length;
} finally {
fos.close();
}
}
}
#打开"D:\content.html"文件看效果吧。
有什么问题可以联系我。
就是一个制作好的图片、在java里直接使用。
Action saveAs = new AbstractAction("Save As ..."){
{putValue(Action.SMALL_ICON, new ImageIcon(...));}
@Override public void actionPerformed(ActionEvent e){
...
}
};
menu.add(saveAs);
toolbar.add(saveAs);
java在JFrame上画东西,主要是使用paint方法,代码如下:
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Draw extends JFrame{
JPanel jPanel=new JPanel();
public Draw() {
jPanel.setBackground(Color.red);
add(jPanel);
Drawation drawaction=new Drawation();//添加画图,把上面jpanel的设置给覆盖了;要是先添加画图再添加
add(drawaction); //jpanel则把画图覆盖了
}
public static void main(String[] args){
Draw draw=new Draw();
draw.setTitle("abc");
draw.setSize(300,300);
draw.setVisible(true);
draw.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
class Drawation extends JPanel{
public void paintComponent(Graphics g){
super.paintComponents(g);
g.drawString("agagh", 50, 45);
}
}
运行结果如下:
按照你的要求编写的Java程序如下(abs()是求绝对值的函数)
public class AA {
public static void main(String[] args) {
int N=4;
for(int i=1;i=2*N-1;i++){
for(int j=1;j=2*N-1-2*Math.abs(N-i);j++){
System.out.print("*");
}
System.out.println();
}
}
}
帮你改了一下。
你要画在panel上,然后frame.add(panel)就能显示。
是不是和applet搞混了,applet复写一些方法就能显示,但现在你编的是java gui
import java.awt.*;
import java.awt.Event.*;
import javax.swing.*; //import javax.swing.Timer;
import java.awt.BasicStroke;
//import java.util.Date;
//import java.text.*;
//import java.util.*;
public class TestGui {
public void paint(Graphics g) {
Graphics2D a2d = (Graphics2D) g;
int x = 120, y = 90, width = 150, height = 150;
a2d.setColor(Color.red);
a2d.setStroke(new BasicStroke(3.0f)); // 设置线条宽度,3.0即线的宽度
a2d.drawOval(x, y, width, height);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
// frame.add(new paint(),BorderLayout.CENTER);
frame.setSize(500, 500);
frame.setLocation(200, 200);
frame.setVisible(true);
Panel p = new Panel();
frame.add(p);
// frame.paint(null);
// TODO code application logic here
}
}
class Panel extends JPanel {
// 重新覆盖paint方法
public void paint(Graphics g) {
super.paint(g);
Graphics2D a2d = (Graphics2D) g;
int x = 120, y = 90, width = 150, height = 150;
a2d.setColor(Color.red);
a2d.setStroke(new BasicStroke(3.0f)); // 设置线条宽度,3.0即线的宽度
a2d.drawOval(x, y, width, height);
}
}