【问题标题】:Drawing paint over a buffered image?在缓冲图像上绘画?
【发布时间】:2015-04-11 02:46:33
【问题描述】:

大家好,我知道这是一个常见的,但我已经搜索了很多,似乎无法让我的绘画方法在我的 JPanel 中绘制组件。

我的绘画方法与按钮按下相关联。它打印出大约 1500 个数据点及其分配的集群 (kmeans)

 package eye_pathscanner;


import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.util.ArrayList;
import java.util.Random;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class ReplayData extends JPanel {

// replay type can be parsed as argument into draw() to change paints behaviour
private int ReplayType = 0;

public ArrayList<DataPoint> points;

//Initialise records
public ReplayData()
{
    points = new ArrayList<DataPoint>();
}

public void ReplaceData() {
    points = new ArrayList<DataPoint>();
}

public void PrintPoints()
{

}
public void addPoint(DataPoint point) {
    points.add(point);

}

@Override
public void paintComponent(Graphics g) {

    Color black = new Color(0, 0, 0);
    Random random = new Random();
    final float luminance = 0.9f;

    if (ReplayType == 1)
    {
        super.paintComponent(g);

        for (int x = 0; x < kMeans.NUM_CLUSTERS; x++)
        {
            // Saturation ideal between 0.1 and 0.3
            float saturation = (random.nextInt(2000) + 1000) / 10000f;
            float hue = random.nextFloat();
            Color cluster_colour = Color.getHSBColor(hue, saturation, luminance);

            // Randomise the border colour
            saturation = (random.nextInt(2000) + 1000) / 10000f;
            hue = random.nextFloat();
            Color cluster_colour_border = Color.getHSBColor(hue, saturation, luminance);

            double centroidx = kMeans.centroids.get(x).getmX();
            double centroidy = kMeans.centroids.get(x).getmY();

            for (int i = 0; i < kMeans.TOTAL_DATA; i++) 
                if(kMeans.dataSet.get(i).cluster() == x){
                    // Set each child data point to a colour so you can see which cluster it belongs too
                    g.setColor(cluster_colour);
                    g.fillRect((int)TrackerData.getRecordNumber(i).getEyeX(),(int)TrackerData.getRecordNumber(i).getEyeY(), 3, 3);
                    g.drawLine((int)kMeans.dataSet.get(i).X(),(int)kMeans.dataSet.get(i).Y(), (int)centroidx, (int)centroidy);
                    //g.setColor(Color.black);
                    g.setColor(cluster_colour_border);
                    g.drawRect((int)TrackerData.getRecordNumber(i).getEyeX(),(int)TrackerData.getRecordNumber(i).getEyeY(), 3, 3);
                }

            g.setColor(black);
            g.fillOval((int)centroidx,(int)centroidy, 15, 15);


        }
    }


}

// 1 for K-means with different colour cluster groups
// 2 for slow replay

public void draw(int i) {
    ReplayType = i;
    repaint();
}

}

这段代码对我来说都很好用,但是在使用后我丢失了在油漆下绘制的图像。我可以最大化页面并且图像再次显示但在油漆上? (谁能解释这种行为)。

    JLabel picture_panel = new JLabel();
    picture_panel.setBounds(70, 130, 640, 797);
    picture_panel.addMouseListener(this);   
    BufferedImage img = null;

    try 
    {
        img = ImageIO.read(new File("C:/Eyetracker_Images/Random.jpg")); // eventually C:\\ImageTest\\pic2.jpg
        ImageIcon icon = new ImageIcon(img);
        picture_panel.setIcon(icon);

    } 
    catch (IOException e) 
    {
        e.printStackTrace();
    }

我的图像在此处创建,在我的按钮上如下所示调用它

 replayData.setBounds(0, 0, 802, 977);
 frame.getContentPane().add(replayData); 
 replayData.draw(1);  

任何帮助将不胜感激,谢谢。

【问题讨论】:

标签: java swing jlabel imageicon


【解决方案1】:

这可能是使用setBounds() 的产物。此外,您似乎正在使用默认布局管理器,而没有在封闭容器上调用 pack()

由于您已经有一个包含渲染图像的BufferedImage,因此只需在您的paintComponent() 实现中调用drawImage()。示例见hereherehere

还考虑覆盖ReplayDatagetPreferredSize() 方法,如建议的herehere 所建议的那样,以建立其维度。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-13
    • 2012-06-25
    • 2018-02-12
    • 2014-04-07
    • 2021-06-05
    相关资源
    最近更新 更多