0

我想用不同的图像重绘背景。这是一个 SSCCE 示例 - 我想向 Listener 添加一个动作,它将用另一个图像重新绘制。

package painting;

import javax.swing.SwingUtilities;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.BorderFactory;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseMotionAdapter;

public class SwingPaintDemo3 {

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            createAndShowGUI(); 
        }
    });
}

private static void createAndShowGUI() {
    System.out.println("Created GUI on EDT? "+
    SwingUtilities.isEventDispatchThread());
    JFrame f = new JFrame("Swing Paint Demo");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    f.add(new MyPanel());
    f.pack();
    f.setVisible(true);
} 
}

class MyPanel extends JPanel {

private int squareX = 50;
private int squareY = 50;
private int squareW = 20;
private int squareH = 20;
Image img;

public MyPanel() {

    setBorder(BorderFactory.createLineBorder(Color.black));

    addMouseListener(new MouseAdapter() {
        public void mousePressed(MouseEvent e) {
            moveSquare(e.getX(),e.getY());
        }
    });

    addMouseMotionListener(new MouseAdapter() {
        public void mouseDragged(MouseEvent e) {
            moveSquare(e.getX(),e.getY());
        }
    });

}

private void moveSquare(int x, int y) {
    int OFFSET = 1;
    if ((squareX!=x) || (squareY!=y)) {
        repaint(squareX,squareY,squareW+OFFSET,squareH+OFFSET);
        squareX=x;
        squareY=y;
        repaint(squareX,squareY,squareW+OFFSET,squareH+OFFSET);
    } 
}


public Dimension getPreferredSize() {
    return new Dimension(250,200);
}

protected void paintComponent(Graphics g) {
    super.paintComponent(g);      
try {
    img = ImageIO.read(new File("graphics/close_0.jpg"));
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
    g.drawImage(img, 0, 0, null); 
    g.drawString("This is my custom Panel!",10,20);
    g.setColor(Color.RED);
    g.fillRect(squareX,squareY,squareW,squareH);
    g.setColor(Color.BLACK);
    g.drawRect(squareX,squareY,squareW,squareH);
}  
}

那么我应该调用什么方法来用不同的图像重绘背景呢?

4

1 回答 1

1

据我所知,这个问题(“很少”)可能会引导您前进。请注意,代码不应尝试paint在orpaintComponent方法中进行潜在的阻塞操作(如加载图像) 。

import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.event.*;
import java.util.Random;

public class SwingPaintDemo3 {

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            createAndShowGUI();
        }
    });
}

private static void createAndShowGUI() {
    System.out.println("Created GUI on EDT? "+
    SwingUtilities.isEventDispatchThread());
    JFrame f = new JFrame("Swing Paint Demo");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.add(new MyPanel());
    f.pack();
    f.setVisible(true);
}
}

class MyPanel extends JPanel {

private int squareX = 50;
private int squareY = 50;
private int squareW = 20;
private int squareH = 20;
Image img;
Random r = new Random();

public MyPanel() {
    img = new BufferedImage(40,40,BufferedImage.TYPE_INT_RGB);

    setBorder(BorderFactory.createLineBorder(Color.black));

    addMouseListener(new MouseAdapter() {
        public void mousePressed(MouseEvent e) {
            img = new BufferedImage(
                r.nextInt(getWidth())+1,
                r.nextInt(getHeight())+1,
                BufferedImage.TYPE_INT_RGB);
            moveSquare(e.getX(),e.getY());
        }
    });
}

private void moveSquare(int x, int y) {
    int OFFSET = 1;
    if ((squareX!=x) || (squareY!=y)) {
        squareX=x;
        squareY=y;
        repaint();
    }
}


public Dimension getPreferredSize() {
    return new Dimension(250,200);
}

protected void paintComponent(Graphics g) {
    super.paintComponent(g);

    g.drawImage(img, 0, 0, null);
    g.drawString("This is my custom Panel!",10,20);
    g.setColor(Color.RED);
    g.fillRect(squareX,squareY,squareW,squareH);
    g.setColor(Color.BLACK);
    g.drawRect(squareX,squareY,squareW,squareH);
}
}
于 2013-04-19T12:14:51.490 回答