2013-02-23 200 views
3

我一直在尝试重写并使用paint组件方法而不是paint方法,因为我已经看到它在这里的多个问题中被提出。paintComponent不工作(Java)

我已经看了很多问题,但我仍然无法得到这个工作。我发布了用于呈现屏幕的原始代码片段。我在想,扩展JFrame不是正确的方法,而是我需要扩展JPanel,并从那里使用paint组件。我有另一个对象,我实际上扩展了JPanel,并添加了JFrame(用于渲染)。

下面是我用来渲染的对象,这个方法完全覆盖了paint方法。

package render; 


import java.util.Arrays; 

import javax.swing.*; 

import java.awt.*; //Graphics, Graphics2D, Image 

import sprites.Picture; 


public class Window extends JFrame{ 
    private static final long serialVersionUID = 1L; 

    public static Picture[] image_list = new Picture[0]; // All the images I want to render 
    private static String win_title = "Window"; // The name of the window 
    private static CustomComponents cc = new CustomComponents(); 


    public Window(){ 
     setTitle(win_title); // set my title 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // close when you hit x button 
     setUndecorated(true); // window has nothing on it 

    } 


    // paints all the images in image list according to their priority 

    public void display(){ 
     add(cc); 
     CustomComponents.updateImageList(image_list); 
     pack(); 

     setMinimumSize(getSize()); 
     setLocationRelativeTo(null); // puts the screen in the middle 
     setResizable(false); // can't resize the window 
     setVisible(true); // to see the window 


    } 



    public double[] getWinSize(){ 
     double[] size = {this.getSize().getWidth(),this.getSize().getWidth()}; 

     return size; 
    } 

    public static void endAll(){ 
     for (Picture i:image_list){ 
      i = null; 
     } 
     image_list = new Picture[0]; 
     CustomComponents.updateImageList(image_list); 
    } 

    // close the window (seen in the game object) 
    public static void close(){ 
     System.exit(0); 
    } 

    // called when adding a new sprite to the image_list array 
    public static void addPicture(Picture element){ 
     Picture[] result = Arrays.copyOf(image_list, image_list.length +1); // does the same thing as the addObject method in objects 
     result[image_list.length] = element; 
     image_list = result; 
     Arrays.sort(image_list); 
     CustomComponents.updateImageList(image_list); 

    } 

    // updates the screen... Just repaints everything 
    public void update() { 
     cc.repaint(); 

    } 

} 


class CustomComponents extends JComponent { 

    private static final long serialVersionUID = 1L; 

    private static Picture[] image_list; 

    public static void updateImageList(Picture[] image_list){ 
     CustomComponents.image_list = image_list; 
    } 

    @Override 
    public Dimension getMinimumSize() { 
     return new Dimension(640,360); 
    } 

    @Override 
    public Dimension getPreferredSize() { 
     return new Dimension(640,360); 
    } 

    @Override 
    public Dimension getMaximumSize() { 
     return new Dimension(640,360); 
    } 

    @Override 
    public void paintComponent(Graphics graphics) { 
     super.paintComponent(graphics); 
     Graphics2D g2d = (Graphics2D) graphics; 
     for (Picture i:image_list){ 
      if (i.getVisibility()){ 
      g2d.drawImage(i.getPic(), i.getXY()[0], i.getXY()[1], this); 
      } 
     } 
     Toolkit.getDefaultToolkit().sync(); // this is for linux machines 
     graphics.dispose(); // just good programming practice to collect the garbage 
    } 
} 

我会发布实际添加到窗口中的对象,但现在它太复杂了,只有少数事情发生。在构造函数中,我添加上面的JFrame窗口,然后使用计时器调用JFrame对象的更新方法。

如果你们真的需要我可以发布的代码,但希望这将足够。

+0

更改'公共无效paintComponent(图形图像) {'to'@Override public void paintComponent(Graphics graphics){'用于在编译时输出一些结果。每当重写方法检查签名时使用它,并且该方法存在。 ;) – 2013-02-23 15:45:27

+0

除了其他的建议 - 不要覆盖update()!也不要调用你的自定义类窗口,这个名称已经有一个AWT compoent,这会导致混淆。类应该有描述性名称。 – camickr 2013-02-23 17:13:26

+0

请勿使用graphics.dispose()。该评论与您创建Graphics对象时更相关。在这种情况下,Graphics对象被传递给组件,并被其他组件使用。 – camickr 2013-02-23 17:18:32

回答

10
  • 不用油漆

    1. 使用paintComponent()直接向JFrame
    2. 不是好主意,直​​接描绘到JFrameJFrame是不妥当的容器这份工作
    3. 有使用paint()代替paintComponent()代替JFrame,
  • @Override之前你认为应该重写父方法,这里是paintComponent(...)方法。这样,编译器会警告你,如果你实际上没有压倒你的想法。

  • JComponent/JPanelJFrame

  • 覆盖getPreferredSizeJComponent/JPanel(你的硬编码// window size handle),otherwise JComponent/JPanel returns zero dimension,简单的什么都不会是可见的creen

+0

对不起,您能否进一步解释,我无法跟进。 我不明白,“把JComponent/JPanel放到JFrame中”。 我仍然不确定我需要修复的问题:/ – Linkxgl 2013-02-23 15:57:16

+0

您是否尝试过在我的帖子中链接过代码示例,这里有关于Oracles教程[Trail:2D Graphics](http://docs.oracle.com)的基本内容/ JavaSE的/教程/ 2D /索引。html) – mKorbel 2013-02-23 16:05:54

+0

@Linkxgl:mKorbel对他的建议是正确的。 1+。如果您无法实施他的建议,请向我们展示您对原始问题底部的编辑尝试,并让我们知道此尝试的任何问题。 – 2013-02-23 16:40:56