我对Java编程仍然有点新意,对于大量的文本转储感到遗憾。我非常感谢您花时间阅读我当前的问题!
我正在开发软件,以帮助加快使用Java Swing进行棋盘游戏设计的过程。它需要一个游戏卡的CSV文件,让你通过放置每个列将在卡上呈现的位置来构建虚拟卡,然后从这些位置自动生成CSV中的所有卡。
许多纸牌游戏都有符号代表游戏中的某些内容,我希望能够将它们插入到字符串的中间。我现在可以用符号替换整个字符串;因为它检查是否string == a known rule
,而是绘制符号。但是,我不知道如何通过字符串搜索特定字符集。如果找到它们,则从字符串中删除它们,然后在其中绘制相应的符号。魔术卡上的法术力符号就是一个很好的例子:https://cdn0.vox-cdn.com/uploads/chorus_asset/file/8039357/C0cIVZ5.png
所以字符串可能是:Gain 1 {GOLD} at the start of each tun.
它需要使用包含要查找的字符串的规则类替换{GOLD}
和黄金图片,并使用缓冲图像替换它。
我希望这可以在不使用符号大小的硬限制的情况下工作,但这不是一个硬性要求。最好的解决方案是缩放符号,使其高度与文本相同。
此方法采用缓冲图像(没有文本的卡片)并将文本叠加在卡片顶部。
//Will modify the buffered image with the placeables
static public BufferedImage buildCard(BufferedImage start, int whichCardID) {
//Copy so we don't lose our template
BufferedImage ni = deepCopy(start); //ni = new image
//The headers of the document
String[] headers = MainWindow.loadedCards.get(0);
//For each placeable, write down it's text
for(int i=0; i<headers.length; i++) {
//get current header
String currentHeader = headers[i];
//The Text
String theText = MainWindow.loadedCards.get(whichCardID)[i];
//The Settings
PlaceableSettings theSettings = MainWindow.placeableSettings.get(currentHeader);
//Make the change to the image
//ni = writeToImage(ni, theText, theSettings);
///////New below
boolean foundRule = false;
//see if we have a rule to draw a graphic instead
for(RuleMaster.Rule r : RuleMaster.rules) {
if(r.keyword.equals(theText)) {
//there is a rule for this!
ni = drawRuleToImage(ni, r, theSettings);
foundRule = true; //so we don't draw the current text
}
}
//No rules for this
//Make the change to the image if there are no rules
if(foundRule == false)
ni = writeToImage(ni, theText, theSettings);
}
return ni;
}
//Takes a buffered image and writes text into it at the location given
static public BufferedImage writeToImage(BufferedImage old, String text, PlaceableSettings setts) {
//make new blank graphics
BufferedImage bi = new BufferedImage(old.getWidth(), old.getHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = bi.createGraphics();
//write old image to it
g2d.drawImage(old, 0, 0, null); //null was set to "this" when this was not static | Note ion case this breaks
//write text on it
g2d.setPaint(setts.getColor());
g2d.setFont(setts.getFont());
//Setup word wrap
FontMetrics fm = g2d.getFontMetrics(setts.getFont());
// int rightSideBuffer = bi.getWidth() - 10;
//Rectangle2D rect = fm.getStringBounds(text, setts.getX(), rightSideBuffer, g2d); // try just -'ing the x slot from the width below
Rectangle2D rect = fm.getStringBounds(text, g2d); //this gets you the bounds for the entire image, need to remove space for x,y position
//TODO: Problem: this is always length 1
//Solution! No auto wrap, let the person define it as a setting
@SuppressWarnings("unchecked")
List<String> textList=StringUtils.wrap(text, fm, setts.getPixelsTillWrap() ); //width counted in # of characters
//g2d.drawString(text, setts.getX(), setts.getY()); //old draw with no wrap
for(int i=0; i< textList.size(); i++) {
g2d.drawString(textList.get(i), setts.getX(), setts.getY() + ( i*(setts.getFont().getSize() + 2/*Buffer*/)));
}
//!!DEBUG
if(EntryPoint.DEBUG) {
Random r = new Random();
g2d.setPaint(Color.RED);
g2d.drawString(Integer.toString(textList.size()), 100, 50+r.nextInt(250));
g2d.setPaint(Color.GREEN);
g2d.drawString(Double.toString(rect.getWidth()), 200, 50+r.nextInt(250));
g2d.setPaint(Color.PINK);
//g2d.drawString(Integer.toString(( ((int) rect.getWidth()) - setts.getX())), 100, 250+r.nextInt(100));
}
//cleanup
g2d.dispose();
return bi;
}
//Takes a buffered image and draws an image on it at the location given
static public BufferedImage drawRuleToImage(BufferedImage old, RuleMaster.Rule rule, PlaceableSettings theSettings) {
//make new blank graphics
BufferedImage bi = new BufferedImage(old.getWidth(), old.getHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = bi.createGraphics();
//write old image to it
g2d.drawImage(old, 0, 0, null); //null was set to "this" when this was not static | Note ion case this breaks
g2d.drawImage(rule.image, theSettings.getX(), theSettings.getY(), null);
//cleanup
g2d.dispose();
//System.exit(1);
return bi;
}
每个规则只包含要替换的字符串和要替换它的图像。
static public class Rule{
//Text to look for
String keyword;
//image to replace it with
BufferedImage image;
public Rule (String key, BufferedImage img) {
keyword = key;
image = img;
}
}
我试图将其设计为许多人使用的工具,因此文本应该能够匹配用户添加的任何内容;虽然我目前的流程是使用诸如“{M}”之类的字符串,但这可能是一个标准。
另一个很大的障碍是文本可以包装在卡片上,这意味着字符串和图像需要在提供的范围内包装在一起。
编辑1:有一些想法,我将尝试这种方法。在获取字符串的“下一半”时,仍然可以看到边界可能出现的问题;但我相信这可行。
//If found a rule mid text:
//Split string in 2 at the rule match: strings 'start', and 'next'
//Calculate x and y for symbol
//x is the # of characters in ('start' % the word wrap width) +1 as the symbol is the next character, then multiply that by the character size of the font
//y is the integer of dividing the # of characters in 'start' by word wrap width multiplied by the font height
//Draw Start of String
//Draw symbol
//next x = sym.x + sym width //TODO next (x,y) math
我能够通过基于文本大小提前扭曲来解决问题。通过使用已知大小的图像,可以提前知道正确完成换行。
然后我循环了每一行。我看起来在文本中的字符串中进行了拆分,将被替换。我他们正常地画了字符串的第一部分。使用int pixelWidth = ni.getGraphics().getFontMetrics().stringWidth(splitString[j]);
计算字符串的宽度。然后在相同的Y处绘制图像,但将前一个字符串的宽度添加到X.然后将图像的当前宽度添加到X并继续循环;根据需要绘制图像和字符串。