TA的每日心情 | 开心 2021-3-12 23:18 |
---|
签到天数: 2 天 [LV.1]初来乍到
|

- import java.awt.*;
- import javax.swing.JPanel;
- public class BackgroundPanel extends JPanel {
- private Image image;// 背景图片
-
- public BackgroundPanel() {
- setOpaque(false);
- setLayout(null);// 使用绝对定位布局控件
- }
-
- /**
- * 设置背景图片对象的方法
- *
- * @param image
- */
- public void setImage(Image image) {
- this.image = image;
- }
-
- /**
- * 画出背景
- */
- protected void paintComponent(Graphics g) {
- Graphics2D g2=(Graphics2D) g;
- g2.setComposite(AlphaComposite.SrcOver.derive(0.5f));
- if (image != null) {// 如果图片已经初始化
- // 画出图片
- g2.drawImage(image, 0, 0, this);
- }
- super.paintComponent(g2);
- }
- }
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- public class KeyMoveBackground extends JFrame {
-
- private JLabel label; // 背景图片的标签
-
- /**
- * 程序入口方法
- * @param args
- */
- public static void main(String args[]) {
- // 在事件队列中执行窗体创建与显示
- EventQueue.invokeLater(new Runnable() {
- public void run() {
- try {
- // 创建本类实例对象
- KeyMoveBackground frame = new KeyMoveBackground();
- // 显示窗体
- frame.setVisible(true);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- });
- }
-
- /**
- * 构造方法
- */
- public KeyMoveBackground() {
- super();
- setResizable(false); // 禁止调整窗体大小
- getContentPane().setLayout(null); // 设置空布局
- setTitle("模拟相机取景框"); // 设置窗体标题
- setBounds(100, 100, 500, 375);
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- label = new JLabel(); // 创建标签组件
- ImageIcon icon = new ImageIcon(getClass().getResource(
- "background.jpg"));
- ImageIcon glassImg = new ImageIcon(getClass()
- .getResource("glass.png"));
- label.setIcon(icon); // 设置标签使用背景图像
- // 使标签与图像同步大小
- label.setSize(icon.getIconWidth(), icon.getIconHeight());
- label.setLocation(-icon.getIconWidth() / 3, -icon
- .getIconHeight() / 3);// 设置标签默认位置
- // 为窗体添加按键事件监听适配器
- addKeyListener(new KeyAdapter() {
- public void keyPressed(final KeyEvent e) {
- do_label_keyPressed(e); // 调用事件处理方法
- }
- });
- getContentPane().add(label); // 添加背景标签到窗体
- // 创建取景框标签
- JLabel glassLabel = new JLabel(glassImg);
- JPanel glassPane = new JPanel(new BorderLayout());
- // 添加取景框标签到玻璃面板
- glassPane.add(glassLabel, BorderLayout.CENTER);
- glassPane.setOpaque(false); // 使面板透明
- setGlassPane(glassPane); // 设置窗体使用玻璃面板
- getGlassPane().setVisible(true); // 显示玻璃面板
- }
-
- /**
- * 背景标签组件的按键输入事件处理方法
- *
- * @param e
- */
- protected void do_label_keyPressed(final KeyEvent e) {
- int code = e.getKeyCode(); // 获取按键代码
- Point location = label.getLocation(); // 获取标签组件位置
- int step = 6; // 移动速度
- switch (code) {
- case KeyEvent.VK_RIGHT: // 如果按键代码是右方向键
- // 在不超出屏幕情况下
- if (location.x > (getWidth() - label.getWidth()))
- label.setLocation(location.x - step,
- location.y); // 向左移动标签
- break;
- case KeyEvent.VK_LEFT: // 如果是按键代码是左方向键
- if (location.x < 0) // 在不超出屏幕情况下
- label.setLocation(location.x + step,
- location.y); // 向右移动标签
- break;
- case KeyEvent.VK_DOWN: // 如果是按键代码是下方向键
- if (location.y > (getHeight() - label
- .getHeight())) // 在不超出屏幕情况下
- label.setLocation(location.x, location.y
- - step); // 向上移动标签
- break;
- case KeyEvent.VK_UP: // 如果是按键代码是上方向键
- if (location.y < 0) { // 在不超出屏幕情况下
- label.setLocation(location.x, location.y
- + step); // 向下移动标签
- }
- break;
- default:
- break;
- }
- }
- }
复制代码
源码下载:http://www.java3z.com/cwbwebhome/dir1/dir5/eBackground.zip |
|