导航:首页 > 编程语言 > java在屏幕上用画一个空心圆

java在屏幕上用画一个空心圆

发布时间:2023-02-02 09:08:59

⑴ 怎么在java里画出小圆点

package test;

import java.awt.Canvas;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class PaintDemo extends JFrame{

/**
*
*/
private static final long serialVersionUID = -95348909224392777L;
JPanel c=canvas();
public PaintDemo(){
.setTitle("Small point paint test");
int w=300,h=300;
this.setSize(w, h);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
this.setSize(w,h);
this.setLocation(screenSize.width / 2 - w / 2,
screenSize.height / 2 - h / 2);
this.setLayout(null);

this.add(this.c);
this.c.setBounds(0, 0, 300, 300);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
System.exit(0);
}
});

this.setVisible(true);
}
public JPanel canvas() {
JPanel panel = new JPanel() {
private static final long serialVersionUID = 1L;

@Override
public void paint(Graphics g) {
super.paint(g);
g.fillOval(100, 100, 100, 100);
}
};
return panel;
}

/**
* @param args
*/
public static void main(String[] args) {
new PaintDemo();
}

}

⑵ 在Java中如何用程序画一个圆

使用java画圆要用到绘图类Graphics,下面是实例代码和运行效果:

packagecom.dikea.demo01;

importjava.awt.*;

importjavax.swing.*;

//java绘图原理

publicclassdemo_01extendsJFrame{

MyPanelmp=null;

publicstaticvoidmain(String[]args){

//TODO自动生成的方法存根

demo_01demo01=newdemo_01();

}

publicdemo_01(){

mp=newMyPanel();

this.add(mp);

this.setSize(400,300);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

this.setVisible(true);

}

}

//定义一个MyPanel面板,用于绘图区域

classMyPanelextendsJPanel{

//覆盖JPanel

//Graphics是绘图的重要类,可以理解成一支画笔

publicvoidpaint(Graphicsg){

//1.调用父类函数完成初始化任务

//这句话不可以少

super.paint(g);

//先画出一个圆圈

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

}

}

代码复制进ide编程工具,运行效果如下:

⑶ 用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在屏幕上用画一个空心圆相关的资料

热点内容
ps3文件分割视频 浏览:280
微信图片一键转发软件 浏览:331
如何判断s200plc编程电缆 浏览:691
太原编程培训班哪个好 浏览:171
树叶吹奏教程 浏览:6
社交app带来了哪些社会问题 浏览:394
如何安装爱宝8800数据采集器 浏览:712
文件保存了怎么找不到了 浏览:476
彩票网站怎么辨真假 浏览:840
pr找不到该文件 浏览:963
java移除panel 浏览:354
jsp填充jsp 浏览:166
海关外贸大数据在哪里查 浏览:381
思特奇java笔试题 浏览:121
葫芦侠在手机中的文件名 浏览:813
plc编程应该怎么收钱 浏览:584
c语言中源文件由什么组成 浏览:890
linuxhttpdphp配置文件 浏览:607
拆单数据要怎么保存 浏览:17
mac电脑怎样压缩文件到100m 浏览:645

友情链接