「java画圆规则」java画一个圆

博主:adminadmin 2022-11-22 08:57:09 79

今天给各位分享java画圆规则的知识,其中也会对java画一个圆进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

JAVA参数画圆

//画圆一般通过继承JPanel 或者JFrame ,通过调用

//panel或者frame中的Graphics实例完成画图

public void drawCircle(int x,int y,int r,Color color)

{

Graphics g=this.getGraphics();

g.setColor(color);

g.drawOval(x-r, y-r, 2*r, 2*r);

}

其他参数的情况可以重载这个方法,并调用方法中通过设定参数

调用这个public void drawCircle(int x,int y,int r,Color color)方法

来实现

如:

public void drawCircle(int x,int y,int r)

{

this.drawCircle( x, y , r ,Color.Black);

}

public void drawCircle(int x,int y,Color color)

{

Random random=new Random;

r=10+random.nextInt(90);

this.drawCircle( x, y, r , color);

}

public void drawCircle(int x,int y)

{

this.drawCircle(x, y, 40 , Color.red);

}

用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中如何用程序画一个圆

使用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编程工具,运行效果如下:

「java画圆规则」java画一个圆

在java中如何绘画圆形

/*

* java绘图原理

*/

package Yousphu;

import java.awt.*;

import javax.swing.*;

public class Demo9_1 extends JFrame {

MyPanel mp=null;

public static void main(String[] args) {

// TODO Auto-generated method stub

Demo9_1 demo = new Demo9_1();

}

public Demo9_1()

{

mp=new MyPanel();

this.add(mp);

this.setSize(300, 400);

this.setDefaultCloseOperation(EXIT_ON_CLOSE);

this.setVisible(true);

}

}

//定义一个吗panel、用于绘制和实现图像

class MyPanel extends JPanel

{

//覆盖jpanel的paint方法

//Graphics是汇入的中药类,你可以吧他理解成画笔

public void paint(Graphics g)

{

//调用父类函数进行初始化,绝对不可少

super.paint(g);

//先画一个园

g.drawOval(10, 10, 30, 30);

/*

g.drawRect(30, 30, 40, 40);

g.setColor(Color.blue);

g.drawString("fdsf", 40, 40);*/

//Image im= Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/aaa.jpg"));

//g.drawImage(im, 20, 20, 200, 200,this);

}

}

怎么用JAVA画半个圆或者是半圆弧?

画圆一般通过继承JPanel 或者JFrame ,通过调用panel或者frame中的Graphics实例完成画图。

java绘图的基本原理:画一个圆

import javax.swing.*;

import java.awt.*;

public class DrawCicle extends JFrame{MyPanel mp=null;public static void main(String[] args) {DrawCicle dc=new DrawCicle();}

public DrawCicle(){mp =new MyPanel();this.add(mp);this.setSize(300, 250);

this.setLocation(600, 300);this.setDefaultCloseOperation(this.EXIT_ON_CLOSE)this.setVisible(true);}}

JAVA画圆

Component c=...........

Graphics g=c.getGraphics();

Graphics类是抽象的,只能通过一个Component的实例获得Graphics类的实例

例:

Frame f=new Frame();

Graphics g=f.getGraphics();

还有,若要将所画的显示出来,则先要使Component可见

关于java画圆规则和java画一个圆的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

The End

发布于:2022-11-22,除非注明,否则均为首码项目网原创文章,转载请注明出处。