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

📄 letter.java

📁 用java写的画图板程序
💻 JAVA
字号:
/**
 * Letter.java
 * 
 * Created on September 1,2008, 17:40 
 * 
 * 字符串的绘制,直接实现IShape接口
 */
package draw;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 * @author 王宽
 * 
 */
public class Letter implements IShape{
	private Color color;
	
	private String string1,string2;
	
	private int style,size;
	
	private int currX, currY;
	
	private Font f;
	
	private int operate = 4,movX, movY;
	
	private IShape shape;
	
	private int translate = 0, rotate = 0,scale = 0,shear = 0;
	
	private double pi = 0.0, sx = 1.0, sy = 1.0, shx = 0.0, shy = 0.0;
	 /**
     * 构造函数
     * 需要画笔色,字体
     * 字体粗细,字体类型,和鼠标坐标
     */
	public Letter(Color c, String s, int si, int st, int x, int y){
		color = c;
		string1 = s;
		size = si;
		style = st;
		currX = x;
        currY = y;       
        string2 = JOptionPane.showInputDialog(null,"Please input the string:","Message",JOptionPane.PLAIN_MESSAGE);
        if(string2 == null) string2="";
	}
	 /**
     * 默认构造函数
     */
	public Letter(){
		 
	}
	 /**
     * 构造函数
     * 用于修改
     */
	public Letter(int op,IShape sh){
		shape = sh;
    	this.setShapeData(shape.getShapeData());
    	operate =op;
    	if(operate == 0){
    		translate = 1;
    	}
    	if(operate == 1){
    		rotate = 1;
    	}
    	if(operate == 2){
    		scale = 1;
    	} 
    	if(operate == 3){
    		shear = 1;
    	}
	}
	
	public void processCursorEvent(MouseEvent e, int t) {
		if (t == IShape.CURSOR_DRAGGED){
        	int x = e.getX();
        	int y = e.getY();
        	if(operate == 0){
        		movX = x - currX;
            	movY = y - currY;
            	return;
        	}
        	if(operate == 1){
        		pi = -(x - currX)*Math.PI/500;
            	return;
        	}
        	if(operate == 2){
        		sx = (x - currX)*size/1000.0;
        		sy = (y - currY)*size/1000.0;
        		return;
        	}
        	if(operate == 3){
        		shx = (x - currX)/200.0;
        		shy = (y - currY)/200.0;
        	}
        	if(operate == 4){
        	}
        	      	
        }
   
	}
	
	/**
     * 绘图函数
     * 根据参数画需要的文本
     */
    public void draw(Graphics2D g) {
        f = new Font(string1,style,size); 
        g.setColor(color);
        g.setFont(f);
        if(translate == 1)g.translate(movX, movY);
    	//System.out.println(pi);
    	if(rotate == 1) g.rotate(pi,currX,currY);
    	if(scale == 1)  { g.translate(currX,currY);g.scale(sx, sy);g.translate(-currX,-currY);}
        if(shear == 1)  { g.translate(currX,currY);g.shear(shx, shy);g.translate(-currX,-currY);}
        g.drawString(string2, currX, currY);
        if(shear == 1)  { g.translate(currX,currY);g.scale(1/(1-shx*shy), 1/(1-shx*shy));g.shear(-shx, -shy);g.translate(-currX,-currY);}
        if(scale == 1) {  g.translate(currX,currY);g.scale(1/sx, 1/sy);g.translate(-currX,-currY);}
        if(rotate == 1) g.rotate(-pi, currX, currY);
        if(translate == 1) g.translate(-movX, -movY);
    } 

	public String getShapeData() {
        int s1 = 0;
        for (int i=0; i<DrawingBoard.STYLE.length; i++) {
            if (style == DrawingBoard.STYLE[i]) {
                s1 = i;
                break;
            }
        }
        int s2 = 0;
        for (int i=0; i<DrawingBoard.SIZE.length; i++) {
            if (size == DrawingBoard.SIZE[i]) {
                s2 = i;
                break;
            }
        }
        int s3 = 0;
        for (int i=0; i<DrawingBoard.FONTS.length; i++) {
            if (string1.equals( DrawingBoard.FONTS[i])) {
                s3 = i;
                break;
            }
        }
        
        StringBuffer buffer = new StringBuffer();
        buffer.append(color.getRGB());
        buffer.append(":");
        buffer.append(s1);
        buffer.append(":");
        buffer.append(s3);
        buffer.append(":");
        buffer.append(string2);
        buffer.append(":");
        buffer.append(currX);
        buffer.append(":");
        buffer.append(currY);
        buffer.append(":");
        buffer.append(s2);
        buffer.append(":");
        buffer.append(movX);
        buffer.append(":");
        buffer.append(movY);
        buffer.append(":");
        buffer.append(translate);
        buffer.append(":");
        buffer.append(rotate);
        buffer.append(":");
        buffer.append(pi);
        buffer.append(":");
        buffer.append(sx);
        buffer.append(":");
        buffer.append(sy);
        buffer.append(":");
        buffer.append(scale);
        buffer.append(":");
        buffer.append(shx);
        buffer.append(":");
        buffer.append(shy);
        buffer.append(":");
        buffer.append(shear);
        return buffer.toString();
	}

	public void setShapeData(String data)  {
		String[] splits = data.split(":");
        color = new Color(Integer.parseInt(splits[0]));
        style = DrawingBoard.STYLE[Integer.parseInt(splits[1])];
        size = DrawingBoard.SIZE[Integer.parseInt(splits[6])];
        string1 = DrawingBoard.FONTS[Integer.parseInt(splits[2])];
        string2 = splits[3];
        currX = Integer.parseInt(splits[4]);
        currY = Integer.parseInt(splits[5]);
        movX = Integer.parseInt(splits[7]);
        movY = Integer.parseInt(splits[8]);
        translate = Integer.parseInt(splits[9]);
        rotate = Integer.parseInt(splits[10]);
        pi = Double.parseDouble(splits[11]);
        sx = Double.parseDouble(splits[12]);
        sy = Double.parseDouble(splits[13]);
        scale = Integer.parseInt(splits[14]);
        shx = Double.parseDouble(splits[15]);
        shy = Double.parseDouble(splits[16]);
        shear = Integer.parseInt(splits[17]);
			
	}
	
	

}

⌨️ 快捷键说明

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