Hasan Moh
Hasan Moh

Reputation: 19

mouse location and graphics painting

this program is suppose to shift the two rectangles by 10 points to the right each time I click on the screen at the x coordinate 300 or greater, but it doesn't, whats the problem?

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * To change this template use File | Settings | File Templates.
 */
public class MasterMind extends JComponent implements ActionListener,MouseListener {
    //private MouseEvent me;
    private int screenX=0;
    private int screenY=0;
    private ActionEvent e;
    private int xX=10;

    public MasterMind() throws  IOException {
    }

    public static void main(String [] args) throws IOException {
        JFrame window = new JFrame("Master Mind");
        MasterMind game= new MasterMind();
        window.add(game);
        window.pack();
        window.setLocationRelativeTo(null);
        window.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        window.setVisible(true);
        Timer t =   new Timer(30, game);
        t.start();
        window.addMouseListener(game);
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(800,600);
    }

    @Override
    protected void paintComponent(Graphics g) {
        g.setColor(Color.red);
        g.drawRect(xX,30,200,200);
        g.setColor(Color.red);
        g.drawString("x,y coordinates: "+screenX+" , "+screenY,400,100);

        g.setColor(Color.blue);
        g.drawRect(xX+3, 33, 194, 194);
    }

    @Override
    public void mousePressed(MouseEvent me) {    
    }
    @Override
    public void mouseReleased(MouseEvent me) {
        repaint();
    }
    @Override
    public void mouseEntered(MouseEvent me) {
    }
    @Override
    public void mouseExited(MouseEvent me) {
    }

    @Override
    public void mouseClicked(MouseEvent e) {
            MouseEvent mouseIvent = (MouseEvent) e;
            int screenX = mouseIvent.getX();
            int screenY = mouseIvent.getY();
            System.out.println("screen(X,Y) = " + screenX + "\t" + screenY);
            repaint();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        //To change body of implemented methods use File | Settings | File Templates.
        if (screenX>300)  {
        xX=xX+10;  }
        //timer animation

        repaint();
    }
}

I am very new to Java. so please answer in details if I may ask. thank you all.

Upvotes: 1

Views: 551

Answers (2)

Reimeus
Reimeus

Reputation: 159754

These lines should be in the mouseClicked method rather than in actionPerformed.

if (screenX > 300) {
    xX = xX + 10;
}

immediately before the existing repaint method there. They ensure that X coordinate variable xX is updated for use later in the paintComponent method.

Unrelated but make sure to invoke super.paintComponent(g) as the first statement of paintComponent

Upvotes: 2

John Snow
John Snow

Reputation: 1007

Put this ,In your MouseClicked or MousePressed or MouseReleased Method

if(me.getX()>300)
            xX=xX+10;
repaint();

Upvotes: 1

Related Questions