public class buttonActionStart implements ActionListener { // overwritten ActionListener
public void actionPerformed (ActionEvent e){
JFrame window2 = new JFrame("Main Game"); //create empty frame
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
window2.setSize(screenSize.width,screenSize.height);
window2.add(new JPanel(){
public void paintComponent(Graphics graph){
super.paintComponent(graph);
Board board = new Board();
graph.drawString(board.sqaures[1].getName(),440,560);
graph.drawString(board.sqaures[2].getName(),335,560);
graph.drawString(board.sqaures[3].getName(),225,560);
graph.drawString(board.sqaures[4].getName(),145,560);
graph.drawString(board.sqaures[5].getName(),30,560);
graph.drawString(board.sqaures[6].getName(),25,460);
graph.drawString(board.sqaures[7].getName(),25,360);
graph.drawString(board.sqaures[8].getName(),35,260);
graph.drawString(board.sqaures[9].getName(),25,160);
graph.drawString(board.sqaures[10].getName(),25,60);
graph.drawString(board.sqaures[11].getName(),140,60);
graph.drawString(board.sqaures[12].getName(),240,60);
graph.drawString(board.sqaures[13].getName(),340,60);
graph.drawString(board.sqaures[14].getName(),440,60);
graph.drawString(board.sqaures[15].getName(),530,60);
graph.drawString(board.sqaures[16].getName(),550,160);
graph.drawString(board.sqaures[17].getName(),545,260);
graph.drawString(board.sqaures[18].getName(),540,360);
graph.drawString(board.sqaures[19].getName(),550,460);
for (int i = 0; i < 6; i++)
graph.drawRect(10 + (i*100),10,100,100);
for (int j = 0; j < 4; j++)
graph.drawRect(10,110 + (j*100),100,100);
for (int k = 0; k < 4; k++)
graph.drawRect(510,110 + (k*100),100,100);
for (int l = 0; l < 6; l++)
graph.drawRect(10 + (l*100),510,100,100);
InputStream resource = UI.class.getResourceAsStream("player1.png");
Image player1Image = null;
try{
player1Image = ImageIO.read(resource);
}catch (IOException e){
e.printStackTrace();
}
resource = UI.class.getResourceAsStream("player2.png");
Image player2Image = null;
try{
player2Image = ImageIO.read(resource);
}catch (IOException e){
e.printStackTrace();
}
resource = UI.class.getResourceAsStream("player3.png");
Image player3Image = null;
try{
player3Image = ImageIO.read(resource);
}catch (IOException e){
e.printStackTrace();
}
resource = UI.class.getResourceAsStream("player4.png");
Image player4Image = null;
try{
player4Image = ImageIO.read(resource);
}catch (IOException e){
e.printStackTrace();
}
graph.drawImage(player1Image, 511, 511, null);
graph.drawImage(player2Image, 585, 510, null);
graph.drawImage(player3Image, 511, 585, null);
graph.drawImage(player4Image, 585, 585, null);
}
});
window2.setVisible(true);
}
}
我正在開發一種棋盤遊戲,這四個圖像將作爲玩家圖標。我希望玩家手動點擊分配給他們的圖像,然後點擊屏幕上的其他位置,圖像將「更新」到該位置。如何將mouselistener添加到使用graph.drawimage()插入的圖像?
「廣場」和板」將是我自定義的類。
我有另外一個窗口,這將有一個‘開始’按鈕,觸發上述圖形要在新窗口中繪製。
我發現很多人建議使用的JLabel添加圖片和添加的JLabel到JFrame,但我發現,圖像將覆蓋我的圖形
對於[本尊]名稱:停止閱讀「paintComponent」中的圖像 - 您的體驗將是極端緩慢的動作。閱讀一次,然後根據需要多次塗刷它們,但每次組件需要痛苦時都停止閱讀! –
對不起,你能解釋一下嗎?我不完全明白你的意思。你的意思是閱讀paintComponent()中的圖像是不好的方法嗎?我想進一步問,在將JLabel直接添加到JFrame之前,是否需要將JLabel添加到JPanel? –
'你的意思是閱讀paintComponent()中的圖像是不好的方法嗎?' - 是的。繪畫方法僅用於繪畫,沒有別的。應該在您的類的構造函數中讀取圖像,以便在創建類時只讀取一次。所以不要創建Board類。 – camickr