欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

java畫圖案代碼 java畫圖形

在Java中如何用程序畫一個(gè)圓

使用java畫圓要用到繪圖類Graphics,下面是實(shí)例代碼和運(yùn)行效果:

成都創(chuàng)新互聯(lián)公司是一家集網(wǎng)站建設(shè),南陽企業(yè)網(wǎng)站建設(shè),南陽品牌網(wǎng)站建設(shè),網(wǎng)站定制,南陽網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營(yíng)銷,網(wǎng)絡(luò)優(yōu)化,南陽網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競(jìng)爭(zhēng)力。可充分滿足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶成長(zhǎng)自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。

package?com.dikea.demo01;

import?java.awt.*;

import?javax.swing.*;

//?java繪圖原理

public?class?demo_01??extends?JFrame?{

MyPanel?mp?=?null;

public?static?void?main(String[]?args)?{

//?TODO?自動(dòng)生成的方法存根

demo_01?demo01?=?new?demo_01();

}

public?demo_01(){

mp?=?new?MyPanel();

this.add(mp);

this.setSize(400,?300);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

this.setVisible(true);

}

}

//?定義一個(gè)MyPanel面板,用于繪圖區(qū)域

class?MyPanel?extends?JPanel{

//覆蓋JPanel

//?Graphics?是繪圖的重要類,可以理解成一支畫筆

public?void?paint(Graphics?g){

//??1.?調(diào)用父類函數(shù)完成初始化任務(wù)

//??這句話不可以少

super.paint(g);

//?先畫出一個(gè)圓圈

g.drawOval(100,?100,?30,?30);

}

}

代碼復(fù)制進(jìn)ide編程工具,運(yùn)行效果如下:

如何用Java編寫一個(gè)繪制圖形的小程序?

import java.awt.*;

import java.awt.event.*;

import java.awt.geom.*;

import javax.swing.*;

//不規(guī)則圖形的繪制

public class IrregularShapeDemo extends JFrame {

GeneralPath gPath= new GeneralPath(); //GeneralPath對(duì)象實(shí)例

Point aPoint;

//構(gòu)造函數(shù)

public IrregularShapeDemo() {

super("不規(guī)則圖形的繪制"); //調(diào)用父類構(gòu)造函數(shù)

enableEvents(AWTEvent.MOUSE_EVENT_MASK|AWTEvent.MOUSE_MOTION_EVENT_MASK); //允許事件

setSize(300, 200); //設(shè)置窗口尺寸

setVisible(true); //設(shè)置窗口可視

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //關(guān)閉窗口時(shí)退出程序

}

public void paint(Graphics g) { //重載窗口組件的paint()方法

Graphics2D g2D = (Graphics2D)g;//獲取圖形環(huán)境

g2D.draw(gPath); //繪制路徑

}

public static void main(String[] args) {

new IrregularShapeDemo();

}

protected void processMouseEvent(MouseEvent e) { //鼠標(biāo)事件處理

if(e.getID() == MouseEvent.MOUSE_PRESSED) {

aPoint = e.getPoint(); //得到當(dāng)前鼠標(biāo)點(diǎn)

gPath = new GeneralPath(); //重新實(shí)例化GeneralPath對(duì)象

gPath.moveTo(aPoint.x,aPoint.y); //設(shè)置路徑點(diǎn)

}

}

protected void processMouseMotionEvent(MouseEvent e) { //鼠標(biāo)運(yùn)動(dòng)事件處理

if(e.getID() == MouseEvent.MOUSE_DRAGGED) {

aPoint = e.getPoint(); //得到當(dāng)前鼠標(biāo)點(diǎn)

gPath.lineTo(aPoint.x, aPoint.y); //設(shè)置路徑

gPath.moveTo(aPoint.x, aPoint.y);

repaint(); //重繪組件

}

}

}

用Java 做一個(gè)星星圖案

這段代碼你參考一下。可以運(yùn)行的package common;public class test {

public static void main(String[] args){

Pentagram pen = new Pentagram(10,0,' ','★');

Draw.printCanvas(pen.getPentagram());

}

}// Pentagram.java 五角星類

class Pentagram {

private final char FILL_CHAR; // 填充字符

private final char SPACE_CHAR; // 空檔字符

private final int R; // 五角星的外接圓半徑

private final float ROTATION; // 五角星逆時(shí)針旋轉(zhuǎn)角度

private final int X; // 用于生成畫圖數(shù)組

private final int Y; // 用于生成畫圖數(shù)組 /**

* 構(gòu)造一個(gè)Pentagram對(duì)象

*

* @param radius

* 五角星的半徑

* @param rotation

* 五角星的逆時(shí)針旋轉(zhuǎn)度數(shù)

* @param spaceChar

* 畫布上空白處填充字符

* @param fillChar

* 畫布上線條部分填充字符

*/

public Pentagram(int radius, float rotation, char spaceChar, char fillChar) {

this.R = radius;

this.ROTATION = rotation;

this.FILL_CHAR = fillChar;

this.SPACE_CHAR = spaceChar;

this.X = 2 * R + 1;

this.Y = 2 * R + 1;

} public char[][] getPentagram() {

char[][] canvas = initCanvas();

Draw draw = new Draw(FILL_CHAR);

// 設(shè)五角星的最右邊的一個(gè)點(diǎn)為 A,逆時(shí)針選取點(diǎn) B~E

// 通過圓的極坐標(biāo)公式可以得出:

// 得出以下各點(diǎn)的坐標(biāo)

// A 點(diǎn)坐標(biāo)(0.951R, 0.309R)

// B 點(diǎn)坐標(biāo)(0, R)

// C 點(diǎn)坐標(biāo)(-0.951R, 0.309R)

// D 點(diǎn)坐標(biāo)(-0.588R, -0.809R)

// E 點(diǎn)坐標(biāo)(0.588R, -0.809R)

// 畫線段CA

draw.drawLine(mcos(162) * R, msin(162) * R, mcos(18) * R, msin(18) * R,

canvas);

// 畫線段DA

draw.drawLine(mcos(234) * R, msin(234) * R, mcos(18) * R, msin(18) * R,

canvas);

// 畫線段CE

draw.drawLine(mcos(162) * R, msin(162) * R, mcos(306) * R, msin(306)

* R, canvas);

// 畫線段DB

draw.drawLine(mcos(234) * R, msin(234) * R, mcos(90) * R, msin(90) * R,

canvas);

// 畫線段BE

draw.drawLine(mcos(90) * R, msin(90) * R, mcos(306) * R, msin(306) * R,

canvas);

return canvas;

} // 在方形的字符數(shù)組中指定兩點(diǎn)畫線條

// 對(duì)圖形數(shù)組進(jìn)行初始化,填充空格

private char[][] initCanvas() {

char[][] canvas = new char[Y][X];

for (int i = 0; i Y; i++) {

for (int j = 0; j X; j++) {

canvas[i][j] = SPACE_CHAR;

}

}

return canvas;

} // 根據(jù)角度求正弦值,保留兩位小數(shù)

private double msin(float a) {

return ((int) (Math.sin(Math.toRadians(a + ROTATION)) * 100)) / 100.0;

} // 根據(jù)角度求余弦值,保留兩位小數(shù)

private double mcos(float a) {

return ((int) (Math.cos(Math.toRadians(a + ROTATION)) * 100)) / 100.0;

}

}// Draw.java 畫圖工具類

class Draw {

private char fillChar; public Draw(char fillChar) {

this.fillChar = fillChar;

} /**

* 根據(jù)兩個(gè)點(diǎn)畫線在二維字符數(shù)組上畫線

*

* @param x1

* @param y1

* @param x2

* @param y2

* @param canvas

*/

public void drawLine(double x1, double y1, double x2, double y2,

char[][] canvas) {

int radius = (canvas.length - 1) / 2;

// 從 x 方向進(jìn)行填充

if (x1 x2) {

double t = x1;

x1 = x2;

x2 = t;

t = y1;

y1 = y2;

y2 = t;

}

// 獲得直線方程的兩個(gè)系數(shù)

double a = (y1 - y2) / (x1 - x2);

double b = y1 - a * x1;

// 根據(jù) x 方向的值求出 y 值,并填充圖形

for (int i = (int) Math.round(x1); i = (int) Math.round(x2); i++) {

// 根據(jù)直線方程 y = ax + b,求 y

int y = (int) Math.round(a * i + b);

// 因?yàn)?y 和 i 算出來的結(jié)果有可能是負(fù)數(shù),

// 為了采用數(shù)組來表示坐標(biāo),做了以下變換

// c[R][R] 即為坐標(biāo)原點(diǎn)

// c[R][0..R] 為 x 方向的負(fù)半軸

// c[R][R+1..2*R] 為 x 方向的正半軸

// c[0..R][R] 為 y 方向的正半軸

// c[R+1..2*R][R] 為 y 方向的負(fù)半軸

int yy = radius - y;

int xx = radius + i;

yy = yy 0 ? 0 : yy;

yy = yy 2 * radius ? 2 * radius : yy;

xx = xx 0 ? 0 : xx;

xx = xx 2 * radius ? 2 * radius : xx;

canvas[yy][xx] = fillChar;

}

// 從 y 方向進(jìn)行填充,便于減少間距問題產(chǎn)生的字符空檔

if (y1 y2) {

double t = x1;

x1 = x2;

x2 = t;

t = y1;

y1 = y2;

y2 = t;

}

// 根據(jù) y 方向的值求出 x 值,并填充圖形

for (int i = (int) Math.round(y1); i = (int) Math.round(y2); i++) {

// 根據(jù) x = (y - b) / a,求 x

int y = (int) Math.round((i - b) / a);

int yy = radius - i;

int xx = radius + y;

yy = yy 0 ? 0 : yy;

yy = yy 2 * radius ? 2 * radius : yy;

xx = xx 0 ? 0 : xx;

xx = xx 2 * radius ? 2 * radius : xx;

canvas[yy][xx] = fillChar;

}

} /**

* 將畫完圖之后的畫布輸出到控制臺(tái)上

*

* @param canvas

*/

public static void printCanvas(char[][] canvas){

for (int i = 0; i canvas.length; i++) {

for (int j = 0; j canvas[i].length; j++) {

System.out.print(canvas[i][j]);

}

System.out.println();

}

}

}

分享名稱:java畫圖案代碼 java畫圖形
本文路徑:http://www.chinadenli.net/article14/dogside.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站建設(shè)響應(yīng)式網(wǎng)站網(wǎng)站內(nèi)鏈網(wǎng)站營(yíng)銷定制網(wǎng)站App開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)頁設(shè)計(jì)公司