我正在开发一个程序,可以用来在课堂前介绍主题。虽然MS Powerpoint和类似程序很好,但我感觉在某些方面缺少它们。我不是在尝试重新创建这些程序,而只是在尝试创建一个程序,可以在其中将文本键入String数组,添加图像并将它们全部放置在屏幕上。

目前,我的程序打开了一个全屏暗窗口,在左上角有一个小X,单击该窗口将关闭程序。

这是类文件:

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.MouseInfo;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Scanner;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;


public class Main {

    static JFrame frame;
    static MyPanel panel;
    static MouseListener listener;

    static final int letterWidth = 6;
    static final int letterHeight = 9;
    static final int letterRow = 10;
    static final int letterCol = 11;
    static final int letterNum = 110;

    static String[] textA = {"Hello World,",
                "Here is a test",
                "of what I can do.",
                "Isn't it neat?!"
                };
    static int textX = 100;
        static int textY = 100;

    public static void main(String[] args){
        System.out.println("Welcome to Presenter");
        System.out.println("Beginning loading");
        frame = new JFrame();
        panel = new MyPanel();

        listener = new MouseListener(){
            public void mouseClicked(MouseEvent arg0) {
                if(arg0.getButton() ==MouseEvent.BUTTON1){

                        //Close button
                    int x = (int) MouseInfo.getPointerInfo().getLocation().getX();
                        int y = (int) MouseInfo.getPointerInfo().getLocation().getY();
                    if(x>20 && x<48 && y>20 && y<56){
                        frame.dispose();
                            System.exit(0);
                    }


                }
             }

            public void mouseEntered(MouseEvent arg0) {}

            public void mouseExited(MouseEvent arg0) {}

            public void mousePressed(MouseEvent arg0) {}

            public void mouseReleased(MouseEvent arg0) {}
        };


        frame.setUndecorated(true);
        frame.setVisible(true);
        frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setFocusable(true);
        frame.add(panel);
            frame.addMouseListener(listener);
            System.out.println("");
        System.out.println("Loading Complete");
    }



public static class MyPanel extends JPanel{
    private static final long serialVersionUID = 1L;

    HashMap<String, BufferedImage> iLetters;
    static BufferedImage exitButton = new BufferedImage(28, 36, BufferedImage.TYPE_INT_ARGB);
    static BufferedImage letters = new BufferedImage(120, 198, BufferedImage.TYPE_INT_ARGB);
    Scanner lightbulb;
    static boolean point = false;
    int x;
    int y;
    BufferedImage textBox;
    int boxW, boxH;

    public MyPanel(){
        //pictures
        File a = new File("Exit.png");
        try { exitButton = ImageIO.read(a); } catch (IOException e) {e.printStackTrace();}
        a = new File("Letters.png");
        try { letters = ImageIO.read(a); } catch (IOException e) {e.printStackTrace();}
        //letter index
        try { lightbulb = new Scanner(new File("letters.txt")); } catch (FileNotFoundException e) {e.printStackTrace();}

        System.out.println("Files Read");



        //create letters
        System.out.println("Beginning tiling");
        iLetters = new HashMap<String, BufferedImage>();
        BufferedImage[] pics = new BufferedImage[letterNum];
        int count = 0;
        for(int x=0; x<letterCol; x++){
            for(int y=0; y<letterRow; y++){
                pics[count] = new BufferedImage(letterWidth, letterHeight, BufferedImage.TYPE_INT_ARGB);

                Graphics2D gr = pics[count++].createGraphics();
                gr.drawImage(letters, 0, 0, letterWidth, letterHeight, letterWidth * y, letterHeight * x, letterWidth * y + letterWidth, letterHeight * x + letterHeight, null);
                gr.dispose();
            }
            System.out.println("Row " + x + " tiled.");
        }
        System.out.println("Completed Tiling");

        System.out.println("Beginning indexing");
        int loc = 0;
        String key = "";
        while(lightbulb.hasNext()){
            loc = lightbulb.nextInt()-1;
            key = lightbulb.next();
            iLetters.put(key, pics[loc]);
        }
        System.out.println("Indexing Complete");

        System.out.println("Making Text Boxes");
        makeTextBox(textA);

        System.out.println("Text Boxes Made");

    }

    public void paintComponent(Graphics g){
        super.paintComponent(g);
        this.setBackground(new Color(0, 0, 10));
        g.drawImage(exitButton, 20, 20, null);
//          g.drawImage(iLetters.get("A"), 100, 100, null);
        drawTexts(g);
    }

    public void drawTexts(Graphics g){
        g.drawImage(textBox, textX, textY, textX+(boxW*2), textY+(boxH*2), null);
    }

    public int findLongest(String[] text){
        int longest = 0;
        for(int i=0; i<text.length; i++){
            if(text[i].length() > longest)
            longest = text[i].length();
        }
        return longest;
    }

    public void makeTextBox(String[] text){
        int longest = findLongest(text);
        boxW = (longest*6)+(longest-1);
        boxH = (text.length*9)+(text.length-1);
        textBox = new BufferedImage(boxW, boxH, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2d = textBox.createGraphics();
        char[] line;
        int drawX = 0;
        int drawY = 0;


        for(int i=0; i<text.length; i++){
            line = text[i].toCharArray();
            for(int j=0; j<line.length; j++){
                if(j<line.length-1){
                    g2d.drawImage(iLetters.get(line[j]), drawX, drawY, null);
                    drawX += letterWidth + 1;
                }
                else{
                    g2d.drawImage(iLetters.get(line[j]), drawX, drawY, null);
                }
            }
            if(i<text.length-1){
                drawY += letterHeight + 1;
            }
            else{
                drawY += letterHeight;
            }
        }
        g2d.dispose();
    }






}

}


运行它所需的其余文件在这里http://www.mediafire.com/?jq8vi4dm3t4b6

2个PNG和1个文本文件。

该程序运行正常,没有错误。唯一的问题是drawTexts()方法。调用该方法时,文本行不应出现在屏幕上的任何位置,而应以文件中字母大小的2倍出现在(100,100)处。

自从我做图形以来已经有一段时间了。我记得需要在某个地方调用repaint(),但是我不记得在这里是否需要它,如果需要的话,它在哪里。

最佳答案

好吧,这可能需要一些时间...



一共有10列和11行,这些都绕错了方向...

static final int letterRow = 10;
static final int letterCol = 11;


我认为您不了解gr.drawImage(letters, 0, 0, letterWidth, letterHeight, letterWidth * y, letterHeight * x, letterWidth * y + letterWidth, letterHeight * x + letterHeight, null);实际在做什么,您真正想要的是BufferedImage#getSubImage ...

BufferedImage subimage = letters.getSubimage(letterWidth * x, letterHeight * y, letterWidth, letterHeight);


您正在按行/列顺序读取Letters.png文件,但是就像按列/行顺序一样构建映射...

代替...

for (int x = 0; x < letterCol; x++) {
    for (int y = 0; y < letterRow; y++) {


您应该使用...

for (int y = 0; y < letterRow; y++) {
    for (int x = 0; x < letterCol; x++) {


lightbulb Scanner不会读取文件...我不经常使用Scanner,但是当我将其更改为lightbulb = new Scanner(new BufferedReader(new FileReader("letters.txt")));时,它可以工作...

在您的makeTextBox方法中,您尝试使用char而不是String检索图像,这意味着查找失败,因为两个键不兼容。您需要使用更多类似...

BufferedImage img = iLetters.get(Character.toString(line[j]));


另外,在每一行中,您都没有保留drawX变量,因此文本一直在运行……事实上,我只是重新编写了循环,使其看起来更像...

for (int i = 0; i < text.length; i++) {
    line = text[i].toCharArray();
    for (int j = 0; j < line.length; j++) {
        BufferedImage img = iLetters.get(Character.toString(line[j]));
        g2d.drawImage(img, drawX, drawY, null);
        if (j < line.length - 1) {
            drawX += letterWidth + 1;
        }
    }
    drawX = 0;
    if (i < text.length - 1) {
        drawY += letterHeight + 1;
    } else {
        drawY += letterHeight;
    }
}


是的,可以进一步简化,但只需很少的步骤...

可能还有很多其他事情,但这应该使您更接近……

09-07 09:43