java代码怎么画圆 javagui画圆

用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); ///此方法将画一个直径100的圆.红色. } } }

用java画一个圆

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代码画一个圆

靠,楼上的回答那么长啊,只要一个函数,就是

drawOval(int

x,int

y,int

w,int

h);

这是是画椭圆形的函数,但是它也可以画圆形。

比如

drawOval(100,100,50,50);

就在坐标50,50画一个直径100的圆,只要把,最后的2个参数设成一样就是一个圆。要画直径200的话,就把最后2个参数设成200,200

一切OK了

java代码画出一个圆

你先想这道题的思路吧。

首先打印出圆形的话就是在一个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('*');

}

}

}


名称栏目:java代码怎么画圆 javagui画圆
当前链接:http://myzitong.com/article/dojdchc.html