import java.awt.*;
創新互聯服務項目包括雙灤網站建設、雙灤網站制作、雙灤網頁制作以及雙灤網絡營銷策劃等。多年來,我們專注于互聯網行業,利用自身積累的技術優勢、行業經驗、深度合作伙伴關系等,向廣大中小型企業、政府機構等提供互聯網行業的解決方案,雙灤網站推廣取得了明顯的社會效益與經濟效益。目前,我們服務的客戶以成都為中心已經輻射到雙灤省份的部分城市,未來相信會繼續擴大服務區域并繼續獲得客戶的支持與信任!
import java.applet.*;
import java.awt.event.*;
class MyCanvas extends Canvas
{
int x,y,r,n;
int x0,y0;
MyCanvas()
{
setSize(圓心位置,圓心位置);
setBackground(Color.red);
}
public void setX(int x)
{
this.x=x;
}
public void setY(int y)
{
this.y=y;
}
public void setR(int r)
{
this.r=r;
}
public void setN(int n)
{
this.n=n;
}
public void paint(Graphics g1)
{
for(int i=0;i=360;i=i+360/n)
{
x0 = (int)(x+r*Math.cos(i));
y0 = (int)(y+r*Math.sin(i));
g1.drawString("*",x0,y0);}
}
}
public class e1 extends Applet implements ActionListener
{
MyCanvas canvas;
TextField inputR,inputX,inputY,inputN;
Label label1,label2,label3;
Button b1,b2;
public void init()
{
canvas = new MyCanvas();
inputR = new TextField(6);
inputX = new TextField(6);
inputY = new TextField(6);
inputN = new TextField(6);
b1 = new Button("確定");
b1.addActionListener(this);
label1 = new Label("輸入位置坐標:");
label2 = new Label("輸入半徑:");
label3 = new Label("輸入要打印的*數:");
add(label1);
add(inputX);
add(inputY);
add(label2);
add(inputR);
add(label3);
add(inputN);
add(b1);
add(canvas);
}
public void actionPerformed(ActionEvent e)
{
int x=0,y=0,n=0,r=0;
try
{
x=Integer.valueOf(inputX.getText()).intValue();
y=Integer.valueOf(inputY.getText()).intValue();
n=Integer.valueOf(inputN.getText()).intValue();
r=Integer.valueOf(inputR.getText()).intValue();
canvas.setX(x);
canvas.setY(y);
canvas.setR(r);
canvas.setN(n);
canvas.repaint();
}
catch(NumberFormatException ee)
{
x = 0;
y = 0;
r = 0;
n = 0;
}
}
}
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
class MyCanvas extends Canvas
{
int x,y,r,n;
int x0,y0;
MyCanvas()
{
setSize(100,100);
setBackground(Color.red);
}
public void setX(int x)
{
this.x=x;
}
public void setY(int y)
{
this.y=y;
}
public void setR(int r)
{
this.r=r;
}
public void setN(int n)
{
this.n=n;
}
public void paint(Graphics g1)
{
for(int i=0;i=360;i=i+360/n)
{
x0 = (int)(x+r*Math.cos(i));
y0 = (int)(y+r*Math.sin(i));
g1.drawString("*",x0,y0);}
}
}
public class e1 extends Applet implements ActionListener
{
MyCanvas canvas;
TextField inputR,inputX,inputY,inputN;
Label label1,label2,label3;
Button b1,b2;
public void init()
{
canvas = new MyCanvas();
inputR = new TextField(6);
inputX = new TextField(6);
inputY = new TextField(6);
inputN = new TextField(6);
b1 = new Button("確定");
b1.addActionListener(this);
label1 = new Label("輸入位置坐標:");
label2 = new Label("輸入半徑:");
label3 = new Label("輸入要打印的*數:");
add(label1);
add(inputX);
add(inputY);
add(label2);
add(inputR);
add(label3);
add(inputN);
add(b1);
add(canvas);
}
public void actionPerformed(ActionEvent e)
{
int x=0,y=0,n=0,r=0;
try
{
x=Integer.valueOf(inputX.getText()).intValue();
y=Integer.valueOf(inputY.getText()).intValue();
n=Integer.valueOf(inputN.getText()).intValue();
r=Integer.valueOf(inputR.getText()).intValue();
canvas.setX(x);
canvas.setY(y);
canvas.setR(r);
canvas.setN(n);
canvas.repaint();
}
catch(NumberFormatException ee)
{
x = 0;
y = 0;
r = 0;
n = 0;
}
}
}
public void draw(Graphics2D g) {
g.setColor(color);//設置顏色
g.setStroke(stroke);//寬度
int x, y, w, h;
if (startX endX) {//以下的startx 、endx都是由鼠標拖 動事件得到
x = endX;
w = startX - endX;
} else {
x = startX;
w = endX - startX;
}
if (startY endY) {
y = endY;
h = startY - endY;
} else {
y = startY;
h = endY - startY;
}
g.drawOval(x, y, w, h);
}
使用java畫圓要用到繪圖類Graphics,下面是實例代碼和運行效果:
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?自動生成的方法存根
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);
}
}
//?定義一個MyPanel面板,用于繪圖區域
class?MyPanel?extends?JPanel{
//覆蓋JPanel
//?Graphics?是繪圖的重要類,可以理解成一支畫筆
public?void?paint(Graphics?g){
//??1.?調用父類函數完成初始化任務
//??這句話不可以少
super.paint(g);
//?先畫出一個圓圈
g.drawOval(100,?100,?30,?30);
}
}
代碼復制進ide編程工具,運行效果如下:
你先想這道題的思路吧。
首先打印出圓形的話就是在一個60-60這種正方形里面話個圓,你先要知道要畫的點的位置對吧。算出所有的點打印出來。就是這個思路。然后是怎么算的問題了
public?class?SSS?{
public?static?void?main(String[]?args)?{
//半徑
int?r?=?30;
for?(int?y?=?0;?y?=?2?*?r;?y?+=?2)?{
long?x?=?Math.round(r?-?Math.sqrt(2?*?r?*?y?-?y?*?y));
long?longLength?=?2?*?(r?-?x);
for?(int?i?=?0;?i?=?x;?i++)?{
System.out.print('?');
}
System.out.print('*');
for?(int?j?=?0;?j?=?longLength;?j++)?{
System.out.print('?');
}
System.out.println('*');
}
}
}
靠,樓上的回答那么長啊,只要一個函數,就是
drawOval(int
x,int
y,int
w,int
h);
這是是畫橢圓形的函數,但是它也可以畫圓形。
比如
drawOval(100,100,50,50);
就在坐標50,50畫一個直徑100的圓,只要把,最后的2個參數設成一樣就是一個圓。要畫直徑200的話,就把最后2個參數設成200,200
一切OK了
新聞標題:java畫圓粗細代碼 JAVA畫圓
標題URL:http://vcdvsql.cn/article24/hepsce.html
成都網站建設公司_創新互聯,為您提供微信公眾號、品牌網站建設、、小程序開發、面包屑導航、軟件開發
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯