⑴ 怎么在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);
}