注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

阿弥陀佛

街树飘影未见尘 潭月潜水了无声 般若观照心空静...

 
 
 

日志

 
 
关于我

一直从事气象预报、服务建模实践应用。 注重气象物理场、实况场、地理信息、本体知识库、分布式气象内容管理系统建立。 对Barnes客观分析, 小波,计算神经网络、信任传播、贝叶斯推理、专家系统、网络本体语言有一定体会。 一直使用Java、Delphi、Prolog、SQL编程。

网易考拉推荐

Descartes --2013-01-27修改  

2013-01-27 23:32:28|  分类: Java |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package graph;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.geom.Point2D;
import javax.swing.JPanel;

/**
 *
 * @author 何险峰
 * 2013-01-27 修改
 */
public class Descartes extends JPanel {
    public Descartes(int w,int h){
        X2Glb = w;
        Y2Glb = h;
        this.setBounds(0, 0, w, h);
    }
    @Override
    protected void paintComponent(Graphics graph) {
        super.paintComponent(graph);
        g = graph;
        g.setColor(Color.WHITE);
        Rectangle r = this.getBounds();
        g.clearRect(0, 0, r.width, r.height);
        DefineWindow(0,0,r.width,r.height,true);
        Clear();
    }
    protected Graphics g;
    protected float X1Wld = 1,  X2Wld = 1000,  Y1Wld = 1,  Y2Wld = 1000,  Bx,  By;          //取值范围
    protected int X1Wnd = 0,  X2Wnd = 400,  Y1Wnd = 1,  Y2Wnd = 400, //坐标轴范围内窗口
             X1Glb = 0,  X2Glb = 400,  Y1Glb = 1,  Y2Glb = 400,  DX = 2,  DY = 2;       //窗口范围;DX,DY 坐标值的间隔
    public boolean Border = true;

    public Graphics get() {
        return g;
    }
    //获取World

    public void GetWorld(Float WldX1, Float WldY1, Float WldX2, Float WldY2) {
        WldX1 = new Float(X1Wld);
        WldY1 = new Float(Y1Wld);
        WldX2 = new Float(X2Wld);
        WldY2 = new Float(Y2Wld);
    }

    public void DefineWorld(float X1, float Y1, float X2, float Y2) {
        if ((X1 != X2) && (Y1 != Y2)) {
            X1Wld = X1;
            Y1Wld = Y1;
            X2Wld = X2;
            Y2Wld = Y2;
        }
        Bx = (X2Wnd - X1Wnd) / (X2Wld - X1Wld);
        By = (Y2Wnd - Y1Wnd) / (Y2Wld - Y1Wld);
    }

    public void DefineGlb(int x1, int y1, int x2, int y2) {
        X1Glb = x1;
        Y1Glb = y1;
        X2Glb = x2;
        Y2Glb = y2;
    //SvgIni(x1,y1,x2,y2);
    //DefineWindow(X1Glb,Y1Glb,X2Glb,Y2Glb,true);
    }
    //定义窗口范围,在调用之前应该先调用DefineWorld

    public void DefineWindow(int X1, int Y1, int X2, int Y2, boolean IsBorder) {
        //当全局窗口不能够涵盖局部窗口时,加大全局窗口
        if (X1Glb > X1) {
            X1Glb = X1;
        }
        if (Y1Glb < Y1) {
            Y1Glb = Y1;
        }
        if (X2Glb < X2) {
            X2Glb = X2;
        }
        if (Y2Glb < Y2) {
            Y2Glb = Y2;
        }

        Border = IsBorder;
        if (Border) {
            Color old = g.getColor();
            g.setColor(Color.WHITE);
            g.drawRect(X1, Y1, X2 - X1 + 1, Y2 - Y1 + 1);
            g.setColor(old);

            X1Wnd = X1 + 2;
            Y1Wnd = Y1 + 2;
            X2Wnd = X2 - 2;
            Y2Wnd = Y2 - 2;
        } else {
            X1Wnd = X1;
            Y1Wnd = Y1;
            X2Wnd = X2;
            Y2Wnd = Y2;
        }
        Bx = (X2Wnd - X1Wnd) / (X2Wld - X1Wld);
        By = (Y2Wnd - Y1Wnd) / (Y2Wld - Y1Wld);
    }

    public void Clear() {
        Color oldcolor = g.getColor();
        g.setColor(Color.white);
        g.clearRect(X1Wnd, Y1Wnd, X2Wnd, Y2Wnd);
        g.fillRect(X1Wnd, Y1Wnd, X2Wnd, Y2Wnd);
        g.setColor(oldcolor);
    }

    public void Write(int X, int Y, String S, int size, Color color) {
        int XX, YY;
        XX = X1Wnd + size * (X - 1);
        YY = Y1Wnd + size * (Y - 1);
        if (XX >= X2Wnd) {
            XX = X2Wnd - (S.length() + 5);
        }
        if (YY > Y2Wnd) {
            YY = Y2Wnd - (size + 5);
        }
        Color oldcolor = g.getColor();
        Font oldfont = g.getFont();
        g.setColor(color);
        Font font = new Font("宋体", Font.PLAIN, size);
        g.setFont(font);
        g.drawString(S, XX, YY);
        g.setColor(oldcolor);
        g.setFont(oldfont);
    }

    public void Write(int x, int y, String s) {
        Write(x, y, s, 10, Color.BLACK);
    }

    public void Write(Point2D p, String s, Color color, int size) {
        int x = ScrX((float) p.getX()), y = ScrY((float) p.getY());
        Color oldcolor = g.getColor();
        Font oldfont = g.getFont();
        g.setColor(color);
        Font font = new Font("宋体", Font.PLAIN, size);
        g.setFont(font);
        g.drawString(s, x, y);
        g.setColor(oldcolor);
        g.setFont(oldfont);
    }
    //由世界坐标返回屏幕坐标

    public int ScrX(float X) {
        float Temp;
        if (X < X1Wld) {
            Temp = X1Wnd;
        } else if (X > X2Wld) {
            Temp = X2Wnd;
        } else {
            Temp = Bx * (X - X1Wld) + X1Wnd;
        }
        return (int) Math.round(Temp);
    }

    public int ScrY(float Y) {
        float Temp;
        if (Y < Y1Wld) {
            Temp = Y2Wnd;
        } else if (Y > Y2Wld) {
            Temp = Y1Wnd;
        } else {
            Temp = Y2Wnd - By * (Y - Y1Wld);
        }
        return (int) Math.round(Temp);
    }
    //由屏幕坐标返回世界坐标

    public float WldX(int X) {
        float Temp;
        if (X < X1Wnd) {
            Temp = X1Wld;
        } else if (X > X2Wnd) {
            Temp = X2Wld;
        } else {
            Temp = 1 / Bx * (X - X1Wnd) + X1Wld;
        }
        return (int) Math.round(Temp);
    }

    public float WldY(int Y) {
        float Temp;
        if (Y < Y1Wnd) {
            Temp = Y2Wld;
        } else if (Y > Y2Wnd) {
            Temp = Y1Wld;
        } else {
            Temp = Y2Wld - 1 / By * (Y - Y1Wnd);
        }
        return (int) Math.round(Temp);
    }

    public void DrawCircal(float Radius, Color color) {
        int x1, y1, r;
        x1 = ScrX(X1Wld);
        y1 = ScrY(Y2Wld);
        r = ScrX(Radius);
        Color old = g.getColor();
        g.setColor(color);
        g.drawOval(x1, y1, r, r);

        g.setColor(old);
    }

    public void DrawArc(float x, float y, float w, float h, int startAg, int arcAg) {
        int i = ScrX(x),
                j = ScrY(y),
                iw = ScrX(w),
                ih = ScrY(h);
        g.drawArc(i, j, iw, ih, startAg, arcAg);
    }

    public void DrawLine(float X1, float Y1, float X2, float Y2, Color color) {
        int X1Loc, Y1Loc, X2Loc, Y2Loc;
        X1Loc = ScrX(X1);
        Y1Loc = ScrY(Y1);
        X2Loc = ScrX(X2);
        Y2Loc = ScrY(Y2);
        Color old = g.getColor();
        g.setColor(color);
        g.drawLine(X1Loc, Y1Loc, X2Loc, Y2Loc);
        g.setColor(old);
    }

    public void DrawBackground(Color penColor, Color brush) {
        Color old = g.getColor();
        g.setColor(penColor);
        g.drawRect(X1Glb, Y1Glb, X2Glb - X1Glb + 1, Y2Glb - Y1Glb + 1);
        g.setColor(brush);
        g.fillRect(DX, DX, X2Glb - X1Glb, Y2Glb - Y1Glb);
        g.setColor(old);
    }

    public void ResetAxis(Color penColor, Color brush) {
        DrawBackground(penColor, brush);
        Axis(DX, DY);
    }

    public void ResetAxis() {
        ResetAxis(Color.RED, Color.WHITE);
    }

    public void Axis(int DDX, int DDY) {
    }
    public void oChar(float X, float Y) {
      oChar(X,Y,"o");
    }

    public void oChar(float X, float Y, String ch) {
        int XX = ScrX(X), YY = ScrY(Y);
        Font font = new Font("宋体", Font.PLAIN, 6);
        Color old = g.getColor();
        g.setColor(Color.RED);
        g.drawString(ch, XX, YY);
        g.setColor(old);

    }
     public void xChar(float X, float Y) {
         xChar(X,Y,"x");
     }

    public void xChar(float X, float Y,String ch) {
        int XX = ScrX(X), YY = ScrY(Y);
        Font font = new Font("宋体", Font.PLAIN, 6);
        Color old = g.getColor();
        g.setColor(Color.BLUE);
        g.drawString(ch, XX, YY);
        g.setColor(old);
    }

    public void FindWorld(Point2D[] Points, float ScaleX, float ScaleY) {
        double Xmax, Ymax, Xmin, Ymin, Xmid, Ymid, Xdiff, Ydiff;
        Xmax = Points[0].getX();
        Ymax = Points[0].getY();
        Xmin = Xmax;
        Ymin = Ymax;
        if (Points.length > 1) {
            for (int i = 0; i < Points.length; i++) {
                if (Points[i] == null) {
                    continue;
                }
                if (Points[i].getX() > Xmax) {
                    Xmax = Points[i].getX();
                } else if (Points[i].getX() < Xmin) {
                    Xmin = Points[i].getX();
                }

                if (Points[i].getY() > Ymax) {
                    Ymax = Points[i].getY();
                } else if (Points[i].getY() < Ymin) {
                    Ymin = Points[i].getY();
                }
            }
        }
        if (ScaleX != 1.0) {
            ScaleX = Math.abs(ScaleX);
            Xdiff = Xmax - Xmin;
            Xmid = (Xmax + Xmin) * 0.5f;
            Xmax = Xmid + ScaleX * 0.5f * Xdiff;
            Xmin = Xmid - ScaleX * 0.5f * Xdiff;
        }
        if (ScaleY != 1.0) {
            ScaleY = Math.abs(ScaleY);
            Ydiff = Ymax - Ymin;
            Ymid = (Ymax + Ymin) * 0.5f;
            Ymax = Ymid + ScaleY * 0.5f * Ydiff;
            Ymin = Ymid - ScaleY * 0.5f * Ydiff;
        }
        DefineWorld((float) Xmin, (float) Ymin, (float) Xmax, (float) Ymax);
    }

    public void DrawPolygon(Point2D[] Points, Color color, Color fillcolor) {
        int xs[] = new int[Points.length];
        int ys[] = new int[Points.length];

        for (int i = 0; i < Points.length; i++) {
            xs[i] = ScrX((float) Points[i].getX());
            ys[i] = ScrY((float) Points[i].getY());
        }
        Color old = g.getColor();
        g.setColor(color);
        g.drawPolygon(xs, ys, Points.length);
        g.setColor(fillcolor);
        g.fillPolygon(xs, ys, Points.length);

        g.setColor(old);
    }

    public void DrawPolyline(Point2D[] Points, Color color) {
        int xs[] = new int[Points.length];
        int ys[] = new int[Points.length];

        for (int i = 0; i < Points.length; i++) {
            xs[i] = ScrX((float) Points[i].getX());
            ys[i] = ScrY((float) Points[i].getY());
        }
        Color old = g.getColor();
        g.setColor(color);
        g.drawPolyline(xs, ys, Points.length);
        g.setColor(old);
    }

    public void DrawPoint(float xx, float yy, Color color) {
        DrawLine(xx, yy, xx + 1, yy + 1, color);
    }
}
  评论这张
 
阅读(633)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017