Thomas Darimont
Erfahrenes Mitglied
Hallo!
Komplett neu machen ging schneller als nach dem Fehler zu suchen ...
hab im Moment wenig Zeit... Gruß Tom
Komplett neu machen ging schneller als nach dem Fehler zu suchen ...
hab im Moment wenig Zeit... Gruß Tom
Code:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.image.BufferedImage;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class ImagePainter extends JFrame {
private ImagePanel panel;
private JButton btnColor;
private JButton btnClear;
public ImagePainter() {
super("ImagePainter");
setDefaultCloseOperation(EXIT_ON_CLOSE);
panel = new ImagePanel(new ImageIcon("c:/Beispiel.jpg"));
getContentPane().add(panel, BorderLayout.NORTH);
ActionListener listener = new ActionListenerImpl();
btnColor = new JButton("Choose Color");
btnColor.addActionListener(listener);
getContentPane().add(btnColor, BorderLayout.CENTER);
btnClear = new JButton("Clear");
btnClear.addActionListener(listener);
getContentPane().add(btnClear, BorderLayout.SOUTH);
pack();
setVisible(true);
}
public static void main(String[] args) {
new ImagePainter();
}
class ActionListenerImpl implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btnColor) {
panel.selectedColor = JColorChooser.showDialog(btnColor,
"Farbwahl", Color.BLACK);
} else if (e.getSource() == btnClear) {
System.out.println("btnClear");
panel.clear();
}
}
}
class ImagePanel extends JPanel {
private BufferedImage img;
private ImageIcon oldImage;
private Color selectedColor = Color.BLACK;
private Graphics2D g2d;
public ImagePanel(ImageIcon imgIcon) {
this.oldImage = imgIcon;
int w = imgIcon.getIconWidth();
int h = imgIcon.getIconHeight();
this.img = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
g2d = (Graphics2D) this.img.getGraphics();
g2d.drawImage(imgIcon.getImage(), 0, 0, this);
this.setPreferredSize(new Dimension(w, h));
this.addMouseMotionListener(new MouseMotionAdapter() {
public void mouseMoved(MouseEvent e) {
int x = e.getX();
int y = e.getY();
g2d.setColor(selectedColor);
repaint();
g2d.fillOval(x, y, 3, 3);
}
});
}
protected void clear() {
img.getGraphics().drawImage(oldImage.getImage(), 0, 0, this);
this.repaint();
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(img, 0, 0, this);
}
}
}