gpt4 book ai didi

java - 如何使用鼠标拖动事件在java applet上绘制一个矩形并使其停留

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:43:52 27 4
gpt4 key购买 nike

我有可以绘制矩形的程序。我有两个无法解决的问题。在我绘制矩形后,它不会停留。我拥有的唯一代码清除 Canvas 下的 Canvas ,重画仅在鼠标拖动时调用。为什么当我释放鼠标或移动鼠标时我的 Canvas 会清除。第二件事不是什么大问题,但我无法弄清楚,当我的矩形的高度或宽度为负时,矩形填充为黑色。

package pracpapp2;


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class MouseTracker4July extends JFrame
implements MouseListener, MouseMotionListener {


private static final long serialVersionUID = 1L;
private JLabel mousePosition;
int x, y;
int x1, x2, y1, y2;
int w, h;
private JLabel recStart;
private JLabel recStop;
private JLabel cords;
// set up GUI and register mouse event handlers
public MouseTracker4July()
{
super( "Rectangle Drawer" );

mousePosition = new JLabel();
mousePosition.setHorizontalAlignment(SwingConstants.CENTER);
getContentPane().add( mousePosition, BorderLayout.CENTER );


JLabel text1 = new JLabel();
text1.setText( "At the center the mouse pointer's coordinates will be displayed." );
getContentPane().add( text1, BorderLayout.SOUTH );

recStart = new JLabel();
getContentPane().add(recStart, BorderLayout.WEST);

recStop = new JLabel();
getContentPane().add(recStop, BorderLayout.EAST);

cords = new JLabel();
getContentPane().add(cords, BorderLayout.NORTH);


addMouseListener( this ); // listens for own mouse and
addMouseMotionListener( this ); // mouse-motion events

setSize( 800, 600 );
setVisible( true );
}

// MouseListener event handlers
// handle event when mouse released immediately after press
public void mouseClicked( MouseEvent event )
{
mousePosition.setText( "Clicked at [" + event.getX() +
", " + event.getY() + "]" );
}

// handle event when mouse pressed
public void mousePressed( MouseEvent event )
{

mousePosition.setText( "Pressed at [" +(x1 = event.getX()) +
", " + (y1 = event.getY()) + "]" );

recStart.setText( "Start: [" + x1 +
", " + y1 + "]" );
}

// handle event when mouse released after dragging
public void mouseReleased( MouseEvent event )
{
mousePosition.setText( "Released at [" +(x2 = event.getX()) +
", " + (y2 = event.getY()) + "]" );

recStop.setText( "End: [" + x2 +
", " + y2 + "]" );

}

// handle event when mouse enters area
public void mouseEntered( MouseEvent event )
{
mousePosition.setText( "Mouse entered at [" + event.getX() +
", " + event.getY() + "]" );
}

// handle event when mouse exits area
public void mouseExited( MouseEvent event )
{
mousePosition.setText( "Mouse outside window" );
}

// MouseMotionListener event handlers
// handle event when user drags mouse with button pressed
public void mouseDragged( MouseEvent event )
{
mousePosition.setText( "Dragged at [" + (x = event.getX()) +
", " + (y = event.getY()) + "]" );
// call repaint which calls paint
repaint();

}

// handle event when user moves mouse
public void mouseMoved( MouseEvent event )
{
mousePosition.setText( "Moved at [" + event.getX() +
", " + event.getY() + "]" );
}

public void paint(Graphics g)
{
super.paint(g); // clear the frame surface
g.drawString("Start Rec Here", x1, y1);
g.drawString("End Rec Here", x, y);

w = x1 - x;
h = y1 - y;
w = w * -1;
h = h * -1;

g.drawRect(x1, y1, w, h);

cords.setText( "w = " + w + ", h = " + h);
}

public static void main( String args[] )
{
MouseTracker4July application = new MouseTracker4July();
application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}

} // end class MouseTracker

最佳答案

好吧,在重新阅读你的问题之后,你似乎不太关心有多个矩形:)

这是一个一次只有一个的解决方案(这与您开始时的情况很接近):

import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;

public class MouseTracker4July extends JFrame implements MouseListener, MouseMotionListener {

private static final long serialVersionUID = 1L;
private final JLabel mousePosition;
int x1, x2, y1, y2;
int x, y, w, h;
private final JLabel recStart;
private final JLabel recStop;
private final JLabel cords; // set up GUI and register mouse event handlers
boolean isNewRect = true;

public MouseTracker4July() {
super( "Rectangle Drawer" );

this.mousePosition = new JLabel();
this.mousePosition.setHorizontalAlignment( SwingConstants.CENTER );
getContentPane().add( this.mousePosition, BorderLayout.CENTER );

JLabel text1 = new JLabel();
text1.setText( "At the center the mouse pointer's coordinates will be displayed." );
getContentPane().add( text1, BorderLayout.SOUTH );

this.recStart = new JLabel();
getContentPane().add( this.recStart, BorderLayout.WEST );

this.recStop = new JLabel();
getContentPane().add( this.recStop, BorderLayout.EAST );

this.cords = new JLabel();
getContentPane().add( this.cords, BorderLayout.NORTH );

addMouseListener( this ); // listens for own mouse and
addMouseMotionListener( this ); // mouse-motion events

setSize( 800, 600 );
setVisible( true );

}

// MouseListener event handlers // handle event when mouse released immediately after press
public void mouseClicked( final MouseEvent event ) {
this.mousePosition.setText( "Clicked at [" + event.getX() + ", " + event.getY() + "]" );

repaint();
}

// handle event when mouse pressed
public void mousePressed( final MouseEvent event ) {

this.mousePosition.setText( "Pressed at [" + ( this.x1 = event.getX() ) + ", " + ( this.y1 = event.getY() ) + "]" );

this.recStart.setText( "Start: [" + this.x1 + ", " + this.y1 + "]" );

this.isNewRect = true;

repaint();
}

// handle event when mouse released after dragging
public void mouseReleased( final MouseEvent event ) {
this.mousePosition.setText( "Released at [" + ( this.x2 = event.getX() ) + ", " + ( this.y2 = event.getY() ) + "]" );

this.recStop.setText( "End: [" + this.x2 + ", " + this.y2 + "]" );

repaint();
}

// handle event when mouse enters area
public void mouseEntered( final MouseEvent event ) {
this.mousePosition.setText( "Mouse entered at [" + event.getX() + ", " + event.getY() + "]" );
repaint();
}

// handle event when mouse exits area
public void mouseExited( final MouseEvent event ) {
this.mousePosition.setText( "Mouse outside window" );
repaint();
}

// MouseMotionListener event handlers // handle event when user drags mouse with button pressed
public void mouseDragged( final MouseEvent event ) {
this.mousePosition.setText( "Dragged at [" + ( this.x2 = event.getX() ) + ", " + ( this.y2 = event.getY() ) + "]" ); // call repaint which calls paint repaint();

this.isNewRect = false;

repaint();
}

// handle event when user moves mouse
public void mouseMoved( final MouseEvent event ) {
this.mousePosition.setText( "Moved at [" + event.getX() + ", " + event.getY() + "]" );
repaint();
}

@Override
public void paint( final Graphics g ) {
super.paint( g ); // clear the frame surface
g.drawString( "Start Rec Here", this.x1, this.y1 );
g.drawString( "End Rec Here", this.x2, this.y2 );

int width = this.x1 - this.x2;
int height = this.y1 - this.y2;

this.w = Math.abs( width );
this.h = Math.abs( height );
this.x = width < 0 ? this.x1
: this.x2;
this.y = height < 0 ? this.y1
: this.y2;

if ( !this.isNewRect ) {
g.drawRect( this.x, this.y, this.w, this.h );
}

this.cords.setText( "w = " + this.w + ", h = " + this.h );

}

public static void main( final String args[] ) {
MouseTracker4July application = new MouseTracker4July();
application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}

} // end class MouseTracker

关于java - 如何使用鼠标拖动事件在java applet上绘制一个矩形并使其停留,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1115359/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com