Can someone explain why i have to drag my mouse on one window and the shape to be printed on another


Joined
Feb 8, 2022
Messages
4
Reaction score
0
I have this code and i can`t understand why I have to drag my mouse on one window (with name Test) and the shape to be printed on another (with no name), instead to drag my mouse on window with name "Test" and the shape to be printed on that window while the other window is useless.
Java:
import java.awt.Frame;

import java.awt.Graphics;

import java.awt.event.MouseEvent;

import java.awt.event.MouseListener;

import java.awt.event.MouseMotionListener;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

import java.awt.event.MouseAdapter;

public class ME2 extends Frame{

int x,y,x1,y1;

        public ME2(){

                setSize(400, 400);

                Frame f=new Frame("Test");

                f.setSize(300,200);

                f.setVisible(true);

                f.addWindowListener(new WindowAdapter () {

                    public void windowClosing(WindowEvent e) {

                            System.exit(0);

                    }

            });  

                addWindowListener(new WindowAdapter () {

                        public void windowClosing(WindowEvent e) {

                                System.exit(0);

                        }

                });

               

                f.addMouseMotionListener( new   MouseMotionListener   (){


                        @Override

                        public void mouseDragged(MouseEvent e) {

                                x1=e.getX(); y1=e.getY(); repaint();

                        }

                        @Override

                        public void mouseMoved(MouseEvent e) {        }              

                } );

                f.addMouseListener( new  MouseAdapter ()  {

                        public void mousePressed(MouseEvent e) {

                                x=e.getX(); y=e.getY();      

                        }      

                } );

                setVisible(true);

        }

        public static void main(String[] args) {

                new ME2();

        }

       

public void paint(Graphics g){

    g.drawRect(x,y,x1,y1);

}



class C implements  MouseListener,MouseMotionListener {


        @Override

        public void mouseClicked(MouseEvent e) {        }

        @Override

        public void mouseEntered(MouseEvent e) {        }

        @Override

        public void mouseExited(MouseEvent e) {        }

        @Override

        public void mousePressed(MouseEvent e) {

                x=e.getX(); y=e.getY();      

        }

        @Override

        public void mouseReleased(MouseEvent e) {

                x1=e.getX(); y1=e.getY(); repaint();

        }//MouseMotionListener{


        @Override

        public void mouseDragged(MouseEvent e) {        }


        @Override

        public void mouseMoved(MouseEvent e) {

        System.out.println("x= "+e.getX()+" y= "+e.getY());      

                x=e.getX(); y=e.getY(); s=x+" "+y; repaint();

        }        

   }

}
 
Ad

Advertisements

Joined
Mar 3, 2021
Messages
243
Reaction score
29
The frames don't seem to be the problem here, it's the arguments to the drawRect function. You don't give it two corners, you give it the top left corner and the height and width. I removed the Frame f and fixed that call and it seems to work. And I had to comment out a line near the bottom.
Java:
import java.awt.ame;

import java.awt.Graphics;

import java.awt.event.MouseEvent;

import java.awt.event.MouseListener;

import java.awt.event.MouseMotionListener;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

import java.awt.event.MouseAdapter;

public class ME2 extends ame{

int x,y,x1,y1;

        public ME2(){

                setSize(400, 400);

                setSize(300,200);

                setVisible(true);

                addWindowListener(new WindowAdapter () {

                    public void windowClosing(WindowEvent e) {

                            System.exit(0);

                    }

            }); 

                addWindowListener(new WindowAdapter () {

                        public void windowClosing(WindowEvent e) {

                                System.exit(0);

                        }

                });

              

                addMouseMotionListener( new   MouseMotionListener   (){


                        @Override

                        public void mouseDragged(MouseEvent e) {

                                x1=e.getX(); y1=e.getY(); repaint();

                        }

                        @Override

                        public void mouseMoved(MouseEvent e) {        }             

                } );

                addMouseListener( new  MouseAdapter ()  {

                        public void mousePressed(MouseEvent e) {

                                x=e.getX(); y=e.getY();     

                        }     

                } );

                setVisible(true);

        }

        public static void main(String[] args) {

                new ME2();

        }

      

public void paint(Graphics g){

    g.drawRect(x,y,x1-x,y1-y);

}



class C implements  MouseListener,MouseMotionListener {


        @Override

        public void mouseClicked(MouseEvent e) {        }

        @Override

        public void mouseEntered(MouseEvent e) {        }

        @Override

        public void mouseExited(MouseEvent e) {        }

        @Override

        public void mousePressed(MouseEvent e) {

                x=e.getX(); y=e.getY();     

        }

        @Override

        public void mouseReleased(MouseEvent e) {

                x1=e.getX(); y1=e.getY(); repaint();

        }//MouseMotionListener{


        @Override

        public void mouseDragged(MouseEvent e) {        }


        @Override

        public void mouseMoved(MouseEvent e) {

        System.out.println("x= "+e.getX()+" y= "+e.getY());     

               // x=e.getX(); y=e.getY(); s=x+" "+y; repaint();

        }       

   }

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top