我怎么画这个?

时间:2014-04-03 21:06:38

标签: java graphics

我已经尝试了很多方法来绘制图像作为背景但没有工作,所以我采用了制作一个覆盖屏幕并设置图像的JButton。 (我知道,我知道)我如何以正确的方式画出一个?

public class Menu extends JFrame {

private static final long serialVersionUID = 6001404440595750724L;
public static int width = 696;
public static int height = 550;

private JPanel window = new JPanel();
private JFrame frame = new JFrame();

private String Title = "Pixeled Universe";

private JButton play, options, quit, image, multi;
private Rectangle Rplay, Roption, Rquit, Rimage, Rmulti;

private int Bwidth = 200;
private int Bheight = 40;

public static String ico;

public static Socket socket;
public static DataOutputStream out;
public static DataInputStream in;

Sound sound = new Sound();

public boolean connect = true;

public static boolean multiP = false;

public Menu(Component component) {

    frame.setTitle(Title);
    frame.setSize(new Dimension(width, height));
    frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
    frame.add(component);
    frame.getContentPane().add(window);
    frame.setResizable(false);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
    ico = "Resources/image.ico";
    frame.setIconImage(new ImageIcon(ico).getImage());
    window.setLayout(null);

    drawButtons();
    button1();
    sound.play("Resources/FlashingWinds.wav");
    play();

    frame.repaint();
}

private void button1() {
    image = new JButton("");
    Rimage = new Rectangle(0, 0, width, height);
    image.setBounds(Rimage);
    image.setBorder(null);
    window.add(image);
    if (Tile.test == true) {
        image.setIcon(new ImageIcon("Resources/menubackground.png"));
    } else {
        image.setIcon(new ImageIcon(
                System.getProperty("user.home")
                        + "/AppData/Roaming/PixeledUniverse/Resources/menubackground.png"));
    }
}

private void drawButtons() {

    try {
        Character.name = readFile("Username.dat", StandardCharsets.UTF_8);
    } catch (Exception e) {
        e.printStackTrace();
    }
    play = new JButton("");
    Rplay = new Rectangle(10, 250, Bwidth, Bheight);
    play.setBounds(Rplay);
    play.setBorder(null);
    window.add(play);

    options = new JButton("");
    Roption = new Rectangle(10, 371, Bwidth, Bheight);
    options.setBounds(Roption);
    options.setBorder(null);
    window.add(options);

    quit = new JButton("");
    Rquit = new Rectangle(480, 250, Bwidth, Bheight);
    quit.setBounds(Rquit);
    quit.setBorder(null);
    window.add(quit);

    multi = new JButton("");
    Rmulti = new Rectangle(480, 371, Bwidth, Bheight);
    multi.setBounds(Rmulti);
    multi.setBorder(null);
    multi.setVisible(false);
    window.add(multi);

    if (Tile.test == true) {
        play.setIcon(new ImageIcon("Resources/play.png"));
        play.setPressedIcon(new ImageIcon("Resources/playS.png"));

        options.setIcon(new ImageIcon("Resources/options.png"));
        options.setPressedIcon(new ImageIcon("Resources/optionsS.png"));

        quit.setIcon(new ImageIcon("Resources/quit.png"));
        quit.setPressedIcon(new ImageIcon("Resources/quitS.png"));
    } else {
        play.setIcon(new ImageIcon(System.getProperty("user.home")
                + "/AppData/Roaming/PixeledUniverse/Resources/play.png"));
        play.setPressedIcon(new ImageIcon(System.getProperty("user.home")
                + "/AppData/Roaming/PixeledUniverse/Resources/playS.png"));

        options.setIcon(new ImageIcon(System.getProperty("user.home")
                + "/AppData/Roaming/PixeledUniverse/Resources/options.png"));
        options.setPressedIcon(new ImageIcon(System
                .getProperty("user.home")
                + "/AppData/Roaming/PixeledUniverse/Resources/optionsS.png"));

        quit.setIcon(new ImageIcon(System.getProperty("user.home")
                + "/AppData/Roaming/PixeledUniverse/Resources/quit.png"));
        quit.setPressedIcon(new ImageIcon(System.getProperty("user.home")
                + "/AppData/Roaming/PixeledUniverse/Resources/quitS.png"));
    }
}

public static String readFile(String path, Charset encoding)
        throws IOException {
    byte[] encoded = Files.readAllBytes(Paths.get(path));
    return encoding.decode(ByteBuffer.wrap(encoded)).toString();
}

public void play() {
    play.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            Component component = new Component();
            sound.stop();
            if (Tile.test == true) {
                sound.play("Resources/Select.wav");
            } else {
                sound.play(System.getProperty("user.home")
                        + "/AppData/Roaming/PixeledUniverse/Resources/Select.wav");
            }
            frame.dispose();
            new GenerateAndLoad(component);
        }
    });
    quit.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            SaveAndLoad.saveOptions();
            System.exit(0);
        }
    });
    options.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            Component component = new Component();
            sound.stop();
            if (Tile.test == true) {
                sound.play("Resources/Select.wav");
            } else {
                sound.play(System.getProperty("user.home")
                        + "/AppData/Roaming/PixeledUniverse/Resources/Select.wav");
            }
            new Options(component);
            frame.dispose();
        }
    });
    multi.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            Component component = new Component();
            frame.dispose();
            component.start();
            try {
                new Connect();
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        }
    });
}
}

1 个答案:

答案 0 :(得分:0)

  • 不要使用null布局,这只是问问题。
  • 您可以使用JLabel代替JButton,相应地设置标签布局管理器并将其他组件添加到其中
  • 仅在完成构建初始UI后调用框架上的setVisible