计算机图形学简单画图代码
计算机图形学编程

1:角度DDA法绘制椭圆的程序:V oid line DDA(int x1,int y1,int x2,int y2,int color) x=x0+acost;y=y0+bsint { if{abs(x2-x1)>=abs(y2-y1);dm=abs(x2-x1)}Else { dm=abs(y2-y1)}float dx=(float)(x2-x1)/dm;float x=(float)(x1+0.5);float y=(float)(y1+0.5);for (int i=0;i<dm;i++)Setpixel(int x,int y,int color)x+=dx;y+=dy;}}2:绘制抛物线的程序:Par (int xs,int ys,int xm,int ym,int xe,int ye){double d,d1,ax,bx,by;int n,i;ax=(xe-2*xm+xs)*2.0;ay=(ye-2*ym+ys)*2.0;bx=xe-xs-ax;by=ye-ys-ay;n=sqrt(ax*ax+ay*ay);d=1.0/n;d1=d;for(i=0;i<=n;i++){ lineto((ax*d1+bx*d1+xs),(ay*d1*d1+by*d1+ys);d1+=d;}Lineto(xe,ye);}3:绘制三次Hermite曲线的程序:(给出四个点P0,P1,P2,P3,要在P1和P2之间绘一条Hermite曲线,用向量P0P1作为P1处的切线向量,用P2P3作为P2处的切线矢量)V oid HermiteCurve(Point po,Point p1,Point p2,Point p3,int count){Point r1,r2; //切线矢量r1=p1-p0; //调用重载r2=p3-p2;double t=0.0; dt=1.0/count;moveto(p1.x,p1.y);//设置起点for(int i=0;i<count+1;i++){double tt=t*t; double ttt=t*t*t;Double F1,F2,F3,F4; //调和函数F1=2*ttt-3*tt+1; F2=-2*ttt+3*tt; F3=ttt-2*tt+t; F4=ttt-tt;double x=p1.x*F1+p2.x*F2+r1.x*F3+r2.x*F4;double x=p1.y*F1+p2.y*F2+r1.y*F3+r2.y*F4;Lineto(x,y);t+=dt;}}4:绘制三次Bezier曲线和三次B样条曲线的程序(以四个点P0,P1,P2,P3作为控制多边形)V oid BezierCurve(Point p0,Pint p1,Point p2,Point p3,int count){double t=0.0; dt=1.0/count;moveto(p1.x, p1.y);//设置起点for(int i=0; i<count+1;i++){ double F1,F2,F3,F4,x,y;//调和函数double u=1.0-t;F1=u*u*u; F2=3*t*u*u; F3=3*t*t*u; F4=t*t*t;x=p0.x*F1+p1.x+F2+p2.x*F3+p3.x*F4;y=po.y*F1+p1.y*F2+p2.y*F3+p3.y*F4;Lineto(x,y);t+=dt;}}Void B_SpLine((Point p0,Pint p1,Point p2,Point p3,int count){double t=0.0;dt=1.0/count;For (int i=0;i<count+1;i++){ double tt=t*t; double ttt=tt*t;Double F1,F2,F3,F4;//调和函数F1=-ttt+3*tt-3*t+1; F2=3*ttt-6*tt+4;F3=-3*ttt+3*tt+3*t+1; F4=ttt;x=p0.x*F1+p1.x*F2+p2.x*F3+p3.x*F4;y=p0.y*F1+p1.y*F2+p2.y*F3+p3.y*F4;x/=6.0; y/=6.0;if(i==0) moveto(x,y);else lineto(x,y);t+=dt;}}5:写出光线跟踪算法(Ray-Tracing算法)的伪代码Color TraceRay(start,direction,depth)Vector start,directionInt depth{if (depth>MAX_DEPTH)color=黑色;else{ 光线与物体求交,找出离start最近的交点;If(无交点)color=背景色;Else{ local——color=用局部光照模型计算出交点处的光强;计算反射方向:R=1+2Ncosθ;Reflected_color=TraceRay(交点,反射方向R,depth+1);计算折射方向: T= (1/η)I-(cosθ2-(1/η)cosθ1)N;Transmitted_color=TraceRay(交点,折射方向,depth+1);Color=combine(local_color,Reflected_color,Kr,transmitted_color,KT)}}Return(color);}6:借助栈数据结构,写出漫水法区域填充的程序。
计算机图形学常用算法及代码大全

2。
1。
1 生成直线的DDA算法数值微分法即DDA法(Digital Differential Analyzer),是一种基于直线的微分方程来生成直线的方法.一、直线DDA算法描述:设(x1,y1)和(x2,y2)分别为所求直线的起点和终点坐标,由直线的微分方程得= m =直线的斜率(2-1)可通过计算由x方向的增量△x引起y的改变来生成直线:x i+1=x i+△x (2-2)y i+1=y i+△y=y i+△x·m (2-3) 也可通过计算由y方向的增量△y引起x的改变来生成直线:y i+1=y i+△y (2-4)x i+1=x i+△x=x i+△y/m (2-5) 式(2-2)至(2-5)是递推的.二、直线DDA算法思想:选定x2-x1和y2-y1中较大者作为步进方向(假设x2-x1较大),取该方向上的增量为一个象素单位(△x=1),然后利用式(2-1)计算另一个方向的增量(△y=△x·m=m)。
通过递推公式(2-2)至(2-5),把每次计算出的(x i+1,y i+1)经取整后送到显示器输出,则得到扫描转换后的直线。
之所以取x2-x1和y2-y1中较大者作为步进方向,是考虑沿着线段分布的象素应均匀,这在下图中可看出。
另外,算法实现中还应注意直线的生成方向,以决定Δx及Δy是取正值还是负值。
三、直线DDA算法实现:1、已知直线的两端点坐标:(x1,y1),(x2,y2)2、已知画线的颜色:color3、计算两个方向的变化量:dx=x2-x1dy=y2-y14、求出两个方向最大变化量的绝对值:steps=max(|dx|,|dy|)5、计算两个方向的增量(考虑了生成方向):xin=dx/stepsyin=dy/steps6、设置初始象素坐标:x=x1,y=y17、用循环实现直线的绘制:for(i=1;i〈=steps;i++){putpixel(x,y,color);/*在(x,y)处,以color色画点*/x=x+xin;y=y+yin;}五、直线DDA算法特点:该算法简单,实现容易,但由于在循环中涉及实型数的运算,因此生成直线的速度较慢。
计算机图形学代码

四、实验结果抓图与分析1、目标的平移的源程序2、绕任意点旋转的源程序实验一、直线的生成一、实验内容根据提供的程序框架,修改部分代码,完成画一条直线的功能(中点画线法或者Bresenham画线法任选一),只要求实现在第一象限内的直线。
二、算法原理介绍双击直线生成.dsw打开给定的程序,或者先启动VC++,文件(file)→打开工作空间(open workspace)。
打开直线生成view.cpp,按注释改写下列函数:1.void CMyView::OnDdaline() (此为DDA生成直线)2.void CMyView::OnBresenhamline()(此为Bresenham画直线)3.void CMYView::OnMidPointLine()(此为中点画线法)三、程序源代码1.DDA生成直线画法程序:float x,y,dx,dy,k;dx=(float)(xb-xa);dy=(float)(yb-ya);k=dy/dx;x=xa;y=ya;if(abs(k)<1){for (x=xa;x<=xb;x++){pdc->SetPixel(x, int(y+0.5),COLOR);y=y+k;}}if(abs(k)>=1){for(y=ya;y<=yb;y++){pdc->SetPixel(int(x+0.5),y,COLOR);x=x+1/k;}}//DDA画直线结束}2.Bresenham画直线源程序:float b,d,xi,yi;int i;float k;k=(yb-ya)/(xb-xa);b=(ya*xb-yb*xa)/(xb-xa);if(k>0&&k<=1)for(i=0;i<abs(xb-xa);i++){ d=ya+0.5-k*(xa+1)-b;if(d>=0){ xi=xa+1;yi=ya;xa++;ya=ya+0.5;}if(d<0){ xi=xa+1;yi=ya+1;xa++;ya=ya+1.5;}pdc->SetPixel(xi,yi,COLOR); }//BresenHam画直线结束}3.中点画线法源程序:float b,d,xi,yi;int i;float k;k=(yb-ya)/(xb-xa);b=(ya*xb-yb*xa)/(xb-xa);if(k>0&&k<=1)for(i=0;i<abs(xb-xa);i++){ d=ya+0.5-k*(xa+1)-b;if(d>=0){ xi=xa+1;yi=ya;xa++;ya=ya+0.5;}if(d<0){ xi=xa+1;yi=ya+1;xa++;ya=ya+1.5;}pdc->SetPixel(xi,yi,COLOR); }//BresenHam画直线结束}四、实验结果抓图与分析1、DDA生成直线2、Bresenham画直线3、中点画线法实验二、bresenham画圆一、实验内容根据提供的程序框架,修改部分代码,用Bresenham画法画一段圆弧或者画圆。
中南大学计算机图形学作业代码

中南大学计算机图形学作业代码您好,根据您的要求,以下是一个简单的计算机图形学作业代码,该代码使用Python编写,实现了简单的图形绘制和变换功能。
代码共有1200多个字,希望能够帮助到您。
```pythonimport tkinter as tkclass Graphics:def __init__(self):self.window = self.canvas = tk.Canvas(self.window, width=800, height=600) self.canvas.packdef draw_rectangle(self, x, y, width, height):self.canvas.create_rectangle(x, y, x+width, y+height)def translate(self, dx, dy):self.canvas.move("all", dx, dy)def scale(self, sx, sy):self.canvas.scale("all", 0, 0, sx, sy)def rotate(self, angle):self.canvas.rotate("all", angle)def mainloop(self):self.window.mainloop#创建图形对象graphics = Graphics#绘制一个矩形graphics.draw_rectangle(100, 100, 200, 150)#平移图形graphics.translate(50, 50)#缩放图形graphics.scale(1.5, 1.5)#旋转图形graphics.rotate(45)#运行主循环graphics.mainloop```以上代码使用Python的tkinter库创建了一个窗口并在窗口中绘制了一个矩形。
python turtle常用代码

python turtle常用代码Python turtle是一款基于Python语言的图形绘制库,它可以让我们通过编写代码来绘制各种形状、图案和动画。
在Python中使用turtle 库可以非常方便地进行可视化编程,同时也可以帮助我们更好地理解计算机图形学的相关知识。
本篇文章将介绍一些常用的Python turtle代码,包括基础图形绘制、颜色设置、填充效果、字体设置等内容。
一、基础图形绘制1. 画线段使用turtle库中的forward()函数可以画出一条直线段,例如:```import turtleturtle.forward(100)```这段代码可以画出长度为100的直线段。
2. 画圆使用turtle库中的circle()函数可以画出一个圆形,例如:```import turtleturtle.circle(50)```这段代码可以画出半径为50的圆形。
3. 画正方形使用turtle库中的forward()和right()函数可以分别向前移动和向右转动角度,从而实现正方形的绘制,例如:```import turtlefor i in range(4):turtle.forward(100)turtle.right(90)```这段代码可以画出边长为100的正方形。
4. 画三角形与绘制正方形类似,只需要改变角度即可实现三角形的绘制,例如:```import turtlefor i in range(3):turtle.forward(100)turtle.right(120)```这段代码可以画出边长为100的等边三角形。
二、颜色设置1. 设置画笔颜色使用turtle库中的pencolor()函数可以设置画笔的颜色,例如:```import turtleturtle.pencolor("red")```这段代码可以将画笔的颜色设置为红色。
2. 设置填充颜色使用turtle库中的fillcolor()函数可以设置填充区域的颜色,例如:```import turtleturtle.fillcolor("yellow")```这段代码可以将填充区域的颜色设置为黄色。
计算机图形学代码

void COval::BresenhamOval(int x,int y,int r,int color,CDC* pDC) {int xx,yy,p,c;p=3-2*r;c=color;xx = 0;yy = r;for(;xx<=yy;xx++){pDC->SetPixel(xx+x,yy+y,c);pDC->SetPixel(-xx+x,-yy+y,c);pDC->SetPixel(-xx+x,yy+y,c);pDC->SetPixel(xx+x,-yy+y,c);pDC->SetPixel(yy+x,xx+y,c);pDC->SetPixel(-yy+x,xx+y,c);pDC->SetPixel(-yy+x,-xx+y,c);pDC->SetPixel(yy+x,-xx+y,c);if(p<0)p=p+4*xx+6;else {yy--;p=p+4*(xx-yy)+10;}}}贝塞尔曲线void CArc::DrawArc(){double t;CPoint Point0,Point1;CClientDC dc(m_pView);Point0=m_P1;for(t=0;t<1.01;t=t+0.001){Point1.x=(1-t)*(1-t)*(1-t)*m_P1.x+3*(1-t)*(1-t)*t*m_P2.x+3*(1-t)*t*t*m_P3.x+t*t*t*m_P4.x;Point1.y=(1-t)*(1-t)*(1-t)*m_P1.y+3*(1-t)*(1-t)*t*m_P2.y+3*(1-t)*t*t*m_P3.y+t*t*t*m_P4.y;dc.MoveTo(Point0.x,Point0.y);dc.LineTo(Point1.x,Point1.y);Point0=Point1;}}void CLine::Bresenhamline(int x0,int y0,int x1,int y1,int color,CDC* pdc) {int x,y,dx,dy,unitx,unity,fabs_dx,fabs_dy,e;dx=x1-x0;dy=y1-y0;fabs_dx = (int)fabs((float)dx);fabs_dy = (int)fabs((float)dy);unitx = dx / fabs_dx ;unity = dy / fabs_dy ;x=x0;y=y0;if( fabs_dx< fabs_dy ) //斜率大于1(窗口坐标与数学坐标相反){e=-dx; //差值设初值for(int i=0;i<=fabs_dx;i++){pdc->SetPixel(x,y,color);x+=unitx; //x增加一步,步长是1,方向由unitx决定e=e+2*dy; //e增加一个2*k*dxif(e>=0) //如果差值大于0,即dy累计大于1{y+=unity;e=e-2*dx; //e减去1,乘以倍数即为2*dx}}}else{e=-dy;for(int i=0;i<=fabs_dy;i++){pdc->SetPixel(x,y,color);y+=unity;e=e+2*dx;if(e>=0){x+=unitx;e=e-2*dy;}}}}。
计算机图形学简单画图代码

软件:NetBeans 图形效果:代码:package newpackage;import java.awt.*;import javax.swing.*;import java.awt.geom.*;import java.awt.image.*;import .URL;import java.io.*;import javax.imageio.*;import java.awt.event.*;import java.util.Calendar;import javax.swing.*;public class Hello2D extends JApplet {public static void main(String s[]) {JFrame frame = new JFrame();frame.setTitle("计算机图形学");frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);JApplet applet = new Hello2D();applet.init();frame.getContentPane().add(applet);frame.pack();frame.setVisible(true);}public void init() {JPanel panel = new Hello2DPanel();getContentPane().add(panel);}}class Hello2DPanel extends JPanel implements ActionListener{ private BufferedImage image;AffineTransform rotH = new AffineTransform();AffineTransform rotM = new AffineTransform();AffineTransform rotS = new AffineTransform();// AffineTransform zuq=new AffineTransform ();public Hello2DPanel() {setPreferredSize(new Dimension(1400,1000));setBackground(Color.white);Timer timer=new Timer(500,this);timer.start();URL url=getClass().getClassLoader().getResource("images/zuqiu.jpg");try{image=ImageIO.read(url);}catch(IOException ex){ex.printStackTrace();}}@Overridepublic void paintComponent(Graphics g) {super.paintComponent(g);Graphics2D g2= (Graphics2D)g;g2.translate(100,100);g2.scale(0.5, 0.5);for (int i = 0; i < 12; i++) {g2.rotate(2*Math.PI/12);g2.fill3DRect(-3, -180, 6, 30, true);}Shape hour = new Line2D.Double(0, 0, 0, -80);hour = rotH.createTransformedShape(hour);Shape minute = new Line2D.Double(0, 0, 0, -120);minute = rotM.createTransformedShape(minute);Shape second = new Line2D.Double(0, 0, 0, -120);second = rotS.createTransformedShape(second);g2.setColor(Color.black);g2.setStroke(new BasicStroke(5, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND)); g2.draw(hour);g2.draw(minute);g2.setStroke(new BasicStroke(2));g2.draw(second);g2.scale(2,2);g2.translate(50,-150);g2.setColor(Color.DARK_GRAY);GeneralPath path=new GeneralPath();path.moveTo(200, 170);path.lineTo(800, 170);path.quadTo(950, 350, 800,530 );path.lineTo(200, 530);path.quadTo(50, 350, 200,170);Stroke stroke=new BasicStroke(4,BasicStroke.CAP_BUTT,BasicStroke.CAP_ROUND);//设置笔画g2.setStroke(stroke);g2.translate(-100, -100);//平移g2.scale(1.4,1.4 );//放大path.closePath();g2.draw(path);g2.setColor(Color.LIGHT_GRAY);g2.fill(path);Shape s1=new Rectangle2D.Double(300,250, 400, 200);Shape s2=new Ellipse2D.Double(200,250, 200, 200);Shape s3=new Ellipse2D.Double(600,250, 200, 200);g2.setColor(Color.DARK_GRAY);g2.draw(s1);g2.draw(s2);g2.draw(s3);g2.setColor(Color.GREEN);Area a1=new Area(s1);Area a2=new Area(s2);a1.add(a2);g2.fill(a1);Area a3=new Area(s3);a1.add(a3);g2.fill(s3);Font font=new Font("Serif",Font.BOLD,25);g2.setFont(font);GradientPaint gp=new GradientPaint(450,200,Color.red,220,220,Color.BLACK,true);g2.setPaint(gp);g2.drawString("四百米跑道",450, 220);g2.setColor(Color.BLACK);Stroke stroke1=new BasicStroke(1);//设置笔画g2.setStroke(stroke1);Shape s4=new Rectangle2D.Double(300,250, 400, 200);g2.draw(s4);g2.drawLine(500, 250, 500, 450);//划直线g2.drawOval(465, 315, 70, 70);g2.drawLine(300, 300, 350, 300);g2.drawLine(350, 300, 350,400);g2.drawLine(350, 400, 300,400);g2.drawLine(300, 325, 325, 325);g2.drawLine(325, 325, 325,375);g2.drawLine(325, 375, 300,375);g2.drawLine(700, 300, 650,300);g2.drawLine(650, 300, 650, 400);g2.drawLine(650, 400, 700,400);g2.drawLine(700, 325, 675,325);g2.drawLine(675, 325, 675, 375);g2.drawLine(675,375, 700,375);//绘制虚线float[] dashArray={20,20,20,20};g2.setColor(Color.BLACK);Font font2=new Font("Serif",Font.BOLD,15);g2.setFont(font2);g2.drawString("1",290,465);g2.drawString("2",290,485);g2.drawString("3",290,505);g2.drawString("4",290,525);g2.drawLine(300, 450,300 ,530);g2.setColor(Color.red);g2.drawLine(700, 450,700 ,530);float dashPhase=50;stroke =newBasicStroke(1,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL,0,dashArray,dashPhase);g2.setStroke(stroke);g2.drawLine(300, 470,700 ,470);g2.drawLine(300, 490,700 ,490);g2.drawLine(300, 510,700 ,510);Font font1=new Font("Serif",Font.BOLD,15);g2.setFont(font1);GradientPaint gp1=new GradientPaint(450,200,Color.BLACK,220,220,Color.red);g2.setPaint(gp1);g2.drawString("200米接力赛跑道",400,480);TexturePaint tp=new TexturePaint(image,new Rectangle2D.Double(550,370,image.getWidth(),image.getHeight()));g2.setPaint(tp);Shape zq=new Rectangle.Double(550,370,90,50);// zq=zuq.createTransformedShape(zq);g2.fill(zq);}@Overridepublic void actionPerformed(ActionEvent e) {int hour = Calendar.getInstance().get(Calendar.HOUR);int min = Calendar.getInstance().get(Calendar.MINUTE);int sec = Calendar.getInstance().get(Calendar.SECOND);rotH.setToRotation(Math.PI * (hour+min/60.0)/6.0);rotM.setToRotation(Math.PI * min /30.0);rotS.setToRotation(Math.PI * sec /30.0);repaint();}}。
计算机图形学-画图程序

Private Sub HScroll1_Change()Picture1.Left = -HScroll1.ValueEnd SubPrivate Sub HScroll1_Scroll()Picture1.Left = -HScroll1.ValueEnd SubPrivate Sub Label3_Click()CommonDialog1.Color = Label3.BackColorLabel2.BackColor = CommonDialog1.Color End SubPrivate Sub Label3_DblClick()CommonDialog1.ShowColorLabel2.BackColor = CommonDialog1.Color End SubPrivate Sub Label4_Click()CommonDialog1.Color = Label4.BackColorLabel2.BackColor = CommonDialog1.Color End SubPrivate Sub Label4_DblClick()CommonDialog1.ShowColorLabel2.BackColor = CommonDialog1.Color End SubPrivate Sub Label5_Click()CommonDialog1.Color = Label5.BackColorLabel2.BackColor = CommonDialog1.Color End SubPrivate Sub Label5_DblClick()CommonDialog1.ShowColorLabel2.BackColor = CommonDialog1.Color End SubPrivate Sub Label6_Click()CommonDialog1.Color = Label6.BackColorLabel2.BackColor = CommonDialog1.Color End SubPrivate Sub Label6_DblClick()CommonDialog1.ShowColorLabel2.BackColor = CommonDialog1.Color End SubPrivate Sub Label7_Click()CommonDialog1.Color = Label7.BackColorLabel2.BackColor = CommonDialog1.Color End SubPrivate Sub Label7_DblClick()CommonDialog1.ShowColorLabel2.BackColor = CommonDialog1.Color End SubPrivate Sub Label8_Click()CommonDialog1.Color = Label8.BackColorLabel2.BackColor = CommonDialog1.Color End SubPrivate Sub Label8_DblClick()CommonDialog1.ShowColorLabel2.BackColor = CommonDialog1.Color End SubPrivate Sub Label9_Click()CommonDialog1.Color = Label9.BackColorLabel2.BackColor = CommonDialog1.Color End SubCommonDialog1.ShowColorLabel2.BackColor = CommonDialog1.Color End SubPrivate Sub Label10_Click()CommonDialog1.Color = Label10.BackColorLabel2.BackColor = CommonDialog1.Color End SubPrivate Sub Label10_DblClick()CommonDialog1.ShowColorLabel2.BackColor = CommonDialog1.Color End SubPrivate Sub Label11_Click()CommonDialog1.Color = Label11.BackColorLabel2.BackColor = CommonDialog1.Color End SubPrivate Sub Label11_DblClick()Label2.BackColor = CommonDialog1.ColorCommonDialog1.ShowColorEnd SubPrivate Sub Label12_Click()CommonDialog1.Color = Label12.BackColorLabel2.BackColor = CommonDialog1.Color End SubPrivate Sub Label12_DblClick()CommonDialog1.ShowColorLabel2.BackColor = CommonDialog1.Color End SubPrivate Sub Label13_Click()CommonDialog1.Color = Label13.BackColorLabel2.BackColor = CommonDialog1.Color End SubPrivate Sub Label13_DblClick()CommonDialog1.ShowColorLabel2.BackColor = CommonDialog1.Color End SubPrivate Sub Label14_Click()CommonDialog1.Color = Label14.BackColorLabel2.BackColor = CommonDialog1.Color End SubPrivate Sub Label14_DblClick()CommonDialog1.ShowColorLabel2.BackColor = CommonDialog1.Color End SubCommonDialog1.Color = Label15.BackColorLabel2.BackColor = CommonDialog1.ColorEnd SubPrivate Sub Label15_DblClick()CommonDialog1.ShowColorLabel2.BackColor = CommonDialog1.ColorEnd SubPrivate Sub Label16_Click()CommonDialog1.Color = Label16.BackColorLabel2.BackColor = CommonDialog1.ColorEnd SubPrivate Sub Label16_DblClick()CommonDialog1.ShowColorLabel2.BackColor = CommonDialog1.ColorEnd SubPrivate Sub Label17_Click()CommonDialog1.Color = Label17.BackColorLabel2.BackColor = CommonDialog1.ColorEnd SubPrivate Sub Label17_DblClick()CommonDialog1.ShowColorLabel2.BackColor = CommonDialog1.ColorEnd SubPrivate Sub Label18_Click()CommonDialog1.Color = Label18.BackColorLabel2.BackColor = CommonDialog1.ColorEnd SubPrivate Sub Label18_DblClick()CommonDialog1.ShowColorLabel2.BackColor = CommonDialog1.ColorEnd SubPrivate Sub Picture1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single) Dim i, jDim col As LongLabel19.Caption = X: Label20.Caption = YPicture1.DrawWidth = Combo1.TextIf Button = 1 Thenflag = 1Select Case name1Case "铅笔"Picture1.PSet (X, Y), CommonDialog1.Colortmp_x = X: tmp_y = YCase "橡皮擦"Picture1.PSet (X, Y), Picture1.BackColorCase "直线"tmp_x = X: tmp_y = YPicture1.PSet (X, Y), CommonDialog1.ColorCase "矩形"tmp_x = X: tmp_y = YPicture1.PSet (X, Y), CommonDialog1.ColorCase "圆"tmp_x = X: tmp_y = YPicture1.DrawWidth = 1For i = -3 To 3Picture1.PSet (X + i, Y), CommonDialog1.ColorPicture1.PSet (X, Y + i), CommonDialog1.Color Next iPicture1.DrawWidth = Combo1.TextCase "椭圆"tmp_x = X: tmp_ = YCase "喷枪"Picture1.DrawWidth = 1For i = -10 To 10 Step 2For j = -10 To 10 Step 2Picture1.PSet (X + i, Y + j), CommonDialog1.Color Next jNext iPicture1.DrawWidth = Combo1.TextCase "填充"col = Picture1.Point(X, Y)Picture1.PSet (X, Y), CommonDialog1.Colori = 1Do While i > 0i = i - 1If Picture1.Point(X - 1, Y) = col ThenPicture1.PSet (X - 1, Y), CommonDialog1.Colori = i + 1X = X - 1End IfIf Picture1.Point(X, Y - 1) = col ThenPicture1.PSet (X, Y - 1), CommonDialog1.Colori = i + 1Y = Y - 1End IfIf Picture1.Point(X + 1, Y) = col ThenPicture1.PSet (X + 1, Y), CommonDialog1.Colori = i + 1X = X + 1End IfIf Picture1.Point(X, Y + 1) = col ThenPicture1.PSet (X, Y + 1), CommonDialog1.Colori = i + 1Y = Y + 1End IfLoopCase "选取"XL = X: YB = YCase "移动"For i = XL To XRFor j = YB To YTPicture1.PSet (X + i - XL, Y + j - YB), Picture1.Point(i, j)Picture1.PSet (i, j), Picture1.BackColorNext jNext iCase "复制"XL = X: YB = YCase "剪切"XL = X: YB = YCase "粘贴"For i = XL To XRFor j = YB To YTIf name1 = "复制" ThenPicture1.PSet (X + i - XL, Y + j - YB), Picture1.Point(i, j)ElsePicture1.PSet (X + i - XL, Y + j - YB), Picture1.Point(i, j)Picture1.PSet (i, j), Picture1.BackColorEnd IfNext jNext iEnd SelectEnd IfEnd SubPrivate Sub Picture1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single) Picture1.DrawWidth = Combo1.TextLabel19.Caption = X: Label20.Caption = YIf flag = 1 ThenSelect Case name1Case "铅笔"Picture1.Line (tmp_x, tmp_y)-(X, Y), CommonDialog1.Colortmp_x = X: tmp_y = YCase "橡皮擦"Picture1.PSet (X, Y), Picture1.BackColorEnd SelectEnd IfEnd SubPrivate Sub Picture1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single) Picture1.DrawWidth = Combo1.TextLabel19.Caption = X: Label20.Caption = Yflag = 0Select Case name1Case "铅笔"Picture1.PSet (X, Y), CommonDialog1.ColorCase "直线"Picture1.Line (tmp_x, tmp_y)-(X, Y), CommonDialog1.ColorCase "矩形"Picture1.Line (tmp_x, tmp_y)-(tmp_x, Y), CommonDialog1.ColorPicture1.Line (tmp_x, tmp_y)-(X, tmp_y), CommonDialog1.ColorPicture1.Line (X, Y)-(tmp_x, Y), CommonDialog1.ColorPicture1.Line (X, Y)-(X, tmp_y), CommonDialog1.ColorCase "圆"Picture1.Circle (tmp_x, tmp_y), Sqr((X - tmp_x) * (X - tmp_x) + (Y - tmp_y) * (Y - tmp_y)), CommonDialog1.ColorCase "椭圆"If Abs(Y - tmp_y) <= Abs(X - tmp_x) ThenPicture1.Circle (Abs(X - tmp_x) / 2 + tmp_x, Abs(Y - tmp_y) / 2 + ymp_y), Abs(X - tmp_y) / 2, , , , Abs(Y - tmp_y) / Abs(X - tmp_x)ElsePicture1.Circle (Abs(X - tmp_x) / 2 + tmp_x, Abs(Y - tmp_y) / 2 + ymp_y), Abs(Y - tmp_y) / 2, , , , Abs(Y - tmp_y) / Abs(X - tmp_x)End IfCase "选取"XR = X: YT = YCase "复制"XR = X: YT = Ytmp(0) = 1Case "剪切"XR = X: YT = Ytmp(0) = 2End SelectEnd SubPrivate Sub VScroll1_Change()Picture1.Top = -VScroll1.ValueEnd SubPrivate Sub VScroll1_Scroll()Picture1.Top = -VScroll1.ValueEnd SubPrivate Sub 保存_Click()CommonDialog1.ShowSaveSavePicture Picture1.Image, CommonDialog1.FileName End SubPrivate Sub 垂直旋转_Click()Dim i, j, col As LongIf XL > XR Thentmp_x = XL: XL = XR: XR = tmp_xEnd IfIf YB > YT Thentmp_x = YB: YB = YT: YT = tmp_xEnd IfFor i = XL To XRFor j = YB To YT / 2col = Picture1.Point(i, YT - j)Picture1.PSet (i, YT - j), Picture1.Point(i, j)Picture1.PSet (i, j), colNext jNext iEnd SubPrivate Sub 打开_Click()CommonDialog1.ShowOpenPicture1.Picture = LoadPicture(CommonDialog1.FileName) VScroll1.Min = 0HScroll1.Min = 0VScroll1.Max = Picture1.Height - Form1.ScaleHeightHScroll1.Max = Picture1.Width - Form1.ScaleWidthIf HScroll1.Max < 0 Then HScroll1.Enabled = FalseIf VScroll1.Max < 0 Then VScroll1.Enabled = FalseEnd SubPrivate Sub 反色_Click()Dim i, j As LongDim col, Red, Green, Blue As LongFor i = XL To XRFor j = YB To YTcol = Picture1.Point(i, j)Red = col Mod 256Green = (col \ 256) Mod 256Blue = col \ 256 \ 256Picture1.PSet (i, j), RGB(255 - Red, 255 - Green, 255 - Blue) Next jNext iEnd SubPrivate Sub 复制_Click()name1 = "复制"End SubPrivate Sub 工具箱_Click()If 工具箱.Caption = "▲工具箱(T)" Then工具箱.Caption = " 工具箱(T)"Frame1.Enabled = FalseElse工具箱.Caption = "▲工具箱(T)"Frame1.Enabled = TrueEnd IfHScrollEnd SubPrivate Sub 剪切_Click()name1 = "剪切"End SubPrivate Sub 清除图像_Click()Picture1.Picture = LoadPicture()Picture1.ClsEnd SubPrivate Sub 全选_Click()XL = 0: YB = 0XR = Picture1.Width: YT = Picture1.HeightEnd SubPrivate Sub 水平翻转_Click()Dim i, j, col As LongIf XL > XR Thentmp_x = XL: XL = XR: XR = tmp_xEnd IfIf YB > YT Thentmp_x = YB: YB = YT: YT = tmp_xEnd IfFor i = YB To YTFor j = XL To XR / 2col = Picture1.Point(XR - j, i)Picture1.PSet (XR - j, i), Picture1.Point(j, i)Picture1.PSet (j, i), colNext jNext iEnd SubPrivate Sub 退出_Click()EndEnd SubPrivate Sub 颜料盒_Click()If 颜料盒.Caption = "▲颜料盒(C)" Then颜料盒.Caption = " 颜料盒(C)"Frame2.Enabled = FalseElse颜料盒.Caption = "▲颜料盒(C)"Frame2.Enabled = TrueEnd IfHScrollEnd SubPrivate Sub 颜色编辑器_Click()CommonDialog1.ShowColorEnd SubPrivate Sub 粘贴_Click()name1 = "粘贴"End SubPrivate Sub 状态栏_Click()If 状态栏.Caption = "▲状态栏(S)" Then状态栏.Caption = " 状态栏(S)"Frame3.Enabled = FalseElse状态栏.Caption = "▲状态栏(S)"Frame3.Enabled = TrueEnd IfHScroll End Sub。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
软件:NetBeans 图形效果:代码:package newpackage;import java.awt.*;import javax.swing.*;import java.awt.geom.*;import java.awt.image.*;import .URL;import java.io.*;import javax.imageio.*;import java.awt.event.*;import java.util.Calendar;import javax.swing.*;public class Hello2D extends JApplet {public static void main(String s[]) {JFrame frame = new JFrame();frame.setTitle("计算机图形学");frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);JApplet applet = new Hello2D();applet.init();frame.getContentPane().add(applet);frame.pack();frame.setVisible(true);}public void init() {JPanel panel = new Hello2DPanel();getContentPane().add(panel);}}class Hello2DPanel extends JPanel implements ActionListener{ private BufferedImage image;AffineTransform rotH = new AffineTransform();AffineTransform rotM = new AffineTransform();AffineTransform rotS = new AffineTransform();// AffineTransform zuq=new AffineTransform ();public Hello2DPanel() {setPreferredSize(new Dimension(1400,1000));setBackground(Color.white);Timer timer=new Timer(500,this);timer.start();URL url=getClass().getClassLoader().getResource("images/zuqiu.jpg");try{image=ImageIO.read(url);}catch(IOException ex){ex.printStackTrace();}}@Overridepublic void paintComponent(Graphics g) {super.paintComponent(g);Graphics2D g2= (Graphics2D)g;g2.translate(100,100);g2.scale(0.5, 0.5);for (int i = 0; i < 12; i++) {g2.rotate(2*Math.PI/12);g2.fill3DRect(-3, -180, 6, 30, true);}Shape hour = new Line2D.Double(0, 0, 0, -80);hour = rotH.createTransformedShape(hour);Shape minute = new Line2D.Double(0, 0, 0, -120);minute = rotM.createTransformedShape(minute);Shape second = new Line2D.Double(0, 0, 0, -120);second = rotS.createTransformedShape(second);g2.setColor(Color.black);g2.setStroke(new BasicStroke(5, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND)); g2.draw(hour);g2.draw(minute);g2.setStroke(new BasicStroke(2));g2.draw(second);g2.scale(2,2);g2.translate(50,-150);g2.setColor(Color.DARK_GRAY);GeneralPath path=new GeneralPath();path.moveTo(200, 170);path.lineTo(800, 170);path.quadTo(950, 350, 800,530 );path.lineTo(200, 530);path.quadTo(50, 350, 200,170);Stroke stroke=new BasicStroke(4,BasicStroke.CAP_BUTT,BasicStroke.CAP_ROUND);//设置笔画g2.setStroke(stroke);g2.translate(-100, -100);//平移g2.scale(1.4,1.4 );//放大path.closePath();g2.draw(path);g2.setColor(Color.LIGHT_GRAY);g2.fill(path);Shape s1=new Rectangle2D.Double(300,250, 400, 200);Shape s2=new Ellipse2D.Double(200,250, 200, 200);Shape s3=new Ellipse2D.Double(600,250, 200, 200);g2.setColor(Color.DARK_GRAY);g2.draw(s1);g2.draw(s2);g2.draw(s3);g2.setColor(Color.GREEN);Area a1=new Area(s1);Area a2=new Area(s2);a1.add(a2);g2.fill(a1);Area a3=new Area(s3);a1.add(a3);g2.fill(s3);Font font=new Font("Serif",Font.BOLD,25);g2.setFont(font);GradientPaint gp=new GradientPaint(450,200,Color.red,220,220,Color.BLACK,true);g2.setPaint(gp);g2.drawString("四百米跑道",450, 220);g2.setColor(Color.BLACK);Stroke stroke1=new BasicStroke(1);//设置笔画g2.setStroke(stroke1);Shape s4=new Rectangle2D.Double(300,250, 400, 200);g2.draw(s4);g2.drawLine(500, 250, 500, 450);//划直线g2.drawOval(465, 315, 70, 70);g2.drawLine(300, 300, 350, 300);g2.drawLine(350, 300, 350,400);g2.drawLine(350, 400, 300,400);g2.drawLine(300, 325, 325, 325);g2.drawLine(325, 325, 325,375);g2.drawLine(325, 375, 300,375);g2.drawLine(700, 300, 650,300);g2.drawLine(650, 300, 650, 400);g2.drawLine(650, 400, 700,400);g2.drawLine(700, 325, 675,325);g2.drawLine(675, 325, 675, 375);g2.drawLine(675,375, 700,375);//绘制虚线float[] dashArray={20,20,20,20};g2.setColor(Color.BLACK);Font font2=new Font("Serif",Font.BOLD,15);g2.setFont(font2);g2.drawString("1",290,465);g2.drawString("2",290,485);g2.drawString("3",290,505);g2.drawString("4",290,525);g2.drawLine(300, 450,300 ,530);g2.setColor(Color.red);g2.drawLine(700, 450,700 ,530);float dashPhase=50;stroke =newBasicStroke(1,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL,0,dashArray,dashPhase);g2.setStroke(stroke);g2.drawLine(300, 470,700 ,470);g2.drawLine(300, 490,700 ,490);g2.drawLine(300, 510,700 ,510);Font font1=new Font("Serif",Font.BOLD,15);g2.setFont(font1);GradientPaint gp1=new GradientPaint(450,200,Color.BLACK,220,220,Color.red);g2.setPaint(gp1);g2.drawString("200米接力赛跑道",400,480);TexturePaint tp=new TexturePaint(image,new Rectangle2D.Double(550,370,image.getWidth(),image.getHeight()));g2.setPaint(tp);Shape zq=new Rectangle.Double(550,370,90,50);// zq=zuq.createTransformedShape(zq);g2.fill(zq);}@Overridepublic void actionPerformed(ActionEvent e) {int hour = Calendar.getInstance().get(Calendar.HOUR);int min = Calendar.getInstance().get(Calendar.MINUTE);int sec = Calendar.getInstance().get(Calendar.SECOND);rotH.setToRotation(Math.PI * (hour+min/60.0)/6.0);rotM.setToRotation(Math.PI * min /30.0);rotS.setToRotation(Math.PI * sec /30.0);repaint();}}。