大橙子网站建设,新征程启航

为企业提供网站建设、域名注册、服务器等服务

随机画圆java代码 python随机画圆

用Java编写applet程序画圆

import java.applet.Applet;

我们提供的服务有:成都网站制作、成都做网站、微信公众号开发、网站优化、网站认证、瓦房店ssl等。为千余家企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的瓦房店网站制作公司

import java.awt.Graphics;

public class draw extends Applet{

public void paint(Graphics g){

g.drawOval(100,200,50,50);

或者将pain里面改为g.drawRoundRect(120,200,30,30,30,30);

也就顷谈是说drawOval和雀昌碰drawRoundRect都可以画圆迅笑.

用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(圆心位置,圆心位置);

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;

}

}

}

Java GUI编程题求解(自己已完成了一部分)

import java.awt.BorderLayout;

import java.awt.Graphics;

import java.awt.GridLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.util.ArrayList;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.JTextField;

/**

*

* @author szmneo

*/

public class DrawCircle {

static JFrame frame = new JFrame();

static DrawPanel p1 = new DrawPanel();

static JLabel l1 = new JLabel("直径:");

static JLabel l2 = new JLabel("弊肆周长:");

static JLabel l3 = new JLabel("面积:");

static JTextField tx = new JTextField("输入半径"瞎绝,10);

public static void main(String[] args){

JPanel p2 = new JPanel();

JPanel p3 = new JPanel();

JPanel p4 = new JPanel();

JButton b1 = new JButton("OK");

JButton b2 = new JButton("Clear");

p2.add(tx);

p2.add(b1);

p2.add(b2);

p3.setLayout(new GridLayout(3,1));

p3.add(l1);

p3.add(l2);

p3.add(l3);

p4.add(p2);

p4.add(p3);

frame.setLayout(new BorderLayout());

frame.add(p1,BorderLayout.CENTER);

frame.add(p4,BorderLayout.SOUTH);

frame.setTitle("Draw Circles");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setSize(500,400);

frame.setVisible(true);

b1.addActionListener(new ActionListener() {//添加磨卜姿绘制事件

public void actionPerformed(ActionEvent e) {

DrawAction();

}

});

b2.addActionListener(new ActionListener() {//添加清除事件

public void actionPerformed(ActionEvent e) {

ClearAction();

}

});

}

static void DrawAction(){

float r = 0;

try {

r = Float.parseFloat(tx.getText());

} catch (NumberFormatException numberFormatException) {

tx.setText("请输入数字(0-400)");

}

l1.setText("直径:" + Float.toString(2 * r));

l2.setText("周长:" + Float.toString((float) (2 * r * Math.PI)));

l3.setText("面积:"+ Float.toString((float) (Math.PI * r * r)));

p1.setR(r);

p1.repaint();

frame.repaint();

frame.setVisible(true);

}

static void ClearAction(){

p1.Clear();

p1.repaint();

frame.repaint();

frame.setVisible(true);

}

static class DrawPanel extends JPanel{

int x,y;

float r=0f;

ArrayList CircleData = new ArrayList();

void setR(float r){

this.r=r;

x = (int)(Math.random()*getWidth()-2*r)+1;

if(x0)x=0;

y = (int)(Math.random()*getHeight()-2*r)+1;

if(y0)y=0;

Circle circle = new Circle();

circle.setR(r);

circle.setX(x);

circle.setY(y);

CircleData.add(circle);//添加一个圆

}

void Clear(){

CircleData = new ArrayList();//清空列表

repaint();

}

public void paintComponent(Graphics g){

super.paintComponent(g);

for(int i=0;iCircleData.size();i++){//画每个圆

int a =((Circle)CircleData.get(i)).getX();

int b =((Circle)CircleData.get(i)).getY();

int d =(int)(2*((Circle)CircleData.get(i)).getR());

g.drawArc(a, b, d, d, 0, 360);

}

}

}

}

class Circle {//定义每个圆的信息

int x,y;

float r=0f;

void setR(float r){

this.r=r;

}

void setX(int x){

this.x=x;

}

void setY(int y){

this.y=y;

}

float getR(){

return r;

}

int getX(){

return x;

}

int getY(){

return y;

}

}


新闻名称:随机画圆java代码 python随机画圆
本文链接:http://dzwzjz.com/article/dsphchg.html
在线咨询
服务热线
服务热线:028-86922220
TOP