0

我使用相对平淡的纹理创建了一个背景(它重复得很好,所以这是一个奖励)。然而,最重要的是,我试图在随机位置添加两个图像,每个图像五次。所以我用这个试了一下——

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Insets;
import java.io.File;
import java.io.IOException;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class RepeatDiagonals {

    public static Image whiteOverlayStreak;
    public static Image blackOverlayStreak;
    public static JFrame framePanel;
    public static DiagonalImages diagTest;

    public static void createAndInitGUI() {
        diagTest = new DiagonalImages();

        framePanel = new JFrame("Diagonal Testing");
        framePanel.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        framePanel.setPreferredSize(new Dimension(1020, 720));
        framePanel.add(diagTest);
        framePanel.pack();
        framePanel.setVisible(true);

    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {

                createAndInitGUI();

            } // public void run() Closing
        }); // SwingUtilities Closing
    }
}


// TODO Add in constructor for better image import
class DiagonalImages extends JPanel {

    public static final String IMAGE_PATH_WHITESTREAK = "imageFolder/whiteBackgroundStreakOverlay.png";
    public static final String IMAGE_PATH_BLACKSTREAK = "imageFolder/blackBackgroundStreakOverlay.png";
    public static Image whiteOverlayStreak;
    public static Image blackOverlayStreak;
    public static Image overlayStreak;

    DiagonalImages() {
        loadImages();
        setVisible(true);
        setOpaque(false);
    };

    public void loadImages() {
        try {
            whiteOverlayStreak = ImageIO.read(new File(IMAGE_PATH_WHITESTREAK));
            blackOverlayStreak = ImageIO.read(new File(IMAGE_PATH_BLACKSTREAK));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;

        Dimension size = this.getSize();
        Insets insets = this.getInsets();

        int w = size.width - insets.left - insets.right;
        int h = size.height - insets.top - insets.bottom;

        Random randomInteger = new Random();
        randomInteger.nextInt(900);


        for (int i = 0; i < 3; i++) {

            int x = randomInteger.nextInt() % w;
            int y = randomInteger.nextInt() % h;

            g2d.drawImage(blackOverlayStreak, x, y, null);
        }

        for (int i2 = 0; i2 < 5; i2++){
            int x2 = randomInteger.nextInt() % w;
            int y2 = randomInteger.nextInt() % h;

            g2d.drawImage(whiteOverlayStreak, x2, y2, null);
        }
    }
}

主要代码的相关部分:

// Makes the Initial BorderLayout 
        allContent = new ImagePanel(image);
        allContent.setLayout(new BorderLayout());   
        allContent.add(new DiagonalImages());
        allContent.add(tabbedPane, BorderLayout.CENTER);
        allContent.add(logoImage, BorderLayout.NORTH);
        allContent.setVisible(true);
        allContent.setOpaque(false);

        // Add ScrollPane
        scrollPane = new JScrollPane(allContent);
        scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        scrollPane.getVerticalScrollBar().setUnitIncrement(10);
        scrollPane.setOpaque(false);
        scrollPane.getViewport().setOpaque(false);
        scrollPane.setBorder(new EmptyBorder(0, 0, 0, 0));
        scrollPane.setWheelScrollingEnabled(true);

        // JFrame programFrame Constructors
        programFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        programFrame.setLayout(new BorderLayout());
        programFrame.add(scrollPane);
        programFrame.pack();
        programFrame.setVisible(true);
        programFrame.setResizable(true);
        programFrame.setSize(1280, 720);
        programFrame.setLocationRelativeTo(null);

这是我拥有的 ImagePanel:

import java.awt.Graphics;
import java.awt.Image;

import javax.swing.JPanel;

class ImagePanel extends JPanel {
    private Image image;
    private boolean tile;

    ImagePanel(Image image) {
        this.image = image;
        this.tile = false;
    };

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        int iw = image.getWidth(this);
        int ih = image.getHeight(this);
        if (iw > 0 && ih > 0) {
            for (int x = 0; x < getWidth(); x += iw) {
                for (int y = 0; y < getHeight(); y += ih) {
                    g.drawImage(image, x, y, iw, ih, this);
                }
            }
        }
    }
}

感谢您的(未来)帮助!

编辑:根据给出的答案做了一个小改动,但它仍然无法正常工作。好的,所以问题是应该重复的图像实际上甚至没有出现。

EDIT2:为此重写了我的整个代码,但它仍然无法正常工作。即使设置背景颜色也不起作用,这让我相信这是我的paintComponent 的问题。

EDIT3:由于帮助,paintComponent 正在工作。我的最后一个问题是让它在我的主要方法中正常工作。

4

2 回答 2

1

第一个 JFrame.setVisible(true); 应该最后完成,然后pack()进行布局。

    framePanel.pack();

    framePanel.setVisible(true);

图像可能更好地驻留在应用程序(jar)本身中,然后您可以使用getClass().getResource("..."). 它们应该在油漆外面加载,比如在构造函数中。我猜,这是测试代码。

public static Image whiteOverlayStreak;
public static Image blackOverlayStreak;

DiagonalImages() {
    loadImages();
}

private void loadImages() {
    whiteOverlayStreak = new ImageIcon(
            getClass().getResource("/white.jpg")).getImage();
    blackOverlayStreak = new ImageIcon(
            getClass().getResource("/black.jpg")).getImage();
}

@Override
public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D) g;

        int x = r.nextInt(w);
        int y = r.nextInt(h);

你的失败没有使用@Override,因为那时你会看到,你写错了,Graphics2D g而不是Graphics g. 函数paintComponent 从未被调用过!哈哈


附加问题:添加第二个面板

    framePanel.setLayout(new BorderLayout());
    framePanel.add(diagTest, BorderLayout.CENTER);
    framePanel.add(otherPanel, BorderLayout.SOUTH);
于 2013-06-27T10:31:30.757 回答
0

这不是一个真正的SSCCE。

我还没有完全测试这个,主要是因为我不想为了指出明显的错误而建立一个完整的 Eclipse 项目。

  • 将图像处理与 JPanel 分开。

  • 仅在修改组件方法时扩展 Swing 组件。

这是我的代码版本。我不得不修改您的代码以读取图像以使其正常工作。您将不得不自己弄清楚这部分代码。

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Insets;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

class GUIImages {

    private Image   whiteDiagonal;

    // private Image blackDiagonal;

    public GUIImages() {
        loadImages();
    }

    private void loadImages() {
        try {
            whiteDiagonal = ImageIO.read(new File(
                    "C:/Documents and Settings/BOP00082/" + 
                    "My Documents/My Pictures/Places-icon.png"));

        } catch (IOException e) {
            e.printStackTrace();
        }
        // whiteDiagonal = new ImageIcon(this.getClass().getResource(
        // "imageFolder/whiteBackgroundStreakOverlay.png")).getImage();
        // blackDiagonal = new ImageIcon(this.getClass().getResource(
        // "imageFolder/blackBackgroundStreakOverlay.png")).getImage();
    }

    public void doDrawing(JPanel panel, Graphics g) {
        Graphics2D g2d = (Graphics2D) g;
        g2d.drawImage(createDiagonalImage(panel), 0, 0, null);
    }

    private Image createDiagonalImage(JPanel panel) {
        BufferedImage buffImg = new BufferedImage(677, 856,
                BufferedImage.TYPE_INT_ARGB);

        Graphics2D gbi = buffImg.createGraphics();

        Dimension size = panel.getSize();
        Insets insets = panel.getInsets();

        int w = size.width - insets.left - insets.right;
        int h = size.height - insets.top - insets.bottom;

        Random r = new Random();

        for (int i = 0; i < 5; i++) {

            int x = Math.abs(r.nextInt()) % w;
            int y = Math.abs(r.nextInt()) % h;

            gbi.drawImage(whiteDiagonal, x, y, null);
        }
        gbi.dispose();

        return buffImg;
    }
}

class Surface extends JPanel {

    GUIImages   images;

    public Surface(GUIImages images) {
        this.images = images;
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        images.doDrawing(this, g);
    }
}

public class RepeatDiagonals implements Runnable {

    JFrame  frame;

    @Override
    public void run() {
        frame = new JFrame();
        frame.setTitle("Repeat Diagonals");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.add(new Surface(new GUIImages()));

        frame.setSize(350, 250);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new RepeatDiagonals());
    }
}
于 2013-06-12T13:11:08.823 回答