我從來沒有寫過任何東西比裸機更GUI程序,所以我採取了個人項目寫一個象棋應用。重寫的paintComponent()在JButton的產生意想不到的結果
我的這個項目的目標之一是讓電路板重新調整到適合窗口,我設法做到這一點沒有太多的麻煩。然而,在這個過程中,我碰到了一個問題,那就是我的作品(在JButtons上表現爲圖標)並未與其他作品重新進行比例調整。
我決定用Image類代替它們,並且創建了一個名爲ScalingJButton的自定義類,它覆蓋了paintComponent。這實際上工作得很好......對於the last piece to be drawn。其餘的部分沒有繪製,結果程序被破壞。這裏是我ScalingJButton類:
public class ScalingJButton extends JButton{
private Image image;
ScalingJButton(){
this.image = null;
}
ScalingJButton (Image image){
this.image = image;
}
public void setImage(Image image){
this.image = image;
}
public Image getImage(){
return image;
}
@Override
public void paintComponent(Graphics g){
//super.paintComponent(g);
int x = getX();
int y = getY();
int width = getWidth();
int height = getHeight();
if (image != null) {
g.drawImage(image, x, y, width, height, this);
}
}}
此外,這裏是負責實例化ScalingJButtons代碼(VisualBoard是擴展的JPanel類,這是它的構造函數)。
public VisualBoard(){
white = Color.WHITE;
black = Color.GRAY;
loadPieceImages();
setLayout(new GridLayout(8, 8));
for (int i = 0; i < 8; i++){
for (int j = 0; j < 8; j++){
squares[i][j] = new ScalingJButton();
if ((i + j) % 2 != 0) {
squares[i][j].setBackground(white);
}
else{
squares[i][j].setBackground(black);
}
add(squares[i][j]);
}
}
initializeStandardBoard();
}
最後,由於佈局可能是相關的,這裏要說的是,使板自動縮放代碼:
public class Chess {
public static void main (String[] args){
final VisualBoard board = new VisualBoard();
final JPanel container = new JPanel(new GridBagLayout());
container.add(board);
container.addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
drawResizedBoard(board, container);
}
});
JFrame frame = new JFrame("Chess");
frame.setSize(1000,1000);
frame.add(container);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
private static void drawResizedBoard(JPanel innerPanel, JPanel container) {
int w = (int)Math.round(container.getWidth()*0.9);
int h = (int)Math.round(container.getHeight()*0.9);
int size = Math.min(w, h);
innerPanel.setPreferredSize(new Dimension(size, size));
container.revalidate();
}}
我曾經做過大量的調試,但大部分有沒有出路。有一點需要注意的是,當調用drawImage時,ScalingJButton中的「image」變量保持正確的圖像,所以這不是問題。另一個有趣的一點是,使用一塊在它的廣場是方形的楦要繪製,並根據什麼順序加方塊董事會,不同的作品將被繪製(所以沒有問題加載件)。
奇怪的是,如果我做掉在超級的paintComponent,滾動我的鼠標在拉片導致其他廣場,以填補那塊當我搖我的鼠標超過他們。
我完全迷路了,不知道該怎麼辦,所以任何幫助將不勝感激!
非常感謝你的解釋!我已經自己辭去了使用StretchIcon(不可否認,這不是一個壞主意),但在閱讀完畢後,我回到了我的自定義類,發現它運行得很好。我只是假設g.drawImage()將從ScalingJButton的位置相對於右上角繪製,但實際上它是相對於ScalingJButton繪製的。所有那些對這種愚蠢的誤解感到悲傷! – TheRussianPatzer