Grafik laden

Hallo!

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);
		}
	}
}
 
Moin Thomas,
tausenddank für dein Beispiel.
Ich habe zumindest den Grund für meinen Fehler entdeckt.

Ich benutze die Methode setXORMode(Color) und diese bewirkt, dass sich die Farben ändern.

Wenn ich allerdings diese Methode entferne, so lassen sich Arrows oder Kästchen nicht mehr richtig zeichnen, da beim Zeichnen jede Bewegung gezeichnet wird und nicht nur die Veänderung...

Womit ich beim nächsten Problem wäre... sorry
 
Hallo!

Wie meinst du das?
Wenn ich allerdings diese Methode entferne, so lassen sich Arrows oder Kästchen nicht mehr richtig zeichnen, da beim Zeichnen jede Bewegung gezeichnet wird und nicht nur die Veänderung...

Wie zeichnest du denn die Arrows und Kästchen... und was meinst du mit
"jede Bewegung gezeichnet wird und nicht nur die Veänderung..." ?

Gruß Tom
 
Also,
ich rufe aus der Klasse wo die Mausevents etc. kontrolliert werden, folgende Methoden auf:

public class ShapesDraw{

private Drawable shape;
private MyGraphicDisplay grBild;
....
public ShapesDraw(MyGraphicDisplay c){
grBild = c;
}
....

public void mouseDragged(MouseEvent e){
if(SwingUtilities.isRightMouseButton(e)) return;
int x = e.getX();
int y = e.getY();
drawGraphics(x,y);
}

protected void drawGraphics(int x, int y) {
shape.draw(grBild.getDisplayedImageGraphic());
grBild.repaint();
}
}
....


// alle shapes wie box, arrow etc. implementieren das Interface Drawable, das unter anderem die Methode draw(Graphic2D c) bereitstellt.

In dieser Methode passierte bislang folgendes:

public void draw(Graphics2D g ){
g.setColor(Color.black);
g.setXORMode(drawingColor);
drawLine(g, old.x, old.y);
drawArrow(st, old, g);
old.x = cur.x; old.y = cur.y;
drawLine(g, cur.x, cur.y);
drawArrow(st, cur, g);
old.x = cur.x; old.y = cur.y;
}

public void drawArrow(Point st, Point end, Graphics2D g){
Point upLine = new Point(0,0);
Point downLine = new Point(0,0);
int direction ;
double theta;
if((end.y -st.y) >= 0) direction = -1;
else direction = 1;

// Rotate the point by 30 deg

double angle = Math.atan((double)(end.x-st.x)/(double)( end.y-st.y));
int len = 10;
Point rel = new Point(len, len);

angle += Math.PI/2;
theta = -(angle + Math.PI/6);
//Rotate the line up

upLine.x =end.x -(int)( len* Math.cos(theta)*direction);
upLine.y =end.y -(int)( len* Math.sin(theta)*direction);
// Rotate the line down

theta = -(angle - Math.PI/6);
downLine.x =end.x -(int)( len* Math.cos(theta)*direction);
downLine.y =end.y -(int)( len* Math.sin(theta)*direction);

// Now the draw the arrow lines
GeneralPath arrowpath = new GeneralPath();
arrowpath.moveTo((float)upLine.x, (float)upLine.y);
arrowpath.lineTo((float)end.x, (float)end.y);
arrowpath.lineTo((float) downLine.x, (float)downLine.y);
arrowpath.closePath();
g.draw(arrowpath);
if(doubleHeaded) {
arrowpath = new GeneralPath();
//theta = -(angle + Math.PI/6);
upLine.x = st.x -(int)( len* Math.cos(theta)*(-direction));
upLine.y = st.y -(int)( len* Math.sin(theta)*(-direction));
// Rotate the line down
theta = -(angle + Math.PI/6);
downLine.x =st.x -(int)( len* Math.cos(theta)*(-direction));
downLine.y =st.y -(int)( len* Math.sin(theta)*(-direction));

arrowpath.moveTo((float)upLine.x, (float)upLine.y);
arrowpath.lineTo((float)st.x, (float)st.y);
arrowpath.lineTo((float) downLine.x, (float)downLine.y);
arrowpath.closePath();
g.draw(arrowpath);
}
}

private void drawLine(Graphics2D g, int endx, int endy) {
Line2D.Float line = new Line2D.Float((float)st.x,
(float)st.y,
(float)endx,
(float)endy);
path = new GeneralPath(line);
g.draw(path);
}


So, wenn ich jetzt die Methode setXORMode entferne lässt sich das Zeichnen nicht mehr richtig durchführen.
Der Arrowkopf wird z.b. andauernd gezeichnet.
Repainte ich das Ganze vielleicht falsch?
 
Hallo!

Meinst du vielleicht damit, dass der Pfeilkopf "schleift" bzw. an jeder Stelle an der er schon mal gezeichnet wurde wieder gezeichnet wird?
Wenn ja, solltest du vielleicht mal ein myPath.reset() nach jedem Zeichnen versuchen ...

Gruß Tom
 
Hallo,

an welcher Stelle würdest du das denn in meinem Code aufrufen? Es will einfach nicht klappen....
 
Moinsen,
also ich weiß echt nicht wie man es machen soll....
Diese eine Zeile mit setXORMode() killt mich.
Wieso kann man nicht einfach diese Methode austauschen gegen eine Methode die einfach in den angegebenen Farben malt und nicht ein Zwischending bildet aus setXORMode-Color und der setColor-Color?
Oder gibt es da was?
Ich brauch echt eure Hilfe,
danke
 

Neue Beiträge

Zurück