2013-11-22 31 views
0

我最近一直在努力制定一個程序,可以追蹤一個人在籃球比賽中的成功。我建立了一個法庭圖,我的想法是,點擊「製作」或「錯過」按鈕後,您可以點擊球場上的一個位置,它會記錄您的失誤和失​​誤,同時保持兩側偏移。我以爲最簡單的方法就是用法院的一個巨大的按鈕(20x20),當你點擊一個按鈕時,它會將文本改爲「x」或「o」,具體取決於它是否爲射擊。我在爲各種按鈕創建監聽器方面沒有任何問題,但按鈕顯示在法院線的圖形頂部。使按鈕透明(我嘗試過)使它看起來更好一些,但我理想的解決方案是使網格中的按鈕不可見,但可點擊並能夠在單擊後更改其文本。使用JButtons(額外的幫助)

我還沒有找到這樣的方法或其他方式來實現這一點,所以如果任何人有任何經驗或投入創造的東西,可以使它以這種方式運作將有所幫助。我想我的最終問題是有沒有辦法讓按鈕不可見,但可點擊?如果沒有簡單的方法來做到這一點(我擔心),有沒有其他的想法可以使這個程序有效地發揮作用,可能不涉及按鈕?

非常感謝,任何想法都會幫助我解決很多問題。

我也想我可能會注意到,我有一個單獨的代碼驅動程序,即使它可能沒有什麼區別。

+0

你可以使用'MouseListener'手動執行此操作。 – MadProgrammer

回答

0

你可以做這樣的事情

這兩個按鈕,只需更改boolean make。你就會明白爲什麼後來

boolean make = false; 

makeButton.addActionListener(new ActionListener(){ 
    public void actionPerformed(ActionEvent){ 
     make = true; 
    } 
}); 

missButton.addActionListener(new ActionListener(){ 
    public void actionPerformed(ActionEvent){ 
     make = false; 
    } 
}); 

然後你有你的paintComponent

protected void paintComponent(Graphics g){ 
    super.paintComponent(g); 

    if (make){ 
     drawOval(whataver are your point requirements) 
    } else { 
     // draw an x at whatever points 
    } 
} 

你可以看到從上面,我利用了made變量

的現在,讓你可以做的位置像這樣的東西

Point p; 

courtPanel.addMouseListener(new MouseAdapter(){ 
    public void mouseClicked(MouseEvent e){ 
     p = e.getLocationOnScreen(); 
     repaint(); 
    } 
}); 

您繪製組件將根據這些點座標進行繪製。

因此,基本上,什麼都事我上面列出的作用是:

1)當按下makeButton,它改變了設置made所以當該板畫,它被塗上了一圈當missButtton被按下時X

2)我添加了一個mouseListener to the courtPanel becuase everytime the panel is clicked somewhere, that's the point on the floor where is it either painted an X or a circle`

如果你想多Xcircle畫你做這樣的事情

private boolean make = false; 
private HashMap<Boolean, Point> points = new HaspMap<>(); 

makeButton.addActionListener(new ActionListener(){ 
    public void actionPerformed(ActionEvent){ 
     make = true; 
    } 
}); 

missButton.addActionListener(new ActionListener(){ 
    public void actionPerformed(ActionEvent){ 
     make = false; 
    } 
}); 

courtPanel.addMouseListener(new MouseAdapter(){ 
    public void mouseClicked(MouseEvent e){ 
     if (make) { 
      points.put(true, e.getLocationOnScreen()); 
      repaint(); 
     } else { 
      points.put(false, e.getLocationOnScreen()); 
      repaint(); 
     } 

}); 

protected void paintComponent(Graphics g){ 
    super.paintComponent(g); 

    for (Map.Entry<Boolean, Point> entry : points.entrySet()) { 
     Boolean key = entry.getKey(); 
     Point point = entry.getValue(); 

     if (key) { 
      g.grawOval(point.getX(), point.getY(), 10, 20); 
     } esle { 
      g.drawLine(.., .., .., ..); //draws one half of `X` 
      g.drawLine(.., .., .., ..); //draws other half 
     } 
    } 
} 
+1

這是一個很好的方式來看待它,我從來沒有這樣想過,但它非常有意義!我沒有太多的MouseListener經驗,但我絕對可以實現你在這裏給我的東西,並從中脫穎而出。非常感謝! – robert825

0

與透明組件的問題是,基本上,幾乎不可能實際點擊它們;)

另一個解決方案重刑可能是在組件上使用MouseListener從點擊點繪製地圖和簡單的使譯文爲「虛擬網格」,然後你就可以渲染器在上面,例如...

enter image description here

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.EventQueue; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.Point; 
import java.awt.Rectangle; 
import java.awt.event.MouseAdapter; 
import java.awt.event.MouseEvent; 
import java.awt.image.BufferedImage; 
import java.io.File; 
import java.io.IOException; 
import java.util.ArrayList; 
import java.util.List; 
import javax.imageio.ImageIO; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 

public class BaseBallMap { 

    public static void main(String[] args) { 
     new BaseBallMap(); 
    } 

    public BaseBallMap() { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 
       } 

       JFrame frame = new JFrame("Testing"); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.setLayout(new BorderLayout()); 
       frame.add(new TestPane()); 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 
     }); 
    } 

    public static class TestPane extends JPanel { 

     public static final int GRID_COUNT = 20; 

     private BufferedImage map; 
     private List<Point> cells; 

     public TestPane() { 
      cells = new ArrayList<>(400); 
      try { 
       map = ImageIO.read(new File("Map.png")); 
      } catch (IOException ex) { 
       ex.printStackTrace(); 
      } 

      addMouseListener(new MouseAdapter() { 

       @Override 
       public void mouseClicked(MouseEvent e) { 
        Point p = e.getPoint(); 
        if (getMapBounds().contains(p)) { 
         p.x -= getXOffset(); 
         p.y -= getYOffset(); 
         int col = p.x/getColumnWidth(); 
         int row = p.y/getRowHeight(); 
         System.out.println(col + "x" + row); 
         Point cell = new Point(col, row); 
         if (cells.contains(cell)) { 
          cells.remove(cell); 
         } else { 
          cells.add(cell); 
         } 
         repaint(); 
        } 
       } 

      }); 

     } 

     @Override 
     public Dimension getPreferredSize() { 
      return map == null ? new Dimension(200, 200) : new Dimension(map.getWidth(), map.getHeight()); 
     } 

     @Override 
     protected void paintComponent(Graphics g) { 
      super.paintComponent(g); 
      if (map != null) { 
       Graphics2D g2d = (Graphics2D) g.create(); 

       int xOffset = getXOffset(); 
       int yOffset = getYOffset(); 

       int x = xOffset; 
       int y = yOffset; 
       g2d.drawImage(map, x, y, this); 

       int colWidth = getColumnWidth(); 
       int rowHeight = map.getHeight()/GRID_COUNT; 

       g2d.setColor(new Color(255, 0, 0, 128)); 
       for (Point p : cells) { 

        x = xOffset + (p.x * colWidth); 
        y = yOffset + (p.y * rowHeight); 

        g2d.fillRect(x, y, colWidth, rowHeight); 

       } 

       g2d.setColor(new Color(128, 128, 128, 64)); 
       for (int col = 0; col < GRID_COUNT; col++) { 
        x = xOffset + (col * colWidth); 
        g2d.drawLine(x, yOffset, x, yOffset + map.getHeight()); 
       } 
       for (int row = 0; row < GRID_COUNT; row++) { 
        y = yOffset + (row * rowHeight); 
        g2d.drawLine(xOffset, y, xOffset + map.getWidth(), y); 
       } 

       g2d.drawRect(xOffset, yOffset, map.getWidth(), map.getHeight()); 

       g2d.dispose(); 
      } 
     } 

     protected int getColumnWidth() { 
      return map == null ? 0 : map.getWidth()/GRID_COUNT; 
     } 

     protected int getRowHeight() { 
      return map == null ? 0 : map.getHeight()/GRID_COUNT; 
     } 

     protected int getXOffset() { 
      return map == null ? 0 : (getWidth() - map.getWidth())/2; 
     } 

     protected int getYOffset() { 
      return map == null ? 0 : (getHeight() - map.getHeight())/2; 
     } 

     protected Rectangle getMapBounds() { 

      return map == null ? new Rectangle(0, 0, 0, 0) : new Rectangle(getXOffset(), getYOffset(), map.getWidth(), map.getHeight()); 

     } 
    } 

} 
+0

MouseListener絕對會幫助我,謝謝! – robert825