gpt4 book ai didi

java在另一个上绘制图像

转载 作者:行者123 更新时间:2023-11-29 05:29:47 24 4
gpt4 key购买 nike

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

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. 不要设定界限。学习使用布局管理器。

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

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

  4. 重写 JPanelgetPreferredSize()

  5. 要在图像之间切换,您可以使用像 boolean icon1, icon2, icon3; 这样的标志。并重新绘制 JPanelmouseXxx。像这样

    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) { ... }
    }
  6. pack() 你的框架。

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


举个例子

enter image description here

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.*;
import java.util.logging.*;
import javax.imageio.ImageIO;
import javax.swing.*;


public class TestButtonOverImage {

public TestButtonOverImage() {
JFrame frame = new JFrame("Test Card");
frame.add(new ImagePanel());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
public void run() {
new TestButtonOverImage();
}
});
}

public class ImagePanel extends JPanel {

BufferedImage img;

public ImagePanel() {
setLayout(new GridBagLayout());
add(new JButton("StackOverflow Button"));

try {
img = ImageIO.read(new URL("http://d8u1nmttd4enu.cloudfront.net/designs/logo-stackoverflow-logo-design-99designs_447080~36d200d82d83d7b2e738cebd2a48de07180cef3a_largecrop"));
} catch (MalformedURLException ex) {
Logger.getLogger(TestButtonOverImage.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(TestButtonOverImage.class.getName()).log(Level.SEVERE, null, ex);
}

}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(img, 100, 100, 300, 300, this);
}

public Dimension getPreferredSize() {
return new Dimension(500, 500);
}
}
}

关于java在另一个上绘制图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21516299/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com