Jslider工作正常,但没有出现过滤图像。请帮我查一下这段代码的错误。是否还有其他方法可以改变图像的亮度? 如果是这样,请更改代码,但使用jslider亮度应该增加或减少。
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.imageio.ImageIO;
import java.io.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.RescaleOp;
public class Bright extends JFrame
{
Dimension screenwidth=getToolkit().getScreenSize();
int mx=(int)screenwidth.getWidth();
int my=(int)screenwidth.getHeight();
BufferedImage picture1;
JLabel label3;
int neww;
int newh;
float[] scales = { 0.5f,1f, 1f, 1f };
float[] offsets = new float[4];
RescaleOp rescale;
public Bright()
{
JFrame f = new JFrame("Image Editor v1.0");
f.setLayout(null);
try{
File file=new File("e:\\8.jpg");
picture1=ImageIO.read(file);
}catch(Exception e)
{
}
JPanel panel2 = new JPanel(); // create GUI to load image i.e picture1
panel2.setLayout(null);
panel2.setBounds(101,20,mx-100,my-20);
f.add(panel2);
label3 = new JLabel("");
panel2.add(label3);
f.setExtendedState(Frame.MAXIMIZED_BOTH);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
createimage();
brightness();
}
public void brightness()
{
JSlider slider=new JSlider(0,100);
JPanel sliderpanel = new JPanel();
JFrame frame1 = new JFrame();
frame1.setTitle("Vertical JSlider");
frame1.setContentPane(sliderpanel);
frame1.setVisible(true);
slider.addChangeListener(new ChangeListener()
{
@Override
public void stateChanged(ChangeEvent e) {
// TODO Auto-generated method stub
JSlider slider = (JSlider)e.getSource();
System.out.println("Slider: "+slider.getValue());
float value = (float) slider.getValue()/100f;
scales[3]=value;
rescale = new RescaleOp(scales,offsets, null);
rescale.filter(picture1,picture1); // what problem is here ??
}
});
frame1.add(slider);
frame1.setSize(550,100);
createimage();
}
public void createimage()
{
BufferedImage bi = null;
if(picture1.getWidth()>mx-110 ||picture1.getHeight()>my-30 )
{
neww= (int) Math.round(picture1.getWidth() * 0.25); // reduce size
newh = (int) Math.round(picture1.getHeight() *0.25);
}
else
{
neww=picture1.getWidth();
newh=picture1.getHeight();
}
bi = new BufferedImage(neww,newh,BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = (Graphics2D) bi.createGraphics();
g2d.addRenderingHints(new RenderingHints(RenderingHints.KEY_RENDERING,RenderingHints.VALUE_RENDER_QUALITY));
g2d.drawImage(picture1,0,0,neww,newh,0,0,picture1.getWidth(),picture1.getHeight(), null);
label3.setIcon(new ImageIcon(bi));
label3.setBounds(150,150,neww,newh); //draw image
}
public static void main(String[] args)
{
new Bright();
}
}
答案 0 :(得分:0)
createimage方法创建一个新的BufferedImage,并通过ImageIcon将此图像添加到UI。因此,对picture1变量的更改将不会反映在UI中。此外,如果您确实进行了更改,则可能需要调用UI对象上的重绘(您可能会更好,并且可以更灵活地创建自己的JPanel并使用自定义绘制绘制图像)。该代码段中还有很多其他内容值得一提,但不一定与您的问题直接相关: