注:【网页版】右上方的悬浮框( 有目录索引 )
一、打印机选择墨盒与纸张进行打印
1-1】效果图 及 包结构
效果图 | 包结构 |
---|---|
![]() | ![]() |
1-2】接口 与 类
com.printer 包下
public interface Ink {
public String getColor(int r,int g,int b);
}
public interface Paper {
// 换行标准
public static final String newLine="\r\n";
// 往纸张里面逐个字符输入内容
public void putInChar(char c);
// 得到纸张中的内容
public String getContent();
}
public class Printer {
// 面向接口编程,定义 接口变量
private Ink ink;
private Paper paper;
public Ink getInk() { return ink; }
public void setInk(Ink ink) { this.ink = ink; }
public Paper getPaper() { return paper; }
public void setPaper(Paper paper) { this.paper = paper; }
public void print(String str){
System.out.println("使用"+ink.getColor(225,220,5)+"颜色打印");
for(int i=0;i<str.length();i++){
paper.putInChar(str.charAt(i));
}
System.out.println(paper.getContent());
}
}
com.ink 包下
public class ColorInk implements Ink {
@Override
public String getColor(int r, int g, int b) {
Color color = new Color(r,g,b);
return "#"+Integer.toHexString(color.getRGB()).substring(2);
}
}
public class GreyInk implements Ink {
@Override
public String getColor(int r, int g, int b) {
int c = (r+g+b)/3;
Color color = new Color(c,c,c);
return "#"+Integer.toHexString(color.getRGB()).substring(2);
}
}
public class TextPaper implements Paper {
private int charPerLine = 16; // 一行有 16 字符
private int linePerPage = 8; // 一张有 18 行
private int posx = 0; // 横向位置 从 0 至 charPerLine-1
private int posy = 0; // 当前行号
private int posp = 1; // 当前页号
private String content = "";
public int getCharPerLine() {
return charPerLine;
}
public void setCharPerLine(int charPerLine) {
this.charPerLine = charPerLine;
}
public int getLinePerPage() {
return linePerPage;
}
public void setLinePerPage(int linePerPage) {
this.linePerPage = linePerPage;
}
@Override
public void putInChar(char c) {
content +=c;
++posx;
// 判断换行
if(posx == charPerLine){
content += Paper.newLine;
posx = 0;
++posy;
}
// 判断换页
if(posy == linePerPage){
content +="===第"+posp+"页===";
content += Paper.newLine + Paper.newLine;
posy = 0;
++posp;
}
}
@Override
public String getContent() {
// 为当前页加上页号
String ret = content;
if(!(posx==0&&posy==0)){
// 当前页不为空
int count = linePerPage - posy;
for (int i=0;i < count;i++){
// 为了保证每一页大小一致。给空的行,添加换行
ret += Paper.newLine + Paper.newLine;
}
ret +="===第"+posp+"页===";
}
return ret;
}
}
1-3】配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean class="com.ink.GreyInk" id="greyInk"/>
<bean class="com.ink.ColorInk" id="colorInk"/>
<bean class="com.ink.TextPaper" id="paperA4">
<property name="charPerLine" value="8"/>
<property name="linePerPage" value="6"/>
</bean>
<bean class="com.ink.TextPaper" id="paperB5">
<property name="charPerLine" value="6"/>
<property name="linePerPage" value="5"/>
</bean>
<bean class="com.printer.Printer" id="printer">
<property name="ink" ref="greyInk"/>
<property name="paper" ref="paperB5"/>
</bean>
</beans>
1-4】测试类
@Test
public void test(){
ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
Printer printer = (Printer) context.getBean("printer");
printer.print("一支玫瑰入眼眸,一壶烈酒融入喉,一缕清风伴长久" +
"思念如同循环小数,一遍一遍执迷不悟" +
"伊茉琳是一个恶魔,以他人的痛苦为生,并以此为乐" +
"即是金克斯也会专注于一件事,冷静下来摧毁对面水晶");
}