完整代碼如下:
創(chuàng)新互聯(lián)-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價(jià)比濱江網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式濱江網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋濱江地區(qū)。費(fèi)用合理售后完善,10多年實(shí)體公司更值得信賴。
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);
}
//下面的是關(guān)鍵的繪圖代碼
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();
}
}
主要是用了幾個(gè)java的畫圖函數(shù),如果有用的話,希望采納
#首先在D盤寫一個(gè)文件"temp.html",如下內(nèi)容
!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
html
head
title圖片轉(zhuǎn)文本/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盤放一個(gè)圖片(放小一點(diǎn)的)"a.jpg"
#運(yùn)行如下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 {
/** 此處設(shè)置灰度字符,此處只用十個(gè)字符,可以設(shè)置更多 */
private static char[] cs = new char[] { '.', ',', '*', '+', '=', '', '$', '@', '#', ' ' };
public static void main(String[] args) throws IOException {
// 讀取圖片
BufferedImage bfedimage = ImageIO.read(new File("D:\\a.jpg"));
// 圖片轉(zhuǎn)字符串后的數(shù)組
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();
// 開始拼接內(nèi)容
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();
}
/**
* 讀文件(使用默認(rèn)編碼)
*
* @param file
* @return 文件內(nèi)容
* @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"文件看效果吧。
有什么問題可以聯(lián)系我。
就是一個(gè)制作好的圖片、在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的設(shè)置給覆蓋了;要是先添加畫圖再添加
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);
}
}
運(yùn)行結(jié)果如下:
按照你的要求編寫的Java程序如下(abs()是求絕對(duì)值的函數(shù))
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復(fù)寫一些方法就能顯示,但現(xiàn)在你編的是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)); // 設(shè)置線條寬度,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)); // 設(shè)置線條寬度,3.0即線的寬度
a2d.drawOval(x, y, width, height);
}
}
網(wǎng)頁標(biāo)題:用java代碼畫出小黃人 用pycharm畫小黃人的代碼
URL分享:http://vcdvsql.cn/article38/hehgsp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站建設(shè)、響應(yīng)式網(wǎng)站、App開發(fā)、搜索引擎優(yōu)化、關(guān)鍵詞優(yōu)化、做網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)