An application that wants to receive the adjustment event must implement the AdjustListener interface and register the listener with the Scrollbar component by using the addAdjustmentListener(AdjustmentListener listener) method. The AdjustListener has the adjustmentValueChanged(AdjustmentEvent e) method that need to be overridden to trigger an action.
In the example code below, the ScrollBar component is used to allow the user graphically to select a value. This value will affect the brightness of the image. When the program initially runs, and an image is read from the local disk and displays on the Label component. The Label component is used to draw graphics elements. The MediaTracker class tracks the image loading process. When the loading process completes, the image can be drawn on the Label component. To change the brightness of the image, the image has to be converted to BufferedImage object. Then you can use the ConvolveOp class to help you in changing the brightness of the image. The ConvolveOp class has a method call filter(BufferedImage source, BufferedImage des) that can be used to convolve the source image to destination image. The CovolveOp class knows how to convolve the source to destination image by looking at the Kernel object that describes how a specific pixel and its surrounding pixels affect the value computed for the pixel position in the destination image. The Keynel object uses the array elements to describe this information. When the user selects a value from the ScrollBar, one value of the array elements (e) changes so the brightness of the image changed.
import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Label;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.awt.image.ConvolveOp;
import java.awt.image.Kernel;
import java.awt.image.ImageObserver;
import java.awt.MediaTracker;
import java.awt.Toolkit;
import java.awt.Image;
import java.io.IOException;
import java.awt.Scrollbar;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import javax.imageio.ImageIO;
class ImageBox extends Label implements ImageObserver{
Image img;
float e;
ImageBox(){
e=0.0f;
img=Toolkit.getDefaultToolkit().getImage("d:/earth1.jpg");
MediaTracker mt=new MediaTracker(this);
mt.addImage(img,0);
try{
mt.waitForID(0);
}catch(InterruptedException e){}
}
public void paint(Graphics g){
Graphics2D g2d=(Graphics2D)g;
float[] elements = {0.0f, 1.0f, 0.0f, -1.0f,e,1.0f,0.0f,0.0f,0.0f};
Kernel kernel = new Kernel(3, 3, elements);
ConvolveOp cop = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null);
BufferedImage bi=createBufferedImage();
BufferedImage bimg= new BufferedImage(bi.getWidth(),bi.getHeight(),BufferedImage.TYPE_INT_RGB);
cop.filter(bi,bimg);
g2d.translate(300-bi.getWidth()/2,200-bi.getHeight()/2);
g2d.drawImage(bimg,0,0,null);
}
public BufferedImage createBufferedImage(){
BufferedImage dest ;
dest = new BufferedImage(img.getWidth(this),img.getHeight(this), BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = dest.createGraphics();
g2.drawImage(img, 0, 0, null);
g2.dispose();
return dest;
}
public void setElement(int e){
this.e=0.1f*e;
repaint();
}
}
class Display extends Frame{
ImageBox ib;
Display(){
setTitle("AdjustmentListener");
setVisible(true);
setSize(600,500);
setLayout(new BorderLayout());
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
Scrollbar scroll= new Scrollbar(Scrollbar.HORIZONTAL, 0, 0, -10, 10);
scroll.setUnitIncrement(1);
scroll.setBlockIncrement(2);
scroll.addAdjustmentListener(new AdjustList());
add(scroll, BorderLayout.NORTH);
add(scroll,BorderLayout.NORTH);
ib=new ImageBox();
add(ib,BorderLayout.CENTER);
validate();
}
class AdjustList implements AdjustmentListener{
public void adjustmentValueChanged(AdjustmentEvent e){
Scrollbar scr=(Scrollbar)e.getSource();
ib.setElement(scr.getValue());
}
}
}
public class AdjustmentListenerTest {
public static void main(String[] args){
new Display();
}
}
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Label;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.awt.image.ConvolveOp;
import java.awt.image.Kernel;
import java.awt.image.ImageObserver;
import java.awt.MediaTracker;
import java.awt.Toolkit;
import java.awt.Image;
import java.io.IOException;
import java.awt.Scrollbar;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import javax.imageio.ImageIO;
class ImageBox extends Label implements ImageObserver{
Image img;
float e;
ImageBox(){
e=0.0f;
img=Toolkit.getDefaultToolkit().getImage("d:/earth1.jpg");
MediaTracker mt=new MediaTracker(this);
mt.addImage(img,0);
try{
mt.waitForID(0);
}catch(InterruptedException e){}
}
public void paint(Graphics g){
Graphics2D g2d=(Graphics2D)g;
float[] elements = {0.0f, 1.0f, 0.0f, -1.0f,e,1.0f,0.0f,0.0f,0.0f};
Kernel kernel = new Kernel(3, 3, elements);
ConvolveOp cop = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null);
BufferedImage bi=createBufferedImage();
BufferedImage bimg= new BufferedImage(bi.getWidth(),bi.getHeight(),BufferedImage.TYPE_INT_RGB);
cop.filter(bi,bimg);
g2d.translate(300-bi.getWidth()/2,200-bi.getHeight()/2);
g2d.drawImage(bimg,0,0,null);
}
public BufferedImage createBufferedImage(){
BufferedImage dest ;
dest = new BufferedImage(img.getWidth(this),img.getHeight(this), BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = dest.createGraphics();
g2.drawImage(img, 0, 0, null);
g2.dispose();
return dest;
}
public void setElement(int e){
this.e=0.1f*e;
repaint();
}
}
class Display extends Frame{
ImageBox ib;
Display(){
setTitle("AdjustmentListener");
setVisible(true);
setSize(600,500);
setLayout(new BorderLayout());
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
Scrollbar scroll= new Scrollbar(Scrollbar.HORIZONTAL, 0, 0, -10, 10);
scroll.setUnitIncrement(1);
scroll.setBlockIncrement(2);
scroll.addAdjustmentListener(new AdjustList());
add(scroll, BorderLayout.NORTH);
add(scroll,BorderLayout.NORTH);
ib=new ImageBox();
add(ib,BorderLayout.CENTER);
validate();
}
class AdjustList implements AdjustmentListener{
public void adjustmentValueChanged(AdjustmentEvent e){
Scrollbar scr=(Scrollbar)e.getSource();
ib.setElement(scr.getValue());
}
}
}
public class AdjustmentListenerTest {
public static void main(String[] args){
new Display();
}
}
nice article in your blog.thank you for sharing useful info.
ReplyDeletevisit
web programming tutorial
welookups
The listener who is working on here, he needs to be there for working on https://esportbetweb.com/fortnite-betting-sites/. If they can find it too, then it will be easy for all of us for sure.
ReplyDelete