⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 object2d.java

📁 java基本图形界面的生成源代码
💻 JAVA
字号:
package graphics0;

import java.awt.*;
import java.awt.image.*;
import java.awt.color.*;
import java.util.Stack;

/**
 * <p>Title: computer graphics </p>
 * <p>Description: homeworks</p>
 * <p>Copyright: Copyright (c) 2004 </p>
 * <p>Company: shu</p>
 * @author dxf
 * @version 1.0
 */
abstract class Object2D {
  protected int LineWidth = 1;
  protected Color ForeColor = Color.BLACK, BackgroundColor = Color.WHITE;
  protected int LineStyle = 0;
  protected boolean Filled;

  static int cWidth = 1024;
  static int cHeight = 768;
  static int LEFT= 1,RIGHT= 2,BOTTOM= 4,TOP= 8;
  static String[] sLineStyle = {
      "11110000", "11111111", "11001100"};
  static int DemoWidth = 5;

  public void SetLineWidth(int width) {
    LineWidth = width;
  }

  public int GetLineWidth() {
    return (LineWidth);
  }

  public void SetFilled(boolean b) {
    Filled = b;
  }

  public boolean GetFilled() {
    return (Filled);
  }

  public void SetLineStyle(int l) {
    LineStyle = l;
  }

  public int GetLineStyle() {
    return (LineStyle);
  }

  public void SetForeColor(Color c1) {
    ForeColor = c1;
  }

  public Color GetForeColor() {
    return (ForeColor);
  }

  public void SetBackgroundColor(Color c1) {
    BackgroundColor = c1;
  }

  public Color GetBackgroundColor() {
    return (BackgroundColor);
  }

////////////////////////////////////////////////////////////////////////////
  static public void FloodFill4(int x, int y, Color fc, Color bc,
                                BufferedImage b, Graphics2D g) {
    if (! ( (x < 0) || (y < 0) || (x >= cWidth) || (y >= cHeight))) {
      if ( (b.getRGB(x, y) != fc.getRGB()) &&
          (b.getRGB(x, y) != bc.getRGB())) {
        g.setColor(bc);
        g.drawLine(x, y, x, y);
        FloodFill4(x, y + 1, fc, bc, b, g);
        FloodFill4(x, y - 1, fc, bc, b, g);
        FloodFill4(x - 1, y, fc, bc, b, g);
        FloodFill4(x + 1, y, fc, bc, b, g);
      }
    }
  }

///////////////////////////////////////////////////////////////////////////////
  static public void FloodFill8(int x, int y, Color fc, Color bc,
                                BufferedImage b, Graphics2D g) {
    if (! ( (x < 0) || (y < 0) || (x >= cWidth) || (y >= cHeight))) {
      if ( (b.getRGB(x, y) != fc.getRGB()) &&
          (b.getRGB(x, y) != bc.getRGB())) {
        g.setColor(bc);
        g.drawLine(x, y, x, y);
        FloodFill4(x, y + 1, fc, bc, b, g);
        FloodFill4(x, y - 1, fc, bc, b, g);
        FloodFill4(x - 1, y, fc, bc, b, g);
        FloodFill4(x + 1, y, fc, bc, b, g);

        FloodFill4(x - 1, y - 1, fc, bc, b, g);
        FloodFill4(x + 1, y - 1, fc, bc, b, g);
        FloodFill4(x - 1, y + 1, fc, bc, b, g);
        FloodFill4(x + 1, y + 1, fc, bc, b, g);

      }
    }
  }

/////////////////////////////////////////////////////////////////////////////////
  static public void ScanLineFill4(int x, int y, int iColor ,Color bc,
                                   BufferedImage b, Graphics2D g) {
    int xl, xr, i;
    boolean spanNeedFill;
    Point pt = new Point(0, 0);
    Stack stack0 = new Stack();
    Point p0;

    stack0.empty();
    pt.x = x;
    pt.y = y;

    stack0.push(pt); //将前面生成的区段压入堆栈

    while (!stack0.isEmpty()) { //检查堆栈状态,空返回F,否则返回T
      pt = (Point) stack0.pop(); //取堆栈顶元素
      y = pt.y;
      x = pt.x;
      while (b.getRGB(x, y) == iColor) {
        g.setColor(bc);
        g.drawLine(x, y, x, y);
        x++;
      }
      xr = x - 1;
      x = pt.x - 1;

      while (b.getRGB(x, y) == iColor) { //向左填充
        g.setColor(bc);
        g.drawLine(x, y, x, y);
        x--;
      }
      xl = x + 1;

      x = xl;
      y = y + 1; //处理上面一条扫描线

      while (x < xr) {
        spanNeedFill = false;

        while (b.getRGB(x, y) == iColor) {
          spanNeedFill = true;         x++;
        }

        if (spanNeedFill) {
          p0 = new Point(0, 0);
          p0.x = x - 1;          p0.y = y;
          stack0.push(p0);
          spanNeedFill = false;
        }

        while (b.getRGB(x, y) != iColor && x < xr)  x++;
      } //End of while(x<xr)

      //处理下面一条扫描线
      x = xl;
      y = y - 2;
      while (x < xr) {
        spanNeedFill = false;

        while (b.getRGB(x, y) == iColor) {
          spanNeedFill = true;        x++;
        }

        if (spanNeedFill) {
          p0 = new Point(0, 0);
          p0.x = x - 1;          p0.y = y;
          stack0.push(p0);
          spanNeedFill = false;
        }

        while (b.getRGB(x, y) != iColor && x < xr)       x++;

      } //End of while(x<xr)

    } //End of while(!isstackempty())

  }
////////////////////////////////////////////////////////////////////////////////
//求两点间距离
  static public float distance2(float x1,float y1,float x2,float y2) {
    return ((x1 - x2)*(x1 - x2)+(y1 - y2)*(y1 - y2));

  }

//////////////////////////////////////////////////////////////////////////////
  //lint mid clip
  static public void Mid_LineClip(Point lsp,Point lep,
                                 Point wsp,Point wep,
                                  Color bk,Graphics2D g){
    int code1, code2;
    int XL, XR, YB, YT;
    int  x1 = lsp.x, x2 = lep.x, y1 = lsp.y, y2 = lep.y;

    g.setColor(bk);
    if (wsp.x > wep.x) {
      XR = wsp.x; XL = wep.x;
    }
    else {
      XR = wep.x;  XL = wsp.x;
    }
    if (wsp.y > wep.y) {
      YB = wsp.y;  YT = wep.y;
    }
    else {
      YB = wep.y; YT = wsp.y;
    }

    code1 = encode(x1, y1, XL, XR, YB, YT);
    code2 = encode(x2, y2, XL, XR, YB, YT);

    if ((code1&code2) !=0) {//显然不可见
      x1=x2=y1=y2=0;
    }
    else if ((code1==0)&&(code2==0)) {//显然可见
      g.drawLine(x1,y1,x2,y2);
    }
    else {//其它
      Point sp = new Point(0,0),ep = new Point(0,0);
      sp = midPoint( x1, y1, x2, y2, XL,XR,YB,YT) ;
      ep = midPoint( x2, y2, x1, y1, XL,XR,YB,YT) ;
      g.drawLine(sp.x,sp.y,ep.x,ep.y);
   }

  }
///////////////////////////////////////////////////////////////////////////
  //midpoint
  static Point midPoint(float x1,float y1,float x2,float y2,
                        int XL,int XR,int YB,int YT) {

    float xn,yn;
    int code1, code2;
    float delta = (float)0.25;
    while (true){
      xn = (x1 + x2) / 2;
      yn = (y1 + y2) / 2;

      if (distance2(xn, yn, x2, y2) < delta) {
        break;
      }
      else {
        code1 = encode(x1, y1, XL, XR, YB, YT);
        code2 = encode(xn, yn, XL, XR, YB, YT);
        if ( (code1 & code2) != 0) {
          x1 = xn; y1 =yn;
        }
        else {
          x2 =xn; y2 = yn;
        }
      }
    } //end of while(true)
    Point p0 = new Point(0,0);
    p0.x = Math.round(xn); p0.y = Math.round(yn);
    return (p0);
  }
////////////////////////////////////////////////////////////////////////////
  static int encode(int x,int y,int XL,int XR,int YB,int YT) {
    int c=0;
    if(x<XL) c|=LEFT;
    if(x>XR) c|=RIGHT;
    if(y>YB) c|=BOTTOM;
    if(y<YT) c|=TOP;
    return c ;
}
////////////////////////////////////////////////////////////////////////////
  static int encode(float x,float y,int XL,int XR,int YB,int YT) {
    int c=0;
    if(x<XL) c|=LEFT;
    if(x>XR) c|=RIGHT;
    if(y>YB) c|=BOTTOM;
    if(y<YT) c|=TOP;
    return c ;
}

//////////////////////////////////////////////////////////////////////////////
  //line cs clip
  static public void CS_LineClip(Point lsp,Point lep,
                                 Point wsp,Point wep,
                                 Color bk,Graphics2D g){
    int code1,code2,code;
    int XL,XR,YB,YT,x=0,y=0;
    int x1=lsp.x,x2=lep.x,y1=lsp.y,y2=lep.y;
    if (wsp.x>wep.x) {
      XR=wsp.x;XL=wep.x;
    }
    else {
      XR=wep.x;XL=wsp.x;
    }
    if (wsp.y>wep.y) {
      YB=wsp.y;YT=wep.y;
    }
    else {
      YB=wep.y;YT=wsp.y;
    }

    code1= encode(x1,y1,XL,XR,YB,YT);
    code2= encode(x2,y2,XL,XR,YB,YT);

     while(code1!=0 ||code2!=0)
     {
       if ((code1&code2) !=0) {
         x1=x2=y1=y2=0;
         break;
       }

       code = code1;

       if(code1==0) code = code2;

       if((LEFT&code) !=0)  {
         x=XL;
         y=y1+(y2-y1)*(XL-x1)/(x2-x1);

       }
       else if ((RIGHT&code) !=0) {
           x=XR;
           y=y1+(y2-y1)*(XR-x1)/(x2-x1);
         }
       else if((BOTTOM&code) !=0) {
         y=YB;
         x=x1+(x2-x1)*(YB-y1)/(y2-y1);
        }
       else if((TOP & code) !=0)  {
         y=YT;
         x=x1+(x2-x1)*(YT-y1)/(y2-y1);
        }

        if(code ==code1)   {
          x1=x; y1=y;
          code1 =encode(x,y,XL,XR,YB,YT);
        }
        else    {
          x2=x;y2=y;
          code2 =encode(x,y,XL,XR,YB,YT);
        }

     }

     g.setColor(bk);
     g.drawLine(x1,y1,x2,y2);

  }

  abstract void draw(Graphics Canvas);

  abstract void drawDemo(Graphics Canvas);

  abstract boolean IsInside(Point p0);

  abstract void scale(double sx, double sy);
  abstract void rotate(double theta, int x, int y) ;
  abstract void translate(int x, int y);

  public Object2D() {
  }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -