Jak ustawić suwak żeby wykonywał akcję?

0

Witam, mam za zadanie stworzyć edytor grafiki rastrowej z podstawowymi efektami, typu czarno-biały, sepia itd. Akurat to już mam, tylko nie mam pojęcia jak za pomocą suwaka regulować te właśnie efekty. Kod:

/*
 * NewJFrame.java
 *
 * Created on 27 listopad 2013, 11:56
 */

package javaapplication25;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.JPanel;

/**
 *
 * @author  student
 */
public class NewJFrame extends javax.swing.JFrame {
    public BufferedImage canvas;
   public class kartka extends JPanel{



public kartka(int x, int y)
{
canvas= new BufferedImage(x,y,BufferedImage.TYPE_INT_ARGB);
}

@Override
       protected void paintComponent(Graphics g)
        {           
            super.paintComponent(g);
            g.drawImage(canvas, 0, 0, canvas.getWidth(),canvas.getHeight(), null);
}
}
    
    public NewJFrame() {
 initComponents();
kartka k= new kartka(800,800);
k.setSize(canvas.getWidth(),canvas.getHeight());
try{
canvas=ImageIO.read(new File("C:\\Users\\pc\\Desktop\\ciastko.jpg"));
} catch (Exception e){}
this.add(k);
this.repaint();

    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jSlider1 = new javax.swing.JSlider();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenuItem2 = new javax.swing.JMenuItem();
        jMenuItem3 = new javax.swing.JMenuItem();
        jMenuItem4 = new javax.swing.JMenuItem();
        jMenuItem5 = new javax.swing.JMenuItem();
        jMenuItem6 = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setMinimumSize(new java.awt.Dimension(1200, 1200));

        jMenu1.setText("File");

        jMenuItem1.setText("Czarno biały");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem1);

        jMenuItem2.setText("Sepia");
        jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem2ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem2);

        jMenuItem3.setText("R");
        jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem3ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem3);

        jMenuItem4.setText("G");
        jMenuItem4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem4ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem4);

        jMenuItem5.setText("B");
        jMenuItem5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem5ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem5);

        jMenuItem6.setText("Kontrast");
        jMenuItem6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem6ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem6);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("Edit");
        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jSlider1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(200, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jSlider1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(256, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {                                           
        for(int i=0; i<canvas.getWidth(null);i++)
	for(int j=0; j<canvas.getHeight(null);j++)
{
int R,B,G;
R=canvas.getRGB(i,j);
R=R>>16;
R=R&0xff;
G=canvas.getRGB(i,j);
G=G>>8;
G=G&0xff;
B=canvas.getRGB(i,j);
B=B&0xff;

if((R+G+B)/3>100) canvas.setRGB(i,j,0xFFFFFFFF); else
canvas.setRGB(i,j,0x00000000);} this.repaint();
    }                                          

    private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {                                           
       for(int i=0; i<canvas.getWidth(null);i++)
	for(int j=0; j<canvas.getHeight(null);j++)
{
int R,B,G;
R=canvas.getRGB(i,j);
R=R>>16;
R=R&0xff;
G=canvas.getRGB(i,j);
G=G>>8;
G=G&0xff;
B=canvas.getRGB(i,j);
B=B&0xff;
int gr=(R+G+B) /3;
gr=gr| gr<<8 | gr<<16;
canvas.setRGB(i,j,gr);
        } this.repaint();
    }                                          

    private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {                                           
      for(int i=0; i<canvas.getWidth(null);i++)
	for(int j=0; j<canvas.getHeight(null);j++)
{
int R,B,G;
R=canvas.getRGB(i,j);
R=R>>16;
R=R&0xff;
G=canvas.getRGB(i,j);
G=G>>8;
G=G&0xff;
B=canvas.getRGB(i,j);
B=B&0xff;

canvas.setRGB(i,j,R<<16);
        } this.repaint();
    
    }                                          

    private void jMenuItem4ActionPerformed(java.awt.event.ActionEvent evt) {                                           
         for(int i=0; i<canvas.getWidth(null);i++)
	for(int j=0; j<canvas.getHeight(null);j++)
{
int R,B,G;
R=canvas.getRGB(i,j);
R=R>>16;
R=R&0xff;
G=canvas.getRGB(i,j);
G=G>>8;
G=G&0xff;
B=canvas.getRGB(i,j);
B=B&0xff;

canvas.setRGB(i,j,G<<8);
        } this.repaint();
    
    }                                          

    private void jMenuItem5ActionPerformed(java.awt.event.ActionEvent evt) {                                           
            for(int i=0; i<canvas.getWidth(null);i++)
	for(int j=0; j<canvas.getHeight(null);j++)
{
int R,B,G;
R=canvas.getRGB(i,j);
R=R>>16;
R=R&0xff;
G=canvas.getRGB(i,j);
G=G>>8;
G=G&0xff;
B=canvas.getRGB(i,j);
B=B&0xff;

canvas.setRGB(i,j,B);
        } this.repaint();
    
    }                                          

    private void jMenuItem6ActionPerformed(java.awt.event.ActionEvent evt) {                                           
       for(int i=0; i<canvas.getWidth(null);i++)
	for(int j=0; j<canvas.getHeight(null);j++)
{
int R,B,G;
R=canvas.getRGB(i,j);
R=R>>16;
R=R&0xff;
G=canvas.getRGB(i,j);
G=G>>8;
G=G&0xff;
B=canvas.getRGB(i,j);
B=B&0xff;

R=R&0xff; G=G&0xff; B=B&0xff; 
if (R>245) R=245; if(G>245) G=245; if(B>245) B=245;
R=R+10; B=B+10; G=G+10;
R=R<<16;
G=G<<8;
int rgb=0;
rgb=R|G|B;
canvas.setRGB(i,j,rgb);
}this.repaint();
    }                                          
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new NewJFrame().setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuItem jMenuItem3;
    private javax.swing.JMenuItem jMenuItem4;
    private javax.swing.JMenuItem jMenuItem5;
    private javax.swing.JMenuItem jMenuItem6;
    private javax.swing.JSlider jSlider1;
    // End of variables declaration
    
}
0

Musisz dodać do JSlidera ChangeListenera i napisać w nim metodę stateChanged.

1 użytkowników online, w tym zalogowanych: 0, gości: 1