【发布时间】:2014-12-06 03:47:15
【问题描述】:
我正在为一个班级制作一个程序。想法是显示图像并提高/降低对比度。这是示例。我能够使图像出现,并且像示例中那样在图像上方出现一个灰色条,但是我的按钮不存在。我不确定如何通过 FlowLayout 正确地做到这一点,或者我是否需要使用另一个布局。我是 GUI 新手,因此我们将不胜感激!
这就是我的样子。我究竟做错了什么?
import javax.imageio.ImageIO;
import javax.swing.*;
import java.util.*;
import java.io.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import java.applet.Applet;
/**
* This class extends JPanel and can load an image to its
* original size.
*
* @author Dahai Guo
*
*/
class ImagePanel extends JPanel{
private BufferedImage image;
public ImagePanel(BufferedImage image){
this.image=image;
}
/**
* Draws the image.
*/
public void paintComponent(Graphics g){
super.paintComponent(g);
g.drawImage(image,0,0,image.getWidth(),image.getHeight(),this);
}
}
/**
* Object of this class can load an image and enable the user
* to increase and decrease the contrast of the image.
*
* Note this class can only deal with the contrast of gray levels
*
* @author Dahai Guo
*
*/
public class HistgramEqualizerApp extends JFrame {
int count=0;
private FlowLayout myLayout;
private BorderLayout borderLayout;
private ImagePanel imagePanel;
private JButton increaseContrastButton;
private JButton decreaseContrastButton;
private BufferedImage image;
/**
* Cummulative density function
*/
private int [] cdf=new int[256];
/**
* The current largest gray level.
*/
private int imageMaxGrayLevel=0;
/**
* The current smallest gray level.
*/
private int imageMinGrayLevel=300;
/**
* The smallest nonzero cell in the CDF of gray level
* of the image.
*/
private int minCdfValue;
/**
* The largest gray level when the image is first loaded.
*/
private int originalImageMaxGray;
/**
* The smallest gray level when the image is first loaded.
*/
private int originalImageMinGray;
private int MAX_GRAY_LEVEL=255;
private int MIN_GRAY_LEVEL=0;
/**
* Sets up the GUI components and register the action listener for
* the two buttons for increasing and decreasing contrast
* @param filename the filename of the image
* @throws IOException thrown when problems occurs when loading the image
*/
public HistgramEqualizerApp(String filename)
throws IOException{
// insert your code
try {
image = ImageIO.read(new File(filename));
} catch (IOException e) {
System.err.println("Problem loading image.");
System.exit(1);
}
// Display image to screen
add(new JLabel(new ImageIcon(filename)));
myLayout = new FlowLayout(FlowLayout.CENTER, 20, 40);
setLayout(myLayout);
// Buttons for contrast control
increaseContrastButton = new JButton(">>");
decreaseContrastButton = new JButton("<<");
add(increaseContrastButton);
increaseContrastButton.setVisible(true);
add(decreaseContrastButton);
decreaseContrastButton.setVisible(true);
//increaseContrastButton.addActionListener(this);
//decreaseContrastButton.addActionListener(this);
}
/**
* Calculates the current image's CDF.
*
* There are only 256 gray levels in consideration. The image is scanned
* pixel by pixel. For each pixel, its gray level is the average of its rgb.
* This gray level will be used to generate a histogram.
*
* When the histogram is ready,
* CDF[i]=sum(histogram[0]+histogram[1]+...+histogram[i]).
*
* Note this method is also in charge of calculating the following instance variable:
* <ul>
* <li>imageMaxGrayLevel</li>
* <li>imageMinGrayLevel</li>
* <li>minCdfCell</li>
* </ul>
*/
private void findCdf(){
// insert your code
}
/**
* Finds the rgb of a pixel in image.
*
* @param image the source image
* @param x horizontal coordinate
* @param y vertical coordinate
* @param rgb an array where the result is saved
*/
private void getRGB(BufferedImage image, int x, int y, int[] rgb){
int temp=image.getRGB(x,y);
Color color=new Color(temp);
rgb[0]=color.getRed();
rgb[1]=color.getGreen();
rgb[2]=color.getBlue();
}
/**
* Sets the rgb values for a pixel of image
*
* @param image source image
* @param x horizontal coordinate
* @param y vertical coordinate
* @param rgb the rgb values to set
*/
private void setRGB(BufferedImage image, int x, int y, int[] rgb){
Color color=new Color(rgb[0],rgb[1],rgb[2]);
image.setRGB(x, y, color.getRGB());
}
/**
* Inner class that handles the event on increaseContrastButton and
* decreaseContrastButton.
*
* @author Dahai Guo
*
*/
private class ChangeConstrastListener implements ActionListener{
/**
* This variable decides how fast the contrast is changing.
*
* When increasing contrast, the largest/smallest gray level will
* be increased/decreased STEP.
*
* When decreasing contrast, the largest/smallest gray level will
* be decrease/increase STEP.
*/
private int STEP=10;
/**
* Is the method that deals with both increaseContrastButton and
* decreaseContrastButton.
*/
@Override
public void actionPerformed(ActionEvent e){
// insert your code
imagePanel.repaint();
}
/**
* Changes the contrast for each pixel of the image which
* is defined in the outer class.
* @param maxGray
* @param minGray
*/
private void changeConstrast(int maxGray, int minGray){
int width=image.getWidth();
int height=image.getHeight();
int rgb[]=new int[3];
for(int i=0;i<width;i++){
for(int j=0;j<height;j++){
equalize(i,j,maxGray,minGray);
}
}
findCdf();
}
/**
* Follows "Histogram Equalization" on Wikipedia.
*
* Changes the rgb for the pixel at (x,y) in the image.
*
* @param x horizontal coordinate
* @param y vertical coordinate
* @param max the new max gray level
* @param min the new min gray level
*/
private void equalize(int x, int y, int max, int min){
// insert your code here
}
}
public static void main(String args[])
throws IOException{
if(args.length!=1){
System.err.println("Missing the path to an image file");
System.err.println("Command: java HistogramEqualizer image_file");
System.exit(1);
}
HistgramEqualizerApp histApp = new HistgramEqualizerApp(args[0]);
histApp.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
histApp.setSize( 512, 400 ); // set frame size
histApp.setVisible( true ); // display frame
histApp.setResizable(true);
}
}
【问题讨论】:
-
您的代码不符合要求。请发帖MCVE。
-
我添加了完整的代码,对不起!
-
MCVE 中的 M 表示 Minimal,“完整代码”正好相反。为什么我们需要所有功能代码来检查为什么不显示按钮?删除对问题不重要的所有内容(不要只是以使代码无法编译的方式删除它)。
-
好吧,首先,您要添加图像“标签”,然后设置布局。您应该使用边框布局并将按钮添加到面板,然后将所述面板添加到主框架,然后将图像面板添加到中心。
-
@Mr.Polywhirl,您非常乐于助人。我想通了!
标签: java button user-interface layout