简体   繁体   English

Java Swing paintComponent()覆盖

[英]Java Swing paintComponent() override

I want to set an Image as background in some JComponent ( JPanel , JLayeredPane ,..). 我想在一些JComponentJPanelJLayeredPane ,..)中将Image设置为背景。 To do that I created the class JImagePanel that extends JPanel and overrides the method. 为此,我创建了JImagePanel类,它扩展了JPanel并覆盖了该方法。 Same thing for the other components. 其他组件也是如此。 I now have 3 classes that differ only for what they extend. 我现在有3个类只是因为它们的扩展而有所不同。 I wanted to create a unique class JImageComponent that extends JComponent , and then all the others will extend it, but I can't since the classes I created already extend a class ( JImagePanel extends JPanel ). 我想创建一个扩展JComponent的唯一类JImageComponent ,然后所有其他人都会扩展它,但我不能,因为我创建的类已经扩展了一个类( JImagePanel扩展了JPanel )。 Is there any other way I can do that? 有没有其他方法可以做到这一点?

This is the code of one of the classes I created: 这是我创建的其中一个类的代码:

public class JImagePanel extends JPanel {

  private static final int DEFAULTX = 0;
  private static final int DEFAULTY = 0;
  private static final int DEFAULTWIDTH = 1100;
  private static final int DEFAULTHEIGHT = 700;

  private BufferedImage img;
  private int imagex;
  private int imagey;
  private int imagewidth;
  private int imageheight;
  private static final long serialVersionUID = 1L;

  public JImagePanel(String path){
        super();
        this.imagex=JImagePanel.DEFAULTX;
        this.imagey=JImagePanel.DEFAULTY;
        this.imagewidth=JImagePanel.DEFAULTWIDTH;
        this.imageheight=JImagePanel.DEFAULTHEIGHT;
        img = loadImage(path);
  }

  public JImagePanel(String path,int x,int y, int width, int height){
        super();
        this.imagex=x;
        this.imagey=y;
        this.imagewidth=width;
        this.imageheight=height;
        img = loadImage(path);
  }

  @Override
  protected void paintComponent(Graphics graphics) {
        super.paintComponent(graphics);

       setOpaque(false);
       graphics.drawImage(img, imagex, imagey,imagewidth,imageheight, null);
  }
}

Thank you. 谢谢。

What you are suggesting isn't logically possible since JPanel , JLayeredPane , etc. already extend JComponent . 由于JPanelJLayeredPane等已经扩展了JComponent ,因此您的建议在逻辑上是不可能的。 What you're trying to do is add a new step in the hierarchy. 您要做的是在层次结构中添加一个新步骤。 Right now it goes JImagePanel -->JPanel --> JComponent , etc. and you're trying to make it JImagePanel --> JPanel --> JImageComponent --> JComponent . 现在它是JImagePanel -->JPanel --> JComponent等,你正试图让它成为JImagePanel --> JPanel --> JImageComponent --> JComponent To do this, you would have to change the JPanel class so it actually extends JImageComponent . 为此,您必须更改JPanel类,以便它实际扩展JImageComponent

Here's my suggestion: Don't use JImageComponent on the classes that already extend components. 这是我的建议:不要在已经扩展组件的类上使用JImageComponent If you make new components that don't extend pre-existing components, have them extend JImageComponent , or JComponent directly. 如果您创建的新组件不扩展预先存在的组件,请让它们直接扩展JImageComponentJComponent If the purpose of your JImageComponent class was to provide unity to your elements, consider making an interface for all your elements to implement. 如果JImageComponent类的目的是为元素提供统一性,请考虑为要实现的所有元素创建一个接口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM