新聞中心
請(qǐng)寫出用java代碼畫一個(gè)圓
靠,樓上的回答那么長(zhǎng)啊,只要一個(gè)函數(shù),就是
我們提供的服務(wù)有:成都網(wǎng)站設(shè)計(jì)、做網(wǎng)站、成都外貿(mào)網(wǎng)站建設(shè)公司、微信公眾號(hào)開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、濱州ssl等。為上1000家企事業(yè)單位解決了網(wǎng)站和推廣的問(wèn)題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的濱州網(wǎng)站制作公司
drawOval(int
x,int
y,int
w,int
h);
這是是畫橢圓形的函數(shù),但是它也可以畫圓形。
比如
drawOval(100,100,50,50);
就在坐標(biāo)50,50畫一個(gè)直徑100的圓,只要把,最后的2個(gè)參數(shù)設(shè)成一樣就是一個(gè)圓。要畫直徑200的話,就把最后2個(gè)參數(shù)設(shè)成200,200
一切OK了
JAVA畫圓
import java.awt.Frame;
import java.awt.Graphics;
public class S extends Frame{
private int x;
private int y;
private boolean drawOval;//為true時(shí)繪制
//測(cè)試入口函數(shù)
public static void main(String []args)
{
new S().print();
}
//構(gòu)造函數(shù),初始化x、y坐標(biāo),設(shè)置drawOval變量為false,設(shè)置窗體大小
public S()
{
x = 200;
y = 200;
drawOval = false;
this.setSize(400,400);
this.setVisible(true);
}
public void print(){
//在調(diào)用S類實(shí)例的print方法時(shí),畫一個(gè)以屬性X,Y為起點(diǎn)的寬高為10的圓.
drawOval = true; //設(shè)置drawOval變量為true
repaint(); //調(diào)用刷新畫面方法
}
public void paint(Graphics g)
{
//為true時(shí)繪制
if(drawOval)g.fillOval(x,y,10,10);
}
}
用JAVA編寫圓
代碼如下:
import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import javax.swing.JFrame; import javax.swing.JPanel; public class TestSw extends JFrame { public static void main(String[] args) { new TestSw(); } public TestSw(){ super("Test"); this.setSize(new Dimension(400,300)); this.setContentPane(new Mypane()); this.setVisible(true); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } class Mypane extends JPanel{ public void paint(Graphics g) { super.paint(g); g.setColor(Color.red); g.setXORMode(Color.white); g.drawArc(20, 20, 100, 100, 0, 360); ///此方法將畫一個(gè)直徑100的圓.紅色. } } }
java代碼畫出一個(gè)圓
你先想這道題的思路吧。
首先打印出圓形的話就是在一個(gè)60-60這種正方形里面話個(gè)圓,你先要知道要畫的點(diǎn)的位置對(duì)吧。算出所有的點(diǎn)打印出來(lái)。就是這個(gè)思路。然后是怎么算的問(wèn)題了
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('*');
}
}
}
用java畫一個(gè)圓
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("輸入位置坐標(biāo):");
label2 = new Label("輸入半徑:");
label3 = new Label("輸入要打印的*數(shù):");
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);//設(shè)置顏色
g.setStroke(stroke);//寬度
int x, y, w, h;
if (startX endX) {//以下的startx 、endx都是由鼠標(biāo)拖 動(dòng)事件得到
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);
}
網(wǎng)頁(yè)標(biāo)題:java畫個(gè)圓代碼,java編程畫個(gè)圓
URL鏈接:http://fisionsoft.com.cn/article/phhdoi.html