M
MD
Gast
Hallo,
ich habe folgende Hauptklasse:
--- Anfang ---
--- Ende ---
und noch folgende Klasse:
--- Anfang ---
--- Ende ---
Wenn man es kompiliert, sieht man, dass die Koordinaten richtig aktualisiert werden.
Nur das Rechteck wird nicht neu gezeichnet, obwohl ich in der updateLocation-Methode ein repaint() aufrufe.
Kann mir jemand weiterhelfen???
Ich finde den Fehler einfach nicht.
MD
[Edit by foobar: Codetags eingefügt]
ich habe folgende Hauptklasse:
--- Anfang ---
Code:
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;
public class GUITest extends JFrame {
static protected JLabel label;
DPanel d;
public GUITest(){
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {System.exit(0);}
});
getContentPane().setLayout(new BorderLayout());
d = new DPanel();
d.setBackground(Color.white);
getContentPane().add(d);
label = new JLabel("Drag rectangle around within the area");
getContentPane().add("South", label);
}
public static void main(String s[]) {
GUITest f = new GUITest();
f.pack();
f.setSize(new Dimension(550,250));
f.show();
}
}
class DPanel extends JPanel implements MouseListener, MouseMotionListener{
OwnRectangle rect = new OwnRectangle(0, 0, 100, 50);
// Holds the coordinates of the user's last mousePressed event.
static int last_x, last_y;
boolean firstTime = true;
static Rectangle area;
public DPanel(){
setBackground(Color.white);
addMouseMotionListener(this);
addMouseListener(this);
}
// Handles the event of the user pressing down the mouse button.
public void mousePressed(MouseEvent e){
rect.mousePressed(e);
}
// Handles the event of a user dragging the mouse while holding
// down the mouse button.
public void mouseDragged(MouseEvent e){
rect.mouseDragged(e);
}
// Handles the event of a user releasing the mouse button.
public void mouseReleased(MouseEvent e){
rect.mouseReleased(e);
}
// This method is required by MouseListener.
public void mouseMoved(MouseEvent e){}
// These methods are required by MouseMotionListener.
public void mouseClicked(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
// Updates the coordinates representing the location of the current rectangle.
public void updateLocation(MouseEvent e){
rect.updateLocation(e);
repaint();
}
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setStroke(new BasicStroke(8.0f));
Dimension dim = getSize();
int w = (int)dim.getWidth();
int h = (int)dim.getHeight();
if ( firstTime ) {
area = new Rectangle(dim);
rect.setLocation(w/2-50, h/2-25);
//rect.setLocation(100, 100);
firstTime = false;
}
// Clears the rectangle that was previously drawn.
g2.setPaint(Color.white);
g2.fillRect(0, 0, w, h);
// Draws and fills the newly positioned rectangle.
g2.setColor(Color.black);
g2.fill(rect);
g2.draw(rect);
}
}
--- Ende ---
und noch folgende Klasse:
--- Anfang ---
Code:
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
/*
* Created on 24.09.2004
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
/**
* @author
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class OwnRectangle extends Rectangle {
// True if the user pressed, dragged or released the mouse outside of
// the rectangle; false otherwise.
boolean pressOut = false;
public OwnRectangle (int x, int y, int width, int height) {
super(x, y, width, height);
}
public void mousePressed(MouseEvent e){
DPanel.last_x = this.x - e.getX();
DPanel.last_y = this.y - e.getY();
// Checks whether or not the cursor is inside of the rectangle
// while the user is pressing the mouse.
if ( this.contains(e.getX(), e.getY()) ) {
updateLocation(e);
} else {
GUITest.label.setText("First position the cursor on the rectangle and then drag.");
}
}
public void mouseDragged(MouseEvent e){
if ( !pressOut) {
updateLocation(e);
} else {
GUITest.label.setText("First position the cursor on the rectangle and then drag.");
}
}
public void mouseReleased(MouseEvent e) {
// Checks whether or not the cursor is inside of the rectangle
// when the user releases the mouse button.
if ( this.contains(e.getX(), e.getY()) ) {
updateLocation(e);
} else {
GUITest.label.setText("First position the cursor on the rectangle and then drag.");
pressOut = false;
}
}
public void updateLocation(MouseEvent e){
this.setLocation(DPanel.last_x + e.getX(), DPanel.last_y + e.getY());
/*
* Updates the label to reflect the location of the
* current rectangle
* if checkRect returns true; otherwise, returns error message.
*/
if (checkRect()) {
GUITest.label.setText("Rectangle located at " +
this.getX() + ", " +
this.getY());
} else {
GUITest.label.setText("Please don't try to "+
" drag outside the area.");
}
}
boolean checkRect(){
if (DPanel.area == null) {
return false;
}
if(DPanel.area.contains(this.x, this.y, 100, 50)){
return true;
}
int new_x = this.x;
int new_y = this.y;
if((this.x+100)>DPanel.area.getWidth()){
new_x = (int)DPanel.area.getWidth()-99;
}
if(this.x < 0){
new_x = -1;
}
if((this.y+50)>DPanel.area.getHeight()){
new_y = (int)DPanel.area.getHeight()-49;
}
if(this.y < 0){
new_y = -1;
}
this.setLocation(new_x, new_y);
return false;
}
}
Wenn man es kompiliert, sieht man, dass die Koordinaten richtig aktualisiert werden.
Nur das Rechteck wird nicht neu gezeichnet, obwohl ich in der updateLocation-Methode ein repaint() aufrufe.
Kann mir jemand weiterhelfen???
Ich finde den Fehler einfach nicht.
MD
[Edit by foobar: Codetags eingefügt]