我正在尝试为游戏开发带有透明按钮的菜单。
我的问题是,每次我将鼠标悬停时,按钮就会堆积在一起。
因此,我添加了一个执行myJFrame.repaint()的MouseListener。每次我将它们悬停时。
问题是,重新粉刷太慢,打扰了用户。
我希望有人知道更好的方法。

如果有错误,我很抱歉,我的英语不是最好的。

public class Hauptmenue extends javax.swing.JFrame implements ActionListener {
private static final long serialVersionUID = 8132389688291883346L;

//Toolkit für das Freie Skalieren des Hauptmenüs
private Toolkit t;
//variablen für das Fenster
private int x, y, width, height;

//variablen für die Köpfe
private int kx, kwidth, kheight;

//Variablen für die Knöpfe im Hauptmenü:
private JButton2 single;
private JButton2 multi;
private JButton2 einstellungen;
private JButton2 info;
private JButton2 ende;

//Hintergrund des Hauptmenüs
Image background;

public Hauptmenue(){
    //Bildschirmgröße messen:
    t = Toolkit.getDefaultToolkit();
    Dimension d = t.getScreenSize();
    width = (int)(d.getWidth() * 0.23);
    height = (int)(d.getHeight() * 0.5);
    x = (int)((d.getWidth() - width) * 0.5);
    y = (int)((d.getHeight() - height) * 0.5);

    setTitle("Hauptmenü");
    setBounds(x, y, width, height);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLayout(null);
    setBackground(null);


    //Hintergrnd Bild
    //setFocusable(true);
    File pfad = new File("res/Images/Hintergrund_1.png");
    ImageIcon u = new ImageIcon(pfad.getPath());
    background = u.getImage();
    //background = background.getScaledInstance(width, height, Image.SCALE_DEFAULT);

    //sorgt dafür, dass das Menü keinen Rahmen hat
    setUndecorated(true);

    //knopfpositionen und Größen berechnen
    kwidth = (int)(width * 0.8);
    kheight = (int)(height * 0.125);
    kx = (int)(width - kwidth) / 2;

    //die Knöpfe:
    single = new JButton2("Singelplayer");
    single.setBounds(kx, (kheight * 1), kwidth, kheight);
    single.addActionListener(this);
    single.addMouseListener(new java.awt.event.MouseAdapter() {
        public void mouseEntered(java.awt.event.MouseEvent evt) {
            JFrame frme = (JFrame) info.getParent().getParent().getParent().getParent();
            frme.repaint();
        }

        public void mouseExited(java.awt.event.MouseEvent evt) {
            JFrame frme = (JFrame) info.getParent().getParent().getParent().getParent();
            frme.repaint();
        }
    });
    add(single);

    multi = new JButton2("Multiplayer");
    multi.setBounds(kx, (int)(kheight * 2.25), kwidth, kheight);
    multi.addActionListener(this);
    multi.addMouseListener(new java.awt.event.MouseAdapter() {
        public void mouseEntered(java.awt.event.MouseEvent evt) {
            JFrame frme = (JFrame) info.getParent().getParent().getParent().getParent();
            frme.repaint();
        }

        public void mouseExited(java.awt.event.MouseEvent evt) {
            JFrame frme = (JFrame) info.getParent().getParent().getParent().getParent();
            frme.repaint();
        }
    });
    add(multi);

    einstellungen = new JButton2("Einstellungen");
    einstellungen.setBounds(kx, (int)(kheight * 3.5), kwidth, kheight);
    einstellungen.addActionListener(this);
    einstellungen.addMouseListener(new java.awt.event.MouseAdapter() {
        public void mouseEntered(java.awt.event.MouseEvent evt) {
            JFrame frme = (JFrame) info.getParent().getParent().getParent().getParent();
            frme.repaint(1);
        }

        public void mouseExited(java.awt.event.MouseEvent evt) {
            JFrame frme = (JFrame) info.getParent().getParent().getParent().getParent();
            frme.repaint(1);
        }
    });
    add(einstellungen);

    info = new JButton2("Info");
    info.setBounds(kx, (int)(kheight * 4.75), kwidth, kheight);
    info.addActionListener(this);
    info.addMouseListener(new java.awt.event.MouseAdapter() {
        public void mouseEntered(java.awt.event.MouseEvent evt) {
            JFrame frme = (JFrame) info.getParent().getParent().getParent().getParent();
            frme.repaint();
        }

        public void mouseExited(java.awt.event.MouseEvent evt) {
            JFrame frme = (JFrame) info.getParent().getParent().getParent().getParent();
            frme.repaint();
        }
    });
    add(info);

    ende = new JButton2("Beenden");
    ende.setBounds(kx, (kheight * 6), kwidth, kheight);
    ende.addActionListener(this);
    ende.addMouseListener(new java.awt.event.MouseAdapter() {
        public void mouseEntered(java.awt.event.MouseEvent evt) {
            JFrame frme = (JFrame) info.getParent().getParent().getParent().getParent();
            frme.repaint();
        }

        public void mouseExited(java.awt.event.MouseEvent evt) {
            JFrame frme = (JFrame) info.getParent().getParent().getParent().getParent();
            frme.repaint();
        }
    });
    add(ende);



    setVisible(true);
}

//der Hintergrund:


//zum Zeichnen des Hintergrundes:
public void paint(Graphics g){
    super.paint(g);
    Graphics2D f2 = (Graphics2D)g;
    f2.drawImage(background, 0, 0, width, height, null);

    //Knöpfe in den Vordergrund holen
    this.single.paint(this.single.getGraphics());
    this.multi.paint(this.multi.getGraphics());
    this.einstellungen.paint(this.einstellungen.getGraphics());
    this.info.paint(this.info.getGraphics());
    this.ende.paint(this.ende.getGraphics());

    g.dispose();
}

//Funktionen hinter den Knöpfen:
private void Singleplayer(){

}

private void Multiplayer(){

}

private void Einstellungen(){
    new Einstellungsfenster();
}

private void Info(){
    new Infofenster();
}

private void Beenden(){
    System.exit(0);
}

//Reaktionen auf die Knopfdrücke:
public void actionPerformed (ActionEvent e){
    if(e.getSource() == single){
        Singleplayer();
    }
    if(e.getSource() == multi){
        Multiplayer();
    }
    if(e.getSource() == einstellungen){
        Einstellungen();
    }
    if(e.getSource() == info){
        Info();
    }
    if(e.getSource() == ende){
        Beenden();
    }
}

//Programmstart
public static void main(String[]args){
    //das Hauptmenü wird erzeugt
    new Hauptmenue();
}}


另外,我对JButton进行了一些更改:

public class JButton2 extends JButton {
private static final long serialVersionUID = 5193885870007603559L;


public JButton2(){

}

public JButton2(String text){
    this.setText(text);
    //this.setOpaque(false);
    //this.setContentAreaFilled(false);
    //this.setBackground(null);
}

@Override
public void paint(Graphics g){
    //super.paint(g);
    Graphics2D g2 = (Graphics2D) g.create();
    g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, (float) 0.8));
    super.paint(g2);
    g.dispose();
    g2.dispose();
}

//@Override
public void repaint(){
    //hier passiert nichts
}}

最佳答案

调用任何repaint()方法只会将要重新绘制的Component排队在稍后的时间。如果您在RepaintManager实际对Component进行重新绘制之前多次调用repaint(),它们将被简单地组合为一个重新绘制操作。

您应该尝试使用paintImmediately()方法之一,因为这些方法会立即执行重新绘制(您不能从名称中猜出该值)。

有关其他信息,请阅读:
Painting in AWT and Swing

关于java - 我如何比使用repaint()更快地更新JFrame;,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29904050/

10-11 22:33