java绘制图像在另一个

时间:2014-02-02 21:11:08

标签: java image swing overlap overwrite

嘿,我正在尝试制作一个基本的发射器,并且为了设计,我得到了一个图像。我想让退出按钮显示在启动器图像上,以便您可以看到它。我不知道我是怎么做的所以我需要帮助。这是我的代码:

package launcher;

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
/**
 * 
 * @author Daniel <Skype: daniel.gusdal>
 *
 * Current Date: 2. feb. 2014 Current Time: 21:46:52
 * Project: 742 client. File Name: Launcher.java
 *
 */
public class Launcher extends JFrame {

    /**
     * Generated serialVersionUID
     */
    private static final long serialVersionUID = 1L;

    private JPanel contentPane;
    static Point mouseDownCompCoords;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            public void run() {
                try {
                    mouseDownCompCoords = null;
                    final Launcher frame = new Launcher();
                    frame.setResizable(true);
                    frame.setUndecorated(true);
                    frame.setBackground(new Color(0, 255, 0, 0));
                    frame.setVisible(true);
                    frame.addMouseListener(new MouseListener() {
                        public void mouseReleased(MouseEvent e) {
                            mouseDownCompCoords = null;
                        }

                        public void mousePressed(MouseEvent e) {
                            mouseDownCompCoords = e.getPoint();
                        }

                        public void mouseExited(MouseEvent e) {
                        }

                        public void mouseEntered(MouseEvent e) {
                        }

                        public void mouseClicked(MouseEvent e) {
                        }
                    });

                    frame.addMouseMotionListener(new MouseMotionListener() {
                        public void mouseMoved(MouseEvent e) {
                        }

                        public void mouseDragged(MouseEvent e) {
                            Point currCoords = e.getLocationOnScreen();
                            frame.setLocation(currCoords.x
                                    - mouseDownCompCoords.x, currCoords.y
                                    - mouseDownCompCoords.y);
                        }
                    });
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public Launcher() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 841, 593);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JLabel Design = new JLabel("New label");
        Design.setIcon(new ImageIcon(getClass().getResource("Launcher3.png")));
        Design.setBounds(-158, -22, 1047, 592);
        contentPane.add(Design);

        ImageIcon img = new ImageIcon(getClass().getResource(
                "Playnow.png"));
        final JButton Playnow = new JButton(img);
        Playnow.setBackground(null);
        Playnow.setOpaque(false);

        Playnow.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseEntered(MouseEvent e) {
                Playnow.setIcon(new ImageIcon(getClass().getResource(
                        "PlaynowHover.png")));
            }

            @Override
            public void mouseClicked(MouseEvent e) {
                Playnow.setIcon(new ImageIcon(getClass().getResource(
                        "PlaynowHover.png")));
                System.out.println(Playnow.getIcon());
            }

            @Override
            public void mouseExited(MouseEvent e) {
                Playnow.setIcon(new ImageIcon(getClass().getResource(
                        "Playnow.png")));
            }
        });

        Playnow.setBounds(258, 442, 301, 46);
        contentPane.add(Playnow);

        final JButton Exit = new JButton();
        Exit.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                System.out.println(Exit.getIcon());
                System.exit(0);
            }
        });
        Exit.setIcon(new ImageIcon(Launcher.class.getResource("Exit.png")));
        Exit.setOpaque(false);
        Exit.setBounds(766, 59, 21, 21);
        contentPane.add(Exit);
    }
}

1 个答案:

答案 0 :(得分:3)

  1. 不要设置边界。学习使用布局管理器。

  2. JPanel上绘制图片,而不是使用JLabel

  3. JButton放在JPanel上。给JPanel一个GridBadLayout

  4. 覆盖getPreferredSize()的{​​{1}}。

  5. 要在图像之间切换,您可以使用JPanel之类的标记。并重新标记boolean icon1, icon2, icon3; JPanel。像这样的东西

    mouseXxx
  6. public void mouseClicked(MouseEvent e) { icon1 = true; icon2 = false; icon3 = false; imagePanel.repaint(); } .... protected void paintComponent(Graphics g) { if (icon1) { g.drawImage(image1, .....); } if (icon2) { ... } if (icon3) { ... } } 您的框架。

  7. 使用Java命名约定。变量以小写字母开头。


  8. 这是一个例子

    enter image description here

    pack()