如何使用drawString和drawImage为游戏实现Java swing GUI启动屏幕?

bkkx9g8r  于 2023-03-11  发布在  Java
关注(0)|答案(2)|浏览(192)

我不确定如何修复程序中的错误,以及如何突出显示用户悬停的选项。我希望它突出显示每个位置的代码,即位置1将被突出显示(作为不同的颜色)开始游戏,等等,上/下会改变位置,我会用上,下,左,好的。这是我到目前为止所拥有的。目前它被窃听了,当用我的窗口编译时,它出来了:

哪个适用于主游戏,哪个适用于这个标题栏,我做错了什么,我该如何修复它?

标题板类

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.util.ArrayList;

//sound + file opening
import java.io.*;
import javax.sound.sampled.*;
public class TitleBoard extends JPanel implements ActionListener{

    private ArrayList<String> OptionList;
    private Image background;
    private ImageIcon bgImageIcon;
    private String cheatString;
    private int position;
    private Timer timer;

    public TitleBoard(){

    setFocusable(true);
    addKeyListener(new TAdapter());
    bgImageIcon = new ImageIcon("");
    background = bgImageIcon.getImage();
    String[] options = {"Start Game","Options","Quit"};
    OptionList = new ArrayList<String>();
    optionSetup(options);
    position = 1;
    timer = new Timer(8, this);
    timer.start();
    /*
      1 mod 3 =>1 highlight on start
      2 mod 3 =>2 highlight on options
      3 mod 3 =>0 highlight on quit
    */

    try{
        Font numFont = Font.createFont(Font.TRUETYPE_FONT,new File("TwistedStallions.ttf"));
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        ge.registerFont(numFont);
        setFont(numFont.deriveFont(24f)); //adjusthislater
    }catch(IOException|FontFormatException e){
        e.printStackTrace();
    }

    }
    private void optionSetup(String[] s){
    for(int i=0; i<s.length;i++) {
        OptionList.add(s[i]);
    }
    }


    public void paint(Graphics g){
    super.paint(g);
    Graphics g2d = (Graphics2D)g;

    g2d.drawImage(background,0,0,this);
    for (int i=0;i<OptionList.size();i++){
        g2d.drawString(OptionList.get(i),200,120+120*i);
    }/*
    g2d.drawString(OptionList.get(1),400,240);
    g2d.drawString(OptionList.get(2),400,360);
    //instructions on start screen maybe??
    //800x500
    //highlighting*/
    Toolkit.getDefaultToolkit().sync();
    g.dispose();
    }

    public void actionPerformed(ActionEvent e){
    repaint();
    }


    public class TAdapter extends KeyAdapter {
    public void keyPressed(KeyEvent e){
        if(e.getKeyCode() == KeyEvent.VK_UP||
           e.getKeyCode() == KeyEvent.VK_RIGHT){
        position++;
        }
        if(e.getKeyCode() == KeyEvent.VK_DOWN||
           e.getKeyCode() == KeyEvent.VK_LEFT){
        position--;
        }
    }
    }

}

窗口类

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

public class Window extends JFrame{
    public Window(){
    int width = 800, height = 600;
    //TO DO: make a panel in TITLE MODE
    ///////////////////////////////////
    //panel in GAME MODE.
    add(new TitleBoard());
    //set default close
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(width,height);
    //centers window
    setLocationRelativeTo(null);
    setTitle("Title");
    setResizable(false);
    setVisible(true);   
    }
    public static void main(String[] args){
    new Window();
    }
}
plicqrtu

plicqrtu1#

有许多方法可以实现这一点,例如,可以使用某种委托模型。
也就是说,您可以设计一个委托,提供一个简单的接口方法,paint方法将调用该方法,并且它知道如何执行其余操作,而不是尝试在单个方法(或多个方法)中管理每个元素。
例如,Swing将这种概念用于JListJTableJTree的单元格呈现器。
比如说...

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class MyAwesomeMenu {

    public static void main(String[] args) {
        new MyAwesomeMenu();
    }

    public MyAwesomeMenu() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private List<String> menuItems;
        private String selectMenuItem;
        private String focusedItem;

        private MenuItemPainter painter;
        private Map<String, Rectangle> menuBounds;

        public TestPane() {
            setBackground(Color.BLACK);
            painter = new SimpleMenuItemPainter();
            menuItems = new ArrayList<>(25);
            menuItems.add("Start Game");
            menuItems.add("Options");
            menuItems.add("Exit");
            selectMenuItem = menuItems.get(0);

            MouseAdapter ma = new MouseAdapter() {

                @Override
                public void mouseClicked(MouseEvent e) {
                    String newItem = null;
                    for (String text : menuItems) {
                        Rectangle bounds = menuBounds.get(text);
                        if (bounds.contains(e.getPoint())) {
                            newItem = text;
                            break;
                        }
                    }
                    if (newItem != null && !newItem.equals(selectMenuItem)) {
                        selectMenuItem = newItem;
                        repaint();
                    }
                }

                @Override
                public void mouseMoved(MouseEvent e) {
                    focusedItem = null;
                    for (String text : menuItems) {
                        Rectangle bounds = menuBounds.get(text);
                        if (bounds.contains(e.getPoint())) {
                            focusedItem = text;
                            repaint();
                            break;
                        }
                    }
                }

            };

            addMouseListener(ma);
            addMouseMotionListener(ma);
        
            InputMap im = getInputMap(WHEN_IN_FOCUSED_WINDOW);
            ActionMap am = getActionMap();
        
            im.put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0), "arrowDown");
            im.put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0), "arrowUp");
        
            am.put("arrowDown", new MenuAction(1));
            am.put("arrowUp", new MenuAction(-1));

        }

        @Override
        public void invalidate() {
            menuBounds = null;
            super.invalidate();
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            if (menuBounds == null) {
                menuBounds = new HashMap<>(menuItems.size());
                int width = 0;
                int height = 0;
                for (String text : menuItems) {
                    Dimension dim = painter.getPreferredSize(g2d, text);
                    width = Math.max(width, dim.width);
                    height = Math.max(height, dim.height);
                }

                int x = (getWidth() - (width + 10)) / 2;

                int totalHeight = (height + 10) * menuItems.size();
                totalHeight += 5 * (menuItems.size() - 1);

                int y = (getHeight() - totalHeight) / 2;

                for (String text : menuItems) {
                    menuBounds.put(text, new Rectangle(x, y, width + 10, height + 10));
                    y += height + 10 + 5;
                }

            }
            for (String text : menuItems) {
                Rectangle bounds = menuBounds.get(text);
                boolean isSelected = text.equals(selectMenuItem);
                boolean isFocused = text.equals(focusedItem);
                painter.paint(g2d, text, bounds, isSelected, isFocused);
            }
            g2d.dispose();
        }
    
        public class MenuAction extends AbstractAction {

            private final int delta;
        
            public MenuAction(int delta) {
                this.delta = delta;
            }

            @Override
            public void actionPerformed(ActionEvent e) {
                int index = menuItems.indexOf(selectMenuItem);
                if (index < 0) {
                    selectMenuItem = menuItems.get(0);
                }
                index += delta;
                if (index < 0) {
                    selectMenuItem = menuItems.get(menuItems.size() - 1);
                } else if (index >= menuItems.size()) {
                    selectMenuItem = menuItems.get(0);
                } else {
                    selectMenuItem = menuItems.get(index);
                }
                repaint();
            }
        
        }

    }

    public interface MenuItemPainter {

        public void paint(Graphics2D g2d, String text, Rectangle bounds, boolean isSelected, boolean isFocused);

        public Dimension getPreferredSize(Graphics2D g2d, String text);

    }

    public class SimpleMenuItemPainter implements MenuItemPainter {

        public Dimension getPreferredSize(Graphics2D g2d, String text) {
            return g2d.getFontMetrics().getStringBounds(text, g2d).getBounds().getSize();
        }

        @Override
        public void paint(Graphics2D g2d, String text, Rectangle bounds, boolean isSelected, boolean isFocused) {
            FontMetrics fm = g2d.getFontMetrics();
            if (isSelected) {
                paintBackground(g2d, bounds, Color.BLUE, Color.WHITE);
            } else if (isFocused) {
                paintBackground(g2d, bounds, Color.MAGENTA, Color.BLACK);
            } else {
                paintBackground(g2d, bounds, Color.DARK_GRAY, Color.LIGHT_GRAY);
            }
            int x = bounds.x + ((bounds.width - fm.stringWidth(text)) / 2);
            int y = bounds.y + ((bounds.height - fm.getHeight()) / 2) + fm.getAscent();
            g2d.setColor(isSelected ? Color.WHITE : Color.LIGHT_GRAY);
            g2d.drawString(text, x, y);
        }

        protected void paintBackground(Graphics2D g2d, Rectangle bounds, Color background, Color foreground) {
            g2d.setColor(background);
            g2d.fill(bounds);
            g2d.setColor(foreground);
            g2d.draw(bounds);
        }

    }

}

从这里,您可以添加ActionListener

l0oc07j2

l0oc07j22#

当一个GUI需要一个按钮时,使用JButtonJButton API允许为许多不同的情况添加图标。这个例子显示了标准图标、悬停图标和按下图标的不同图标。你的GUI显然会使用带有文本的图标来达到所需的效果。
这些图标是从Example images for code and mark-up Q&As直接(热链接)提取的。

标准

悬停在三角形上

按下三角形

编号

import java.awt.*;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.net.URL;

public class IconHoverFocusIndication {

    // the GUI as seen by the user (without frame)
    // swap the 1 and 0 for single column
    JPanel gui = new JPanel(new GridLayout(1,0,50,50));
    public static final int GREEN = 0, YELLOW = 1, RED = 2;
    String[][] urls = {
        {
            "http://i.stack.imgur.com/T5uTa.png",
            "http://i.stack.imgur.com/IHARa.png",
            "http://i.stack.imgur.com/wCF8S.png"
        },
        {
            "http://i.stack.imgur.com/gYxHm.png",
            "http://i.stack.imgur.com/8BGfi.png",
            "http://i.stack.imgur.com/5v2TX.png"
        },
        {
            "http://i.stack.imgur.com/1lgtq.png",
            "http://i.stack.imgur.com/6ZXhi.png",
            "http://i.stack.imgur.com/F0JHK.png"
        }
    };

    IconHoverFocusIndication() throws Exception {
        // adjust to requirement..
        gui.setBorder(new EmptyBorder(15, 30, 15, 30));
        gui.setBackground(Color.BLACK);
        Insets zeroMargin = new Insets(0,0,0,0);
        for (int ii = 0; ii < 3; ii++) {
            JButton b = new JButton();
            b.setBorderPainted(false);
            b.setMargin(zeroMargin);
            b.setContentAreaFilled(false);
            gui.add(b);

            URL url1 = new URL(urls[ii][GREEN]);
            BufferedImage bi1 = ImageIO.read(url1);
            b.setIcon(new ImageIcon(bi1));

            URL url2 = new URL(urls[ii][YELLOW]);
            BufferedImage bi2 = ImageIO.read(url2);
            b.setRolloverIcon(new ImageIcon(bi2));

            URL url3 = new URL(urls[ii][RED]);
            BufferedImage bi3 = ImageIO.read(url3);
            b.setPressedIcon(new ImageIcon(bi3));
        }
    }

    public JComponent getGUI() {
        return gui;
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {

            @Override
            public void run() {
                try {
                    IconHoverFocusIndication ihfi =
                            new IconHoverFocusIndication();
                    JFrame f = new JFrame("Button Icons");
                    f.add(ihfi.getGUI());
                    // Ensures JVM closes after frame(s) closed and
                    // all non-daemon threads are finished
                    f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                    // See https://stackoverflow.com/a/7143398/418556 for demo.
                    f.setLocationByPlatform(true);

                    // ensures the frame is the minimum size it needs to be
                    // in order display the components within it
                    f.pack();
                    // should be done last, to avoid flickering, moving,
                    // resizing artifacts.
                    f.setVisible(true);
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        };
        // Swing GUIs should be created and updated on the EDT
        // http://docs.oracle.com/javase/tutorial/uiswing/concurrency
        SwingUtilities.invokeLater(r);
    }
}

相关问题