2017-04-02 70 views
0

为什么JPanel(面板)不能在绿色背景(jpanel)上绘制?我希望能够做到这一点,而无需扩展j面板...为什么JPanel(面板)不能在绿色背景(jpanel)上绘制?

此外,对于java游戏,我应该在java中使用keybindings或keylistener。

import javax.swing.*; 
import java.awt.event.*; 
import java.awt.Color; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 

public class Game { 

JFrame window; 
JPanel panel; 

int charPosX = 0; 
int charPosY = 0; 

public Boolean createGui() { 

    window = new JFrame("Game"); 
    window.setSize(1000,500); 
    window.setResizable(false); 
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    window.setVisible(true); 

    panel = new JPanel(); 
    panel.setVisible(true); 
    panel.setLayout(null);; 
    panel.setBackground(new Color(65,130,92)); 

    window.add(panel); 

    return true; //returns true if ran and will be ran by check status in Main. 
} 

public void paintComponent(Graphics g) { 
    panel.paintComponents(g); 
    g.setColor(Color.RED); 
    g.drawRect(100,10,30,40); 
    g.fillRect(10, 10, 20, 10); 
} 



} 
+0

问自己这个问题,谁是真正的调用你的'paintComponent'方法?也许在你的'paintComponent'中添加'@ Override'并且看看它的作用 – MadProgrammer

+1

Shell,我们还讨论了这个事实:你的'panel'没有可定义的大小,也不能管理你可能添加的任何子组件。作为一般性建议,我会避免以这种方式使用组件,并通过自定义绘画方法实现功能,因为它更高效且不易出错 – MadProgrammer

+0

定制绘画方法意味着什么? ???? – Blockeus

回答

1

让我们把你的代码,第二个,并添加@OverridepaintComponent方法...

public class Game { 

    //... 

    @Override 
    public void paintComponent(Graphics g) { 
     panel.paintComponents(g); 
     g.setColor(Color.RED); 
     g.drawRect(100, 10, 30, 40); 
     g.fillRect(10, 10, 20, 10); 
    } 

} 

而现在我们有一个编译器错误!这是因为Game扩展了Object并且没有paintComponent方法。这意味着该方法不可能被现有绘画系统的任何部分调用,所以它永远不会被调用。

组件做出错误的“游戏”的实体,他们有很多的“管道”,这并不能让他们对这种工作非常有效的,你通常最好标题下一个完整的自定义绘制路线

Example

import javax.swing.*; 
import java.awt.Color; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.Rectangle; 

public class Game { 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       new Game().createGui(); 
      } 
     }); 
    } 

    JFrame window; 
    GamePanel panel; 

    int charPosX = 0; 
    int charPosY = 0; 

    public Boolean createGui() { 

     window = new JFrame("Game"); 
     window.setSize(1000, 500); 
     window.setResizable(false); 
     window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     panel = new GamePanel(); 
     panel.setBackground(new Color(65, 130, 92)); 
     window.add(panel); 

     window.setVisible(true); 
     return true; //returns true if ran and will be ran by check status in Main. 
    } 

    public class GamePanel extends JPanel { 

     private Rectangle entity = new Rectangle(100, 10, 30, 40); 

     @Override 
     public void paintComponent(Graphics g) { 
      super.paintComponent(g); 
      Graphics2D g2d = (Graphics2D) g.create(); 
      g2d.setColor(Color.RED); 
      g2d.draw(entity); 
      g2d.setColor(Color.BLUE); 
      g2d.fill(entity); 
      g2d.dispose(); 
     } 

    } 

} 

另外请注意,我叫window.setVisible(true);我已经添加了panelwindow后,才,这是因为Swing是懒惰的,当涉及到添加/删除组件。如果您想要在屏幕上实现用户界面后添加/删除组件,则需要在容器上调用revalidaterepaint以触发布局和颜色通过

另外,请注意, paintComponentpaintComponents;)

我会强烈建议在看看Painting in AWT SwingPerforming Custom Painting以更好地了解绘画在Swing是如何工作的,以及如何利用它

+0

好..谢谢!非常多 – Blockeus

+0

btw即时通讯试图做到这一点,所以我可以在学校或什么idk玩游戏。 – Blockeus

+0

等待什么是GamePanel From。 ????另外,即使你创建了Jpanel(gamepanel idk它是什么),但是已经扩展JPanel – Blockeus