Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

slider to modify brightness

Status
Not open for further replies.
Anyone can help me? Perhaps, if the slider is all the way to the left, the image should be black, and if it's all the way to the left, the image should look normal. I suppose to do this, the slider value should go from 0 to 1. Then, each time the slider moves, I create a new picutre by multiplying each pixel in the source image by the value of the slider.

Does that make good sense? Can anyone help me get started on coding this?

Here's the code for the applet above:

grad.java


import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Rectangle;

import javax.swing.JApplet;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.SwingConstants;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class grad extends JApplet implements ChangeListener {
JSlider[] jss;
JPanel mainP, slidP;
display disp;
Color col1, col2;

public void init() {
mainP = new JPanel(new GridLayout(1, 2));
slidP = new JPanel(new GridLayout(6, 2));
jss = new JSlider[6];
for (int loop = 0; loop < 6; loop++) {
jss[loop] = new JSlider(SwingConstants.HORIZONTAL, 0, 255, 0);
jss[loop].addChangeListener(this);
}
slidP.add(new JLabel("Red 1"), slidP.add(jss[0]));
slidP.add(new JLabel("Green 1"), slidP.add(jss[1]));
slidP.add(new JLabel("Blue 1"), slidP.add(jss[2]));
slidP.add(new JLabel("Red 2"), slidP.add(jss[3]));
slidP.add(new JLabel("Green 2"), slidP.add(jss[4]));
slidP.add(new JLabel("Blue 2"), slidP.add(jss[5]));
//add others in order r1, g1, b1, r2, g2, b2;
disp = new display();
mainP.add(disp);
mainP.add(slidP);
getContentPane().add(mainP);
}

public void stateChanged(ChangeEvent evt) {
col1 = new Color(jss[0].getValue(), jss[1].getValue(), jss[2].getValue());
col2 = new Color(jss[3].getValue(), jss[4].getValue(), jss[5].getValue());
disp.setColors(col1, col2);
System.out.println("grad.stateChanged()");
disp.repaint();
}

class display extends JComponent {
Color col1, col2;

public display() {
col1 = Color.black;
col2 = Color.black;
}

public void setColors(Color col1, Color col2) {
this.col1 = col1;
this.col2 = col2;
}

public void paint(Graphics _g) {
Graphics2D g = (Graphics2D) _g;
int boxW = 300;
int boxY = 300;
g.setPaint(new GradientPaint(0, 0, col1, boxW, boxY, col2));
g.fill(new Rectangle(0, 0, boxW, boxY));
}
}
}






myPaint.java




import java.awt.Color;
import java.awt.Paint;
import java.awt.PaintContext;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
import java.awt.image.ColorModel;
import java.awt.image.Raster;
import java.awt.image.WritableRaster;
public class myPaint extends Object implements Paint {
private Color c1, c2;

public myPaint(Color c1, Color c2) {
this.c1 = c1;
this.c2 = c2;
}

public int getTransparency() {
return OPAQUE;//this constant is in Paint class;
}

public PaintContext createContext(ColorModel cm,
Rectangle deviceBounds, Rectangle2D userBounds,
AffineTransform xform, RenderingHints hints) {
return new myPaintContext(c1, c2);
}

class myPaintContext implements PaintContext {
private int r1, g1, b1, r2, g2, b2;

myPaintContext(Color c1, Color c2) {
r1 = (c1.getRGB() >> 16) & 0xff;
g1 = (c1.getRGB() >> 8) & 0xff;
r1 = c1.getRGB() & 0xff;
r2 = (c2.getRGB() >> 16) & 0xff;
g2 = (c2.getRGB() >> 8) & 0xff;
b2 = c2.getRGB() & 0xff;
}

public ColorModel getColorModel() {
return ColorModel.getRGBdefault();
}

public Raster getRaster(int x, int y, int w, int h) {
WritableRaster raster = getColorModel().createCompatibleWritableRaster(w,
h);
int[] data = new int[w * h * 4];
int base = 0;
float rat1, rat2;
for (int xloop = 0; xloop < w; xloop++) {
rat1 = ((float) xloop) / ((float) w);//this is ratio of color1
rat2 = 1 - rat1;//this is ratio of color2;
for (int yloop = 0; yloop < h; yloop++) {
data[base++] = (int) ((r1 * rat1) + (r2 * rat2));
data[base++] = (int) ((g1 * rat1) + (g2 * rat2));
data[base++] = (int) ((b1 * rat1) + (b2 * rat2));
data[base++] = 255;//alpha
}
}
raster.setPixels(0, 0, w, h, data);
return raster;
}

public void dispose() {}
}
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top