重绘JLabel,图像应用程序的问题

时间:2013-02-20 12:56:00

标签: java swing jpanel jlabel hsb

所以这是我们的代码......我们的nobs并需要一些帮助,让我们的按钮在显示高度和宽度的色调/饱和度/亮度之间切换。目前,我们的应用程序仅显示宽度和高度饱和度的色调。我们的按钮不起作用,我们无法清空视图并重新绘制另一个视图。有人可以帮忙吗?

package no.uib.info221.oblig1;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import java.util.List;

import javax.imageio.ImageIO;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import com.github.imgur.ImgUr;
import com.github.imgur.api.album.AlbumRequest;
import com.github.imgur.api.album.AlbumResponse;
import com.github.imgur.api.model.ImageProperty;


public class ImgurViz extends JFrame {
    private static final long serialVersionUID = 7954882289770950884L;
    //private JProgressBar progressBar;
    private JButton HSbutton;
    private JButton BSbutton;
    private JButton BHbutton; 
    private JPanel knappepanel;
    private JPanel bildepanel; 
    public JLabel skjerm;
    int w = 0;
    int h = 1;  

    /**
     * constructor
     * klassen.
     * @throws IOException 
     */

    public ImgurViz() throws IOException {
        super(); //Calls the superclass
        setupInterface(); //        
        fetchAndDisplayImages(); //     
    }

    //Empties the panel when a new jlabel is called
    public void emptyPanel(){   
        bildepanel.revalidate(); 
        bildepanel.removeAll();
        bildepanel.repaint();       
    }

    // Creates JButtons to change the sorting of the images
    public void knapper(){
        knappepanel = new JPanel(); // Knappepanel = button panel
        bildepanel = new JPanel();      // Imagepanel = image panel
        bildepanel.setLayout(null);
        this.getContentPane().setLayout(new BorderLayout());
        this.getContentPane().add(knappepanel,BorderLayout.NORTH);
        this.getContentPane().add(bildepanel,BorderLayout.CENTER);


        //knappepanel.setOpaque(false);

        // Knapp for HUE og SATURATION KNAPP = button
        HSbutton = new JButton ("Hue og Saturation");
        knappepanel.add(HSbutton);

        //Knapp for BRIGHTNESS og SATURARION
        BSbutton = new JButton ("Brightness og Saturation");
        knappepanel.add(BSbutton);

        // Knapp for BRIGHTNESS OG HUE
        BHbutton = new JButton ("Brightness og Hue");
        knappepanel.add(BHbutton);      

        // Metode for knapp for HUE og SATURATION
        HSbutton.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e) {            
                emptyPanel();   

                w = 0; 
                h = 1;          

                System.out.println("Hue: " + w + " Saturation: " + h);
            }       
        });

        // Metode for knapp for BRIGHTNESS og SATURATION
        BSbutton.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent e) {

                w = 2; 
                h = 1;

                System.out.println("Brightness: " + w + " Saturation: " + h);
            }
        });

        // Metode for knapp for BRIGHTNESS OG HUE
        BHbutton.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e) {
                emptyPanel(); 

                w = 2; 
                h = 0;      
                System.out.println("Brightness: " + w + " Hue: " + h);
            }           
        });

        this.setContentPane(knappepanel);       
    }



    /**
     * Fetches images for an album on Imgur
     * 
     * @throws IOException
     */


    public void fetchAndDisplayImages() throws IOException {
        ImgUr imgUr = new ImgUr ("4ded93e63e96762");
        AlbumResponse response = imgUr.call(new AlbumRequest("RJBiQ"));
        List<ImageProperty> images = response.getImages();
        float[] bildedata = new float[3];
        int count = images.size();      

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

            BufferedImage img = ImageIO.read(new URL(images.get(i).getLinks().getOriginal()));          
            BufferedImage thumb = ImageIO.read(new URL(images.get(i).getLinks().getSmallSquare())); 
            bildedata = pictureAnalyze (img);

            int width  = ((int)Toolkit.getDefaultToolkit().getScreenSize().getWidth())/2; // Henter ut bredden p brukerens skjerm
            int height = ((int)Toolkit.getDefaultToolkit().getScreenSize().getHeight())/2; // Henter ut høyden p brukerens skjerm
            int posW = (int)(bildedata [w] * width);
            int posH = (int)(bildedata [h] * height);

            skjerm = new JLabel(new ImageIcon(thumb));
            skjerm.setBounds(posW, posH, width, height);
            bildepanel.add(skjerm);
            this.getContentPane().add(skjerm);
            this.getContentPane().setLayout(null);          
            this.getContentPane().repaint();                
            this.setVisible(true);      

//          skjerm.addMouseListener(new MouseAdapter() {
//              @Override
//              public void mouseEntered(java.awt.event.MouseEvent evt) {
//                  //FAEN TA DEG!!
//              }
//          });
        }
    }

    // RGB to HSB Finds the RGB value of each image, transformes the values to HSB and finds the average 
    public float[] pictureAnalyze (BufferedImage img){
        float[] result = new float[3];          
        float h = 0f; 
        float s = 0f;
        float b = 0f; 
        for (int x = 0; x < img.getWidth(); x++){
            for (int y = 0; y < img.getHeight(); y++){
                int c = img.getRGB(x,y);        
                int red = (c >> 16) & 0xff;
                int green = (c >> 8) & 0xff;
                int blue = (c & 0xff);

                float[] hsb = Color.RGBtoHSB(red,green,blue, null);

                h += hsb[0];
                s += hsb[1];
                b += hsb[2];       
            }
        }

        h = h/(img.getWidth()*img.getHeight());
        s = s/(img.getWidth()*img.getHeight());
        b = b/(img.getWidth()*img.getHeight());
        result [0] = h;
        result [1] = s;
        result [2] = b;
        return result;
    }

    //      // Shannon entropi
    //      public static Double calculateShannonEntropy(List <ImageProperty> images) {         
    //            Map<String, Integer> map = new HashMap<String, Integer>();    
    //            
    //            // tel kvar instans av verdiane           
    //            for (String sequence : images) {          
    //              if (!map.containsKey(sequence)) {           
    //                map.put(sequence, 0);
    //          
    //              }           
    //              map.put(sequence, map.get(sequence) + 1);           
    //            }      
    //            
    //            //Kalkulerer entropien
    //          
    //            Double result = 0.0;          
    //            for (String sequence : map.keySet()) {            
    //              Double frequency = (double) map.get(sequence) / images.size();          
    //              result -= frequency * (Math.log(frequency) / Math.log(2));          
    //            }      
    //          
    //            return result;
    //          
    //          }


    /**

    private void setupInterface() {
        int width  = (int)Toolkit.getDefaultToolkit().getScreenSize().getWidth(); // Henter ut bredden p brukerens skjerm
        int height = (int)Toolkit.getDefaultToolkit().getScreenSize().getHeight(); // Henter ut h¯yden p brukerens skjerm
        this.setSize(width, height); // Setter st¯rrelsen p vinduet til  vÊre lik skjermens bredde og h¯yde
        this.setExtendedState(Frame.MAXIMIZED_BOTH); // Sier fra om at vinduet skal vÊre maksimert 
        this.setUndecorated(true); // setter fullscreen
        this.getContentPane().setBackground(Color.BLACK ); // Setter bakgrunnsfargen p innholdspanelet (contentPane) til svart 
        knapper(); 
        this.setVisible(true); // Viser vinduet p skjermen

    }


    public static void main(String[] args)  throws IOException { 
        ImgurViz viz = new ImgurViz(); // Oppretter et nytt ImgurViz-objekt. Dette medf¯rer at det nye objektets konstrukt¯r kj¯res

    }



}

1 个答案:

答案 0 :(得分:1)

JColorChooser的HSV面板所示,一次只显示两个颜色空间three dimensions更容易。无论您的目标是什么,使用Color.getHSBColor()缩放调色板都会更容易。这些examples可能会提供一些见解或帮助您准备sscce