2014-02-07 191 views
0

我正在構建一個棋盤遊戲,我有一個空圈的網格。在JButton上繪製圖像?

  • 我在photoshop中創建了自己的單個空圈的模板(.png),並重復添加(使用循環和佈局管理器)並使用它創建多個按鈕並將它們放置在面板上。

我在photoshop中創建了自定義的圓形標記(.png),然後它會在發生事件時將「填滿」或取出圓圈中的空白空間。我希望你明白我的意思。

我不完全確定使用paint()是唯一的方法來做到這一點。

任何人都可以提出一些關於如何實現這一點的提示嗎?我是GUI新手。

這是我網格的樣子:

enter image description here

enter image description here

而那些空的白色空間是令牌我創建將不得不佔用的空間,但我不當然,除了熟悉油漆外,我還會這樣做()

這是當用戶點擊一個按鈕時「填滿」空白的.png文件

enter image description here

+2

你知道你不必畫它們嗎?您可以將它們用作按鈕的「圖標」。 'button.setIcon(圖標)'。或者只是'button = new JButton(icon);' –

+0

''setIcon'只是將該標記添加到按鈕中,就像「填充」空白空間(我編輯它以適合空白空間),而不是刪除我分配給它之前的空圈圖像?因爲這就是我需要的,因爲令牌和方塊(空圓圈模板)同時可見。 – user3026693

+2

*「發生事件時,請在圈子中留出空白。」*具體是什麼事件?你能告訴我們各種圖標嗎?請注意,我懷疑最好的方法是使用一個JToggleButton,第一個圖標作爲默認圖標,並將組合圖標作爲「已按下的圖標」。 –

回答

2

看看這個想法。它結合了2幅圖像的「最後的邏輯」的樣子:

enter image description here

我不能打擾等待透明的模板,所以我做了我自己。 ;)

import java.awt.*; 
import java.awt.event.*; 
import java.awt.geom.*; 
import java.awt.image.BufferedImage; 
import javax.swing.*; 
import java.net.URL; 
import javax.imageio.ImageIO; 

class GameGrid { 

    public static BufferedImage getImage(BufferedImage image, boolean fill) { 
     int pad = 4; 
     BufferedImage temp = new BufferedImage(
       image.getWidth()+2*pad, 
       image.getHeight()+2*pad, 
       BufferedImage.TYPE_INT_ARGB); 
     Graphics2D g = temp.createGraphics(); 
     g.setColor(Color.MAGENTA.darker()); 
     Ellipse2D.Double ellipse = new Ellipse2D.Double(
       pad, pad, image.getWidth(), image.getHeight()); 
     Rectangle2D.Double outline = new Rectangle2D.Double(
       0, 0, image.getWidth()+(2*pad), image.getHeight()+(2*pad)); 
     Area a = new Area(outline); 
     a.subtract(new Area(ellipse)); 
     if (fill) { 
      g.drawImage(image,pad,pad,null); 
     } 
     g.setClip(a); 
     g.fillRect(0, 0, image.getWidth()+(2*pad), image.getHeight()+(2*pad)); 
     g.dispose(); 

     return temp; 
    } 

    public static void main(String[] args) throws Exception { 
     URL url = new URL("http://i.stack.imgur.com/t5MFE.png"); 
     BufferedImage image = ImageIO.read(url); 

     final BufferedImage img1 = getImage(image, true); 
     final BufferedImage img2 = getImage(image, false); 
     Runnable r = new Runnable() { 
      @Override 
      public void run() { 
       JPanel gui = new JPanel(new GridLayout(0,3)); 

       ActionListener al = new ActionListener() { 

        @Override 
        public void actionPerformed(ActionEvent e) { 
         JButton b = (JButton)e.getSource(); 
         b.setIcon(new ImageIcon(img2)); 
        } 
       }; 

       for (int ii=0; ii<9; ii++) { 
        JButton b = new JButton(new ImageIcon(img1)); 
        b.setBackground(Color.RED); 
        //b.setContentAreaFilled(false); 
        b.setBorder(null); 
        b.addActionListener(al); 
        gui.add(b); 
       } 

       JOptionPane.showMessageDialog(null, gui); 
      } 
     }; 
     // Swing GUIs should be created and updated on the EDT 
     // http://docs.oracle.com/javase/tutorial/uiswing/concurrency 
     SwingUtilities.invokeLater(r); 
    } 
} 
+0

哇,謝謝你。我已經上傳了透明模板(它在評論中,真的很抱歉等待),如果你不介意的話,你能申請嗎?只有你不介意。 :) – user3026693

+0

我很抱歉這裏是調整大小的文件: TEMPLATE:http://imageshack.com/a/img191/8492/hloy.png 令牌:http://imageshack.com/a/img842/ 6576/bz7j.png – user3026693

+0

Aah ..我只是想說:A)我不明白這兩幅圖像應該如何一起工作,是不同的尺寸,B)因爲我用更好的填充和BG的顏色,我真的不能在沒有看到'你的*'部分的'最佳嘗試'的情況下做進一步的改動。仔細查看代碼並查看是否可以編輯它以合併模板和填充圖像。如果您遇到困難,請發佈[MCTaRE](http://stackoverflow.com/help/mcve)(最小完整測試和可讀示例)。 –