java - 使用java将图像加载到数组中

标签 java

我正在开发一款纸牌游戏,需要将纸牌图像加载到屏幕上。我正在使用 ImageIcon 和 PaintIcon 来打印图像。我希望图片可以点击,但我不知道该怎么做。

我使用 PaintIcon 的原因是因为我希望通过单击按钮移动图像。(堆叠卡片以节省空间,展开以查看全部)

我不知道要搜索什么或要开始。

如果有人可以向我展示示例代码或正确的方向,那将有所帮助。

这是我使用的代码:

public class CustomGraphicsDemo2 extends JFrame {

  // Define constances
  private static final int CANVAS_WIDTH = 640;
  private static final int CANVAS_HEIGHT = 480;

  //Array of image cards


  //Handle for the custom drawing panel
  private DrawCanvas canvas;

  private ImageIcon card1, card2, card3,card4;

  //Attributes of Drawing object
  private int x = 100;      // x and y position
  private int y = 100;
  private int size = 50;    
  private int xSpeed = 3;   // moving speed in x and y directions
  private int ySpeed = 5;

  //Constructor to create the UI components
 public CustomGraphicsDemo2() {
  canvas = new DrawCanvas();
  canvas.setPreferredSize(new Dimension(CANVAS_WIDTH, CANVAS_HEIGHT));
  this.setContentPane(canvas);
  this.setDefaultCloseOperation(EXIT_ON_CLOSE);
  this.pack();
  this.setTitle("Custom Graphics Demo");
  this.setVisible(true);

  // Refresh the display at regular interval.
  // Run the display refresh code in its own thread.
  Thread updateThread = new Thread() {
     public void run() {
        while (true) {
           update();   // update the (x, y) position
           repaint();  // Refresh the JFrame, callback paintComponent()
           try {
              // Delay and give other thread a chance to run
              Thread.sleep(50);  // milliseconds
           } catch (InterruptedException ex) {}
        }
     }
  };
   updateThread.start();   // callback run()
 }

 // Update the (x, y) position of the graphical object
   public void update() {
   x += xSpeed;
   y += ySpeed;
   if (x > CANVAS_WIDTH - size || x < 0) {
     xSpeed = -xSpeed;
   }
   if (y > CANVAS_HEIGHT - size || y < 0) {
      ySpeed = -ySpeed;
   }
}


   //Custom drawing canvas (designed as inner class).
   class DrawCanvas extends JPanel {
   // Custom drawing codes
   @Override
   public void paintComponent(Graphics g) {

      super.paintComponent(g);
      setBackground(Color.BLACK);
      g.setColor(Color.BLUE);
      g.fillOval(x, y, size, size);  // draw a circle


      //cards being drawn
      card1 = new ImageIcon("Uno Cards/Blue/ EIGHT.png");
      card1.paintIcon(this, g, 50, 100);

      card2 = new ImageIcon("Uno Cards/Blue/FIVE.png");
      card2.paintIcon(this, g, 100, 100);

      card3 = new ImageIcon("Uno Cards/Blue/NINE.png");
      card3.paintIcon(this, g, 150, 100);

      card4 = new ImageIcon("Uno Cards/Blue/EIGHT.png");
      card4.paintIcon(this, g, x, y);

      //Graphics2D g2 = (Graphics2D) g; //we use this for drawing later. 
      //g2.fill(new drawImage());
      //g2.fill(new Rectangle2D.Double(10,y,size,size));

    }
 }

  // main program
  public static void main(String[] args) {

   SwingUtilities.invokeLater(new Runnable() {
      public void run() {
         new CustomGraphicsDemo2();
       }
    });
  }
 }

最佳答案

您可以将 ImageIcon 添加到 JLabel,然后将该标签添加到面板。

JLabel label = new JLabel(new ImageIcon(path));
panel.add(label);

更好的方法是为每张卡片创建单独的组件。让 CardUI 扩展 JComponent

class CardUI extends JComponent {
    //... class members ...

    public CardUI(BufferedImage cardPhoto){
        this.cardPhoto= cardPhoto;
    }

    void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(cardPhoto, x, y, this);
    }
}

编辑: 忘了说,现在你有了一个 CardUI 类,你可以给它添加监听器来查看鼠标是否在这个 CardUI 上。

关于java - 使用java将图像加载到数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8422347/

相关文章:

java - 使用 Bean Validation 验证正数

java - Actor 模型实现中的多个 Apache HTTP 客户端

java - 错误: failed to load JDBC driver - org. hsqldb.jdbcDriver

java - Guava循环是如何实现的

java - @Repository @Component 注释的 NoSuchBeanDefinitionException

java - 如何在 PayPal REST API 中获取用户信息

java - 数组排序后项目消失

java - Inno Setup 常量相当于 System.getProperty ("user.home")

java - 单击 ListView 中的按钮不起作用

java - Netty 使用 TCP 给我一个错误的端口