0

After writing and modifying this code, I encountered with this problem: Clicking with the mouse is making the object appear for a secong and then fly out diagonally of the image bounds.

apparentally the function "repaint()" is responsible for this occurence in "paint(Graphics g)" block. eliminating the reapaint() part make the object appear for a second and then dissappear.

public class MainWindow extends JFrame implements MouseListener
{
public BufferedImage myImage,packman_icon;
private ArrayList<Point> points;


public MainWindow() 
{
    initGUI();      
    this.addMouseListener(this); 
}

private void initGUI() 
{
    MenuBar menuBar = new MenuBar();
    Menu File = new Menu("File"); 
    Menu Run=new Menu("Run");
    Menu Insert=new Menu("Insert");

    MenuItem New=new MenuItem("New");
    MenuItem Open = new MenuItem("Open");
    MenuItem Save=new MenuItem("Save");
    MenuItem start=new MenuItem("start");
    MenuItem stop=new MenuItem("stop");
    MenuItem packman=new MenuItem("packman");
    MenuItem fruit=new MenuItem("fruit");

    menuBar.add(File);
    menuBar.add(Run);
    menuBar.add(Insert);

    File.add(New);
    File.add(Open);
    File.add(Save);
    Run.add(start);
    Run.add(stop);
    Insert.add(packman);
    Insert.add(fruit);  

    this.setMenuBar(menuBar); 

    try {
         myImage = ImageIO.read(new File("C:\\Users\\Owner\\Desktop\\Matala3\\Ariel1.png"));//change according to your path
         packman_icon=ImageIO.read(new File("C:\\Users\\Owner\\Desktop\\pacman_icon.gif"));
    } catch (IOException e) {
        e.printStackTrace();
    }   

}

int x = -1;
int y = -1;

public void paint(Graphics g)
{
   super.paintComponents(g);
    g.drawImage(myImage, 0, 0, this);

    g.drawImage(packman_icon, x, y, 20, 20, this);

    if(x!=-1 && y!=-1)
    {
        int r = 10;
        x = x - (r / 2);
        y = y - (r / 2);
        g.fillOval(x, y, r, r);

    }

}

@Override
public void mouseClicked(MouseEvent arg) {

    System.out.println("mouse Clicked");
    System.out.println("("+ arg.getX() + "," + arg.getY() +")");    

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

    repaint();

}
}


public class Main 
{
public static void main(String[] args)
{
    MainWindow window = new MainWindow();
    window.setVisible(true);

window.setSize(window.myImage.getWidth(),window.myImage.getHeight());
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

I want the image icon to stay in picture and not dissappear, thus creating multiple objects that appear on the picture.

**I edited the code acoording to instructions: deleted reapint() method from paint and used

super.paintComponet(g) but now it inly appears for a brief second and then dissappears.

quepasa
  • 11
  • 1
  • (a) You shouldn't call `repaint()` inside `paint()`. (b) In Swing, you shouldn't override `paint()` but `paintComponent()` (and remember to call `super.paintComponent()`. – RealSkeptic Dec 24 '18 at 14:30
  • For a start delete repaint() in a paint() method. You have to know that repaint() method in your case acts like a lightweight component, therefore it calls paint() method making it a dead circle with new x1 and y1 all the time. – MS90 Dec 24 '18 at 14:32
  • 1) For better help sooner, [edit] to add a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) One way to get image(s) for an example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). E.G. [This answer](https://stackoverflow.com/a/10862262/418556) hot links to an image embedded in [this question](https://stackoverflow.com/q/10861852/418556). 3) Use a logical and consistent form of indenting code lines and blocks. The indentation is intended to make the flow of the code easier to follow! .. – Andrew Thompson Dec 24 '18 at 23:03
  • .. 4) Please learn common Java nomenclature (naming conventions - e.g. `EachWordUpperCaseClass`, `firstWordLowerCaseMethod()`, `firstWordLowerCaseAttribute` unless it is an `UPPER_CASE_CONSTANT`) and use it consistently. – Andrew Thompson Dec 24 '18 at 23:04

1 Answers1

0

Don't use AWT components in a Swing application. Swing component start with "J" (JMenuBar, JMenu, JMenuItem).

//public void paint(Graphics g)
protected void paintComponent(Graphics g)

The original comment was to override paintCompnent(...).

Also, painting is a dynamic process and the painting methods are invoked whenever Swing determines the components needs to be painted so you need to make sure to reset the state each time the component is painted.

Therefore, a painting method should NOT change the state of the class. You are using the x/y variables for two purposes:

  1. to paint the image
  2. to paint the oval.

Because you update the x/y variable in the painting method, this will affect the location of the image the next time the painting method is invoked.

If you want the image fixed then you need to use separate variables for the location of the image.

You will also need to reset the x/y variables in the painting method to paint the ovals, since these variables are local and need to be reset each time the painting is done.

camickr
  • 321,443
  • 19
  • 166
  • 288