实现八点画圆,bresenham算法画椭圆,圆,画圆弧。
bresenhanm中点算法画椭圆,
EllipsePanel 。java
package com.deng.ellipse;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JPanel;
public class EllipsePanel extends JPanel{
private int a, b;
public EllipsePanel(int a,int b)
{
super();
int temp;
this.a =a;
this.b =b;
if(a<b)
{
temp =a;
a =b;
b =temp;
}
setBackground(Color.black);
setPreferredSize(new Dimension(800,600));
repaint();
}
public EllipsePanel()
{
super();
this.a =10;
this.b =5;
setBackground(Color.black);
setPreferredSize(new Dimension(800,600));
repaint();
}
//妈呀个呸,这个函数名字加一个"S"就出错了!
//container父类里面有个paintComponents,我重载就画不出图形了
//然后这个paintComponent 函数不知道是从哪里重载过来的
//这个函数的名字经常写错的
@Override
public void paintComponent(Graphics g)
{
// TODO Auto-generated method stub
super.paintComponent(g);
g.setColor(Color.red);
midBresenhamEllipse(g);
}
public void midBresenhamEllipse(Graphics g)
{
int x,y;
float d1,d2;
int baseX,baseY;//基本坐标,以面板中心为坐标原点
x=0;
y =b;
d1 =(float) (b*b+a*a*(-b+0.25));
g.setColor(Color.red);
//画坐标
baseX =(int)(this.getWidth()/2);
baseY =(int)(this.getHeight()/2);
g.drawLine(0, baseY, this.getWidth(), baseY);
g.drawLine(baseX, 0, baseX, this.getHeight());
//画四个基本点, 这是由一个点衍生出来的对称点
g.fillOval(baseX+x, baseY+y, 5, 5);
g.fillOval(baseX-x, baseY-y, 5, 5);
g.fillOval(baseX-x, baseY+y, 5, 5);
g.fillOval(baseX+x, baseY-y, 5, 5);
while (b*b*(x+1)<a*a*(y-0.5))
{
if(d1<=0)
{
d1 =d1 +b*b*(2*x+3);
x++;
}
else
{
d1 =d1 +(b*b*(2*x+3)+a*a*(-2*y+2));
x++;
y--;
}
g.fillOval(baseX+x, baseY+y, 5, 5);
g.fillOval(baseX-x, baseY-y, 5, 5);
g.fillOval(baseX-x, baseY+y, 5, 5);
g.fillOval(baseX+x, baseY-y, 5, 5);
}//while ends 上部分
d2 =(float) (b*b*(x+0.5)*(x+0.5)+a*a*(y-1)*(y-1) -a*a*b*b);
while (y>0) {
if(d2<=0)
{
d2 =d2+b*b*(2*x+2)+a*a*(-2*y+3);
x++;
y--;
}
else
{
d2 =d2+a*a*(-2*y+3);
y--;
}
g.fillOval(baseX+x, baseY+y, 5, 5);
g.fillOval(baseX-x, baseY-y, 5, 5);
g.fillOval(baseX-x, baseY+y, 5, 5);
g.fillOval(baseX+x, baseY-y, 5, 5);
}
} //midbresemham ends
}
BresenhamEllipseDriver .java
package com.deng.ellipse;
import javax.swing.JFrame;
public class BresenhamEllipseDriver {
public static void main(String[] args) {
JFrame framBresenhamEllipse = new JFrame("bresenmham ellipse");
framBresenhamEllipse.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
EllipsePanel panelEllipse = new EllipsePanel(200, 100);
framBresenhamEllipse.getContentPane().add(panelEllipse);
framBresenhamEllipse.pack();
framBresenhamEllipse.setVisible(true);
}
}
bresemham 画圆算法
BresenhamCicleDriver.java
package com.deng.bresenham;
import javax.swing.JFrame;
public class BresenhamCicleDriver {
public static void main(String[] args) {
JFrame frameCicleFrame =new JFrame("bresenham cicel");
frameCicleFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
BresenhamCiclePanel panel =new BresenhamCiclePanel(100);
frameCicleFrame.getContentPane().add(panel);
frameCicleFrame.pack();
frameCicleFrame.setVisible(true);
}
}
BresenhamCiclePanel.java
package com.deng.bresenham;
//using bresenham to dra a cicle
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JPanel;
import com.deng.cicle.EightPoint;
public class BresenhamCiclePanel extends JPanel{
private int r;
//construction
public BresenhamCiclePanel(int r)
{
super();
this.r =r;//把这句话忘了,所以圆的半径是0,我说怎么就没有圆弧呢,我靠
setBackground(Color.black);
setPreferredSize(new Dimension(800,600));
repaint();
}
public BresenhamCiclePanel()
{
this.r =10;
}
//draw cicle
@Override
public void paintComponent(Graphics g)
{
// TODO Auto-generated method stub
super.paintComponent(g);
g.setColor(Color.red);
int x,y, d;
x =0;
y =this.r;
d =1-this.r;
while (x<y) {
painEightPoint(g, x, y);
//repaint();
if(d<0)
d =d+2*x+3;
else
{
d =d+2*(x-y)+5;
y--;
}
x++;
}
}
public void painEightPoint(Graphics g,int x,int y)//这个就是八点画圆法
{
int baseX,baseY;
g.setColor(Color.red);
baseX =(int)(this.getWidth()/2);
baseY =(int)(this.getHeight()/2);
g.drawLine(0, baseY, this.getWidth(), baseY);
g.drawLine(baseX, 0, baseX, this.getHeight());
//(x,y) X Y 系列点
g.fillOval(baseX+x, baseY+y, 5, 5);
//8个点,无论什么情况,前面的极坐标都是以baseX开始的,否则(YX)系列点会跑偏,以前就写错了
//(-x,y)
g.fillOval(baseX-x, baseY+y, 5, 5);
//(-x,-y)
g.fillOval(baseX-x, baseY-y, 5, 5);
//(x,-y)
g.fillOval(baseX+x, baseY-y, 5, 5);
//(y,x) YX系列点
g.fillOval( baseX+y, baseY+x, 5,5);
//(-y,x)
g.fillOval( baseX-y,baseY+x, 5, 5);
g.fillOval( baseX-y,baseY-x, 5, 5);
g.fillOval( baseX+y,baseY- x, 5, 5);
}
}
八点画圆算法
EightPoint。java
package com.deng.cicle;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JPanel;
public class EightPoint extends JPanel
{
private int x,y;
//construction
public EightPoint(int x,int y)
{
super();
this.setBackground(Color.black);
this.setPreferredSize(new Dimension(800,600));
this.x =x;
this.y =y;
repaint();
}//construction ends
public void paintComponent(Graphics g)//函数名字写错了,我晕啊
{
super.paintComponent(g);
g.setColor(Color.red);
eight(g,x,y);
}//paint Graphic function ends
public void eight(Graphics g,int x,int y)
{
int baseX,baseY;
g.setColor(Color.red);
baseX =(int)(this.getWidth()/2);
baseY =(int)(this.getHeight()/2);
g.drawLine(0, baseY, this.getWidth(), baseY);
g.drawLine(baseX, 0, baseX, this.getHeight());
//(x,y) X Y 系列点
g.fillOval(baseX+x, baseY+y, 5, 5);
//8个点,无论什么情况,前面的极坐标都是以baseX开始的,否则(YX)系列点会跑偏
//(-x,y)
g.fillOval(baseX-x, baseY+y, 5, 5);
//(-x,-y)
g.fillOval(baseX-x, baseY-y, 5, 5);
//(x,-y)
g.fillOval(baseX+x, baseY-y, 5, 5);
//(y,x) YX系列点
g.fillOval( baseX+y, baseY+x, 5,5);
//(-y,x)
g.fillOval( baseX-y,baseY+x, 5, 5);
g.fillOval( baseX-y,baseY-x, 5, 5);
g.fillOval( baseX+y,baseY- x, 5, 5);
}
}
CicleDriver。java
package com.deng.cicle;
import javax.swing.JFrame;
public class CicleDriver
{
public static void main(String[] args)
{
JFrame frame = new JFrame("画圆和椭圆");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
EightPoint eightPanel = new EightPoint(10, 18);
frame.getContentPane().add(eightPanel);
frame.pack();
frame.setVisible(true);
}
}
画圆弧
ArcPanel.java
package com.deng.arc;
//动态产生圆弧的算法,运算量比较大
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JPanel;
public class ArcPanel extends JPanel
{
private int r;
public ArcPanel(int r)
{
super();
//setd(JFrame.EXIT_ON_CLOSE);
this.r =r;
setPreferredSize(new Dimension(800,600));
setBackground(Color.black);
repaint();
}//construction ends
//重载父类的一个方法,八分之一圆弧
@Override
public void paintComponent(Graphics g)//是paint 不是print
{
// TODO Auto-generated method stub
super.paintComponent(g);
g.setColor(Color.red);
int x1 =0;
int x2;
int y2;
do
{
x2 =x1+1;
y2 =(int)Math.round(Math.sqrt(r*r-x2*x2));
g.fillRect(x2, y2, 1, 1);
x1=x2;
}while(x1<(r));
///(Math.sqrt(2)));
}//paint ends
}
ArcDriver。java
package com.deng.arc;
import javax.swing.JFrame;
public class ArcDriver {
public static void main(String[] args) {
JFrame frameArc = new JFrame("八分圆弧");
frameArc.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ArcPanel panel = new ArcPanel(500);
frameArc.getContentPane().add(panel);
frameArc.pack();
frameArc.setVisible(true);
}
}