0

私はNetBeansプラットフォームに基づくカードゲームに取り組んでおり、動的画像に頭を悩ませています。なぜ動的なのですか?実行時にページの変更(名前、テキスト、コストなど)に合わせてカードを調整したいと思います。

私が最初にハックしたのは、カードの値に基づいてテキスト/画像を読み込んだ場所にラベルが事前に配置されたコンポーネント(JPanel)を作成することでした。それはうまくいくようですが、後の版でいくつかのページの外観が異なることを考えると、面倒になりました(つまり、すべてが同じ場所にあるわけではありません)。

だから私はある種のテンプレートに基づいてこれを行う方法についてのアイデアを得ようとしています。

何か案が?

フォローアップの質問があります:JList of card?

4

1 に答える 1

0

最後に、これに戻る時間があり、 Java2Dチュートリアルを使用して方法を見つけることができました。

写真は、アプリケーションで使用するものに近いものではありませんが、概念実証として機能します。

パッケージjavaapplication3;

importjava.awt。*; import java.awt.font.FontRenderContext; java.awt.font.LineBreakMeasurerをインポートします。import java.awt.font.TextAttribute; import java.awt.font.TextLayout; import java.awt.image.BufferedImage; java.io.Fileをインポートします。インポートjava.io.IOException; インポートjava.net.MalformedURLException; java.net.URLをインポートします。インポートjava.text.AttributedCharacterIterator; インポートjava.text.AttributedString; import java.util.ArrayList; java.util.HashMapをインポートします。import java.util.logging.Level; import java.util.logging.Logger; インポートjavax.imageio.ImageIO;

/ ** * *@authorJavierA.OrtizBultrón*/public class DefaultImageManager {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    try {
        // TODO code application logic here
        DefaultImageManager manager = new DefaultImageManager();
        URL url = DefaultImageManager.class.getResource("weather-rain.png");
        manager.getLayers().add(ImageIO.read(url));
        url = DefaultImageManager.class.getResource("weather-sun.png");
        manager.getLayers().add(ImageIO.read(url));
        manager.addText(new Font("Arial", Font.PLAIN, 10), "Many people believe that Vincent van Gogh painted his best works "
                + "during the two-year period he spent in Provence. Here is where he "
                + "painted The Starry Night--which some consider to be his greatest "
                + "work of all. However, as his artistic brilliance reached new "
                + "heights in Provence, his physical and mental health plummeted. ",
                200, 150, new Point(0, 0));
        manager.generate();
    } catch (MalformedURLException ex) {
        Logger.getLogger(DefaultImageManager.class.getName()).log(Level.SEVERE,

null、例); } catch(IOException ex){Logger.getLogger(DefaultImageManager.class.getName())。log(Level.SEVERE、null、ex); }} /***最終的な画像の作成に使用されるレイヤー*/private ArrayList layer = new ArrayList(); private ArrayList textLayers = new ArrayList();

/**
 * @return the layers
 */
public ArrayList<BufferedImage> getLayers() {
    return layers;
}

private Dimension getMaxSize() {
    int width = 0, height = 0;
    for (BufferedImage img : getLayers()) {
        if (img.getWidth() > width) {
            width = img.getWidth();
        }
        if (img.getHeight() > height) {
            height = img.getHeight();
        }
    }
    return new Dimension(width, height);
}

public void addText(Font font, String text, int height, int width, Point location) {
    BufferedImage textImage = new BufferedImage(width, height,
            BufferedImage.TYPE_INT_ARGB);
    HashMap<TextAttribute, Object> map =
            new HashMap<TextAttribute, Object>();
    map.put(TextAttribute.FAMILY, font.getFamily());
    map.put(TextAttribute.SIZE, font.getSize());
    map.put(TextAttribute.FOREGROUND, Color.BLACK);
    AttributedString aString = new AttributedString(text, map);
    AttributedCharacterIterator paragraph = aString.getIterator();
    // index of the first character in the paragraph.
    int paragraphStart = paragraph.getBeginIndex();
    // index of the first character after the end of the paragraph.
    int paragraphEnd = paragraph.getEndIndex();
    Graphics2D graphics = textImage.createGraphics();
    FontRenderContext frc = graphics.getFontRenderContext();
    // The LineBreakMeasurer used to line-break the paragraph.
    LineBreakMeasurer lineMeasurer = new LineBreakMeasurer(paragraph, frc);
    // Set break width to width of Component.
    float breakWidth = width;
    float drawPosY = 0;
    // Set position to the index of the first character in the paragraph.
    lineMeasurer.setPosition(paragraphStart);

    // Get lines until the entire paragraph has been displayed.
    while (lineMeasurer.getPosition() < paragraphEnd) {
        // Retrieve next layout. A cleverer program would also cache
        // these layouts until the component is re-sized.
        TextLayout layout = lineMeasurer.nextLayout(breakWidth);

        // Compute pen x position. If the paragraph is right-to-left we
        // will align the TextLayouts to the right edge of the panel.
        // Note: this won't occur for the English text in this sample.
        // Note: drawPosX is always where the LEFT of the text is placed.
        float drawPosX = layout.isLeftToRight()
                ? 0 : breakWidth - layout.getAdvance();

        // Move y-coordinate by the ascent of the layout.
        drawPosY += layout.getAscent();

        // Draw the TextLayout at (drawPosX, drawPosY).
        layout.draw(graphics, drawPosX, drawPosY);

        // Move y-coordinate in preparation for next layout.
        drawPosY += layout.getDescent() + layout.getLeading();
    }
    getTextLayers().add(textImage);
}

public void generate() throws IOException {
    Dimension size = getMaxSize();
    BufferedImage finalImage = new BufferedImage(size.width, size.height,
            BufferedImage.TYPE_INT_ARGB);
    for (BufferedImage img : getLayers()) {
        finalImage.createGraphics().drawImage(img,
                0, 0, size.width, size.height,
                0, 0, img.getWidth(null),
                img.getHeight(null),
                null);
    }
    for(BufferedImage text: getTextLayers()){
        finalImage.createGraphics().drawImage(text,
                0, 0, text.getWidth(), text.getHeight(),
                0, 0, text.getWidth(null),
                text.getHeight(null),
                null);
    }
    File outputfile = new File("saved.png");
    ImageIO.write(finalImage, "png", outputfile);
}

/**
 * @return the textLayers
 */
public ArrayList<BufferedImage> getTextLayers() {
    return textLayers;
}

/**
 * @param textLayers the textLayers to set
 */
public void setTextLayers(ArrayList<BufferedImage> textLayers) {
    this.textLayers = textLayers;
} }

それでも、テキストの配置について特別に改良する必要がありますが、機能します。このすべての情報を格納するためのxml形式を実装できるので、簡単に構成できると思います。以下の例では、雨の上に太陽が描かれ、その上にテキストが表示されています。私のアプリケーションでは、各レイヤーが必要なページを一緒に構築します。

これが私が使用した画像です: ここに画像の説明を入力してください ここに画像の説明を入力してください

そして最終結果:

ここに画像の説明を入力してください

于 2011-11-22T23:08:25.937 に答える