2011-12-12 46 views
0

我正在创建一个使用摆动的战舰型游戏。我在一个面板中绘制图像并显示正常,但是当我在其他面板中绘制更多图像时,它会删除第一个面板中的大部分(如果不是全部)图像并使第二个面板中的图像保持完好。我将如何去保持图像在面板中绘制后消失。图形消失原因不明

我一直在大约一个星期左右,并寻找答案网上已经想出了什么...

public class GameBoard extends JPanel{ 
Graphics g0; 
public void paintComponent(Graphics g1) { 
    g0 = this.getGraphics(); 
    // fill with the color you want 
    int wide = 275; 
    int tall = 275; 

    // go into Graphics2D for all the fine art, more options 
    // optional, here I just get variable Stroke sizes 
    Graphics2D g2 = (Graphics2D) g1; 
    g2.setColor(Color.black); 
    g2.setStroke(new BasicStroke(1)); 
    Graphics2D g3 = (Graphics2D) g0; 
    g3.setColor(Color.black); 
    g3.setStroke(new BasicStroke(1)); 
    // the verticals 
    for (int i = 0; i < 12*25; i+=25) { 
     g2.drawLine(i, 0, i, tall); 
     g3.drawLine(i, 0, i, tall); 
    } 
    // the horizontal 
    for (int i = 0; i < 12*25; i+=25) { 
     g2.drawLine(0, i, wide, i); 
     g3.drawLine(0, i, wide, i); 
    } 
    g0 = this.getGraphics(); 
} 
public void paintComponent(Image i, int x, int y) { 
    g0.drawImage(i, x, y, null); 
} 

} 

以上是我所创建的第一个功能面板绘制网格和二是在插入cooridnates(X,Y)

public class Game { 
static JFrame main = new JFrame(); 
static GameBoard panel = new GameBoard(), panel1 = new GameBoard(); 
static Container c = main.getContentPane(); 
static JLabel title = new JLabel("BattleShip!"); 
static int count = 0, x, y, j; 
static String b; 
static BufferedImage pB = null, aC = null, bS = null, deS = null, suB = null; 
static BattleShips[] player = new BattleShips[5]; 
static BattleShips[] computer = new BattleShips[5]; 
static Graphics g; 

public static void setup(){ 
    player[0] = new BattleShips(); 
    player[1] = new BattleShips(); 
    player[2] = new BattleShips(); 
    player[3] = new BattleShips(); 
    player[4] = new BattleShips(); 
    computer[0] = new BattleShips(); 
    computer[1] = new BattleShips(); 
    computer[2] = new BattleShips(); 
    computer[3] = new BattleShips(); 
    computer[4] = new BattleShips(); 
    c.add(title); 
    c.add(panel); 
    c.add(panel1); 
    panel.setAlignmentY(Component.CENTER_ALIGNMENT); 
    panel.setAlignmentX(Component.CENTER_ALIGNMENT); 
    panel.setSize(276, 276); 
    panel1.setAlignmentY(Component.CENTER_ALIGNMENT); 
    panel1.setAlignmentX(Component.CENTER_ALIGNMENT); 
    panel1.setSize(276, 276); 
    title.setAlignmentY(Component.CENTER_ALIGNMENT); 
    title.setAlignmentX(Component.CENTER_ALIGNMENT); 

    main.setVisible(true); 
    main.setSize(new Dimension(291,630)); 
    main.setResizable(true); 
    main.setLayout(new BoxLayout(c, BoxLayout.Y_AXIS)); 
    main.setLocation(400, 15); 
    main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    Graphics g = panel.getGraphics(), g1 = panel1.getGraphics(); 
    panel.paintComponent(g); 
    panel1.paintComponent(g1); 
    panel.setBorder(new EmptyBorder(0,0,25,0)); 

    panel1.addMouseListener(new MouseListener(){ 

     @Override 
     public void mouseClicked(MouseEvent e) { 

      String d = "H"; 


      switch(count){ 
      case 0: 
       player[0].Name = "patrolBoat"; 
       player[0].x[0] = e.getX()-(e.getX()%25); 
       player[0].y[0] = (e.getY()-(e.getY()%25)); 
       Object[] possibilities = {"H", "V"}; 
       d = (String)JOptionPane.showInputDialog(
            main, 
            "Place " + player[0].Name + " vertically or horizontally?", 
            "Customized Dialog", 
            JOptionPane.PLAIN_MESSAGE, 
            null, 
            possibilities, 
            "ham"); 
       try {pB = ImageIO.read(new File("src/resources/"+player[0].Name+d+".png"));} catch (IOException e1) {} 
       panel1.paintComponent(pB,player[0].x[0],player[0].y[0]); 
       count++; 
       break; 
      case 1: 
       player[1].Name = "battleship"; 
       player[1].x[0] = e.getX()-(e.getX()%25); 
       player[1].y[0] = (e.getY()-(e.getY()%25)); 
       Object[] possibilities1 = {"H", "V"}; 
       d = (String)JOptionPane.showInputDialog(
            main, 
            "Place " + player[1].Name + " vertically or horizontally?", 
            "Customized Dialog", 
            JOptionPane.PLAIN_MESSAGE, 
            null, 
            possibilities1, 
            "ham"); 

       try {bS = ImageIO.read(new File("src/resources/"+player[1].Name+d+".png"));} catch (IOException e1) {} 
       panel1.paintComponent(bS,player[1].x[0],player[1].y[0]); 
       count++; 
       break; 
      case 2: 
       player[2].Name = "aircraftCarrier"; 
       player[2].x[0] = e.getX()-(e.getX()%25); 
       player[2].y[0] = (e.getY()-(e.getY()%25)); 
       Object[] possibilities11 = {"H", "V"}; 
       d = (String)JOptionPane.showInputDialog(
            main, 
            "Place " + player[2].Name + " vertically or horizontally?", 
            "Customized Dialog", 
            JOptionPane.PLAIN_MESSAGE, 
            null, 
            possibilities11, 
            "ham"); 
       try {aC = ImageIO.read(new File("src/resources/"+player[2].Name+d+".png"));} catch (IOException e3) {} 
       panel1.paintComponent(aC,player[2].x[0],player[2].y[0]); 
       count++; 
       break; 
      case 3: 
       player[3].Name = "destroyer"; 
       player[3].x[0] = e.getX()-(e.getX()%25); 
       player[3].y[0] = (e.getY()-(e.getY()%25)); 
       Object[] possibilities111 = {"H", "V"}; 
       d = (String)JOptionPane.showInputDialog(
            main, 
            "Place " + player[3].Name + " vertically or horizontally?", 
            "Customized Dialog", 
            JOptionPane.PLAIN_MESSAGE, 
            null, 
            possibilities111, 
            "ham"); 

       try {deS = ImageIO.read(new File("src/resources/"+player[3].Name+d+".png"));} catch (IOException e2) {} 
       panel1.paintComponent(deS,player[3].x[0],player[3].y[0]); 
       count++; 
       break; 
      case 4: 
       player[4].Name = "submarine"; 
       player[4].x[0] = e.getX()-(e.getX()%25); 
       player[4].y[0] = (e.getY()-(e.getY()%25)); 
       Object[] possibilities1111 = {"H", "V"}; 
       d = (String)JOptionPane.showInputDialog(main, "Place " + player[4].Name + " vertically or horizontally?", "Customized Dialog",JOptionPane.PLAIN_MESSAGE,null, possibilities1111, "ham"); 
       try {suB = ImageIO.read(new File("src/resources/"+player[4].Name+d+".png"));} catch (IOException e1) {} 
       panel1.paintComponent(suB,player[4].x[0],player[4].y[0]); 
       count = 5; 
       break; 
      case 5: 
       try {setupComp(); 
       count++;} catch (IOException e1) {} 
      } 


     } 

     @Override 
     public void mousePressed(MouseEvent e) {} 

     @Override 
     public void mouseReleased(MouseEvent e) {} 

     @Override 
     public void mouseEntered(MouseEvent e) {} 

     @Override 
     public void mouseExited(MouseEvent e) {} 
    }); 

} 

static void setupComp() throws IOException{ 
    b = "H"; 
    g = panel.getGraphics(); 

    ////////DESTROYER 
    j = (int) (Math.random()*1); 
    computer[1].Name = "destroyer"; 
    if(j == 0){ 
     b = "H"; 
     deS = ImageIO.read(new File("src/resources/"+computer[1].Name+b+".png"));} 
    else if(j == 1){ 
     b = "V"; 
     deS = ImageIO.read(new File("src/resources/"+computer[1].Name+b+".png"));} 

    computer[1].x[0] = ((int)(Math.random()*150)); 
    computer[1].x[0] = computer[1].x[0]-computer[1].x[0]%25; 
    computer[1].y[0] = ((int)(Math.random()*150)); 
    computer[1].y[0] = computer[1].y[0]-computer[1].y[0]%25; 
    ///////END DESTROYER 

    //////PATROL BOAT 
    j = (int) (Math.random()*1); 
    computer[2].Name = "patrolBoat"; 
    switch(j){ 
    case 0: 
     b = "H"; 
     pB = ImageIO.read(new File("src/resources/"+computer[2].Name+b+".png")); 
    case 1: 
     b = "V"; 
     pB = ImageIO.read(new File("src/resources/"+computer[2].Name+b+".png")); 
    } 
    computer[2].x[0] = ((int)(Math.random()*225)); 
    computer[2].x[0] = computer[2].x[0]-computer[2].x[0]%25; 
    computer[2].y[0] = ((int)(Math.random()*225)); 
    computer[2].y[0] = computer[2].y[0]-computer[2].y[0]%25; 


    ///////END PATROL BOAT 

    ///////AIRCRAFT CARRIER 
    j = (int) (Math.random()*1); 
    computer[3].Name = "aircraftCarrier"; 
    switch(j){ 
    case 1:b = "H"; 
     aC = ImageIO.read(new File("src/resources/"+computer[3].Name+b+".png")); 
    case 0: 
     b = "V"; 
     aC = ImageIO.read(new File("src/resources/"+computer[3].Name+b+".png")); 
    } 
    computer[3].x[0] = ((int)(Math.random()*125)); 
    computer[3].x[0] =computer[3].x[0]-computer[3].x[0]%25; 
    computer[3].y[0] = ((int)(Math.random()*125)); 
    computer[3].y[0] = computer[3].y[0]-computer[3].y[0]%25; 
    ///////END AIRCRAFT CARRIER 

    ///////SUBMARINE 
    j = (int) (Math.random()*1); 
    computer[4].Name = "submarine"; 
    switch(j){ 
    case 0:b = "H"; 
     suB = ImageIO.read(new File("src/resources/"+computer[4].Name+b+".png")); 
    case 1: 
     b = "V"; 
     suB = ImageIO.read(new File("src/resources/"+computer[4].Name+b+".png")); 
    } 
    computer[4].x[0] = ((int)(Math.random()*200)); 
    computer[4].x[0] = computer[4].x[0]-computer[4].x[0]%25; 
    computer[4].y[0] = ((int)(Math.random()*200)); 
    computer[4].y[0] = computer[4].y[0]-computer[4].y[0]%25; 
    //END SUBMARINE 

    ///////BATTLESHIP 
    j = (int) (Math.random()*1); 
    computer[0].Name = "battleship"; 
    switch(j){ 
    case 1:b = "H"; 
     bS = ImageIO.read(new File("src/resources/"+computer[0].Name+b+".png")); 
    case 0: 
     b = "V"; 
     bS = ImageIO.read(new File("src/resources/"+computer[0].Name+b+".png")); 
    } 
    computer[0].x[0] = ((int)(Math.random()*200)); 
    computer[0].x[0] = computer[0].x[0]-computer[0].x[0]%25; 
    computer[0].x[0] = ((int)(Math.random()*200)); 
    computer[0].x[0] = computer[0].y[0]-computer[0].y[0]%25; 
    ///////END BATTLESHIP 

    System.out.println(computer[0].x[0]+","+computer[0].y[0]); 
    System.out.println(computer[1].x[0]+","+computer[1].y[0]); 
    System.out.println(computer[2].x[0]+","+computer[2].y[0]); 
    System.out.println(computer[3].x[0]+","+computer[3].y[0]); 
    System.out.println(computer[4].x[0]+","+computer[4].y[0]); 
    g.drawImage(bS, computer[0].x[0], computer[0].x[0], null); 
    g.drawImage(aC, computer[1].x[0], computer[1].x[0], null); 
    g.drawImage(pB, computer[2].x[0], computer[2].x[0], null); 
    g.drawImage(suB, computer[3].x[0], computer[3].x[0], null); 
    g.drawImage(deS, computer[4].x[0], computer[4].x[0], null); }} 
+0

对不起,如果有任何不便 – user1092953

+0

为了更好地提供帮助,请发布[SSCCE](http://sscce.org/)。 –

回答

5

绘制和图像不要使用getGraphics()获得组件的图形对象,从未呼叫paintComponent(...)直接自己。通过getGraphics()获得的图形对象只是临时性的,并且在重新绘制时不会持续。

直接在paintComponent方法(或由其调用的方法)中执行所有绘画,或者间接地在PaintObjects中显示的BufferedImage中执行所有绘画。然后在需要显示更改的图像的组件上调用repaint()。这将告诉JVM它应该调用paintComponent(...)并传递一个有效的Graphics对象。

最重要的是,阅读图形教程,看看如何做到这一点。这是所有你需要了解和学习的地方 - 你的“搜索的一周左右”应该已经找到了这些,因为他们将在任何有关该主题的Google搜索的顶部。

编辑
说了这么多,东西告诉我,你会做的不会有任何此图的业务都搞乱更好,而是创建一个从您的图片ImageIcons,然后在显示的JLabel他们。这是使用Java Swing显示图像的最简单方法。