2 Buttons in Frame

exit666

Grünschnabel
Ich habe einen Frame der mir ein Bild anzeigt und einen Button um den Kontrast zu erhöhen. Nun wollte ich einen zweiten Button einfügen der den Kontrast verringert. Dies will mir aber infach nicht gelingen. Ich hab schon so viel Versucht, aber es klappt einfach nicht. Vielleicht kann mir wer weiterhelfen. Hier der Code:
Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;


class BufferedImaging3 extends JFrame {
	DisplayPanel displayPanel;
	
	public BufferedImaging3() {
		Container container = getContentPane();
		
		// JPanel-Objekt erzeugen		
		displayPanel = new DisplayPanel();

		//JPanel-Objekt dem Container hinzufügen
		container.add(displayPanel);
		
		Box box2 = Box.createHorizontalBox();
		Box box = Box.createHorizontalBox();
        JButton kontrastButton = new JButton("Kontrast ++");
        JButton kButton = new JButton("Kontrast --");
        kontrastButton.addActionListener(new ButtonListener());
        kButton.addActionListener (new ButtonListener2());
        // Add the button at the center of box using glue components
        box.add(Box.createHorizontalGlue()); 
        box.add(kontrastButton);
        box.add(Box.createHorizontalGlue());
        
        box2.add(Box.createHorizontalGlue());
        box2.add(kButton);
        box2.add(Box.createHorizontalGlue());
		
		
        container.add(box, BorderLayout.SOUTH);
		//container.add(box2, BorderLayout.SOUTH);
		
		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				dispose();
				System.exit(0);
			}
		});		
		
		setSize(450, 400); // Width and height
		show(); // Anzeigen des JFrames
	}

	public static void main(String args[]) {
		System.out.println("Starting FirstExample...");
		new BufferedImaging2();

	}
	
	class ButtonListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
        	
        	displayPanel.incKontrast();
            displayPanel.repaint();
        }
    }
	
	class ButtonListener2 implements ActionListener {
        public void actionPerformed(ActionEvent e) {
        	
        	displayPanel.decKontrast();
            displayPanel.repaint();
        }
    }
}

class DisplayPanel extends JPanel {
	BufferedImage bi1;
    Image image;
    double kontrast = 1.0;
    int helligkeit = 0;
    int value;
    WritableRaster raster;

    // Konstructor
    DisplayPanel() {
        setBackground(Color.white);  
        setSize(450, 400); 

        // Image File laden
        image = getToolkit().getImage("helm.jpg");

        // Mediatracker installieren
        MediaTracker mt = new MediaTracker(this);
        mt.addImage(image, 1);
        try {
            mt.waitForID(1);
        } catch (Exception e) {
            System.out.println("Exception while loading image.");
        }
		
		// Wenn das Image eine unbekannte Breite hat, ist es nicht geladen worden;
        // dann Programm beenden
        if (image.getWidth(this) == -1) {
            System.out.println("*** Make sure you have the image "
                + "(car.jpg) file in the same directory.***");
            System.exit(0);
        }
        
        // Buffered Image anlegen
        bi1 = new BufferedImage(image.getWidth(this),
                                image.getHeight(this),
                                BufferedImage.TYPE_BYTE_GRAY);
        
        Graphics2D big = bi1.createGraphics();
        
        // Draw the image into the buffer
        big.drawImage(image, 0, 0, this);
        
        
		raster = bi1.getRaster();
	

		for (int y = 0; y < image.getHeight(this); ++y){
			for (int x = 0; x < image.getWidth(this); ++x) { 
				value = (int)(raster.getSample(x, y, 0)*kontrast) + helligkeit;
				if(value>255)
					value = 255;
				if(value <= 0)
					value = 0;
				raster.setSample(x, y, 0, value);
			}
		}   
    }

	public void incKontrast(){
		kontrast = kontrast + 0.1;
		
		for (int y = 0; y < image.getHeight(this); ++y){
			for (int x = 0; x < image.getWidth(this); ++x) { 
				value = (int)(raster.getSample(x, y, 0)*kontrast) + helligkeit;
				if(value>255)
					value = 255;
				if(value <= 0)
					value = 0;
				raster.setSample(x, y, 0, value);
			}
		}   
	}
	
	public void decKontrast(){
		kontrast = kontrast -0.1;
		
		for (int y=0; y < image.getHeight(this); ++y){
			for (int x=0; x < image.getWidth(this); ++x){
				value = (int) (raster.getSample(x,y,0)*kontrast) - helligkeit;
				if(value>255)
					value = 255;
				if(value<=0)
					value=0;
				raster.setSample(x,y,0,value);
			}
		}
	}
	
    // die paintComponent Methode
    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        // ein 2d Grafik-Kontext erzeugen
        Graphics2D g2D = (Graphics2D) g;

        // das Image anzeigen
        g2D.drawImage(bi1, 0, 0, this);
        
    }
    
}
 
In jedem Bereich des BorderLayouts hat genau eine Komponente Platz. Willst Du zwei Buttons im "SOUTH"-Bereich anzeigen, mußt Du Dir ein eigenes Panel (z.B. mit FlowLayout) erstellen, dem Du die Buttons hinzufügst und das Du dann statt der Buttons dem "SOUTH"-Bereich hinzufügst.
 
Zurück