JFrame retain() problem – Java

I want to be able to draw using java paint () on JFrame When I click on the JFrame (anywhere now), I want to redraw the JFrame with the coordinates I clicked - similar to this Java applet http://www.realapplets.com/tutorial/MouseClickExample.html

I am working:

>Initially draw everything and display the JFrame correctly

inoperation:

>Even if repaint() is declared, the JFrame will not be redrawn and updated

This is my code - please use it as strictly as possible - I want to improve my java programming skills (if you have time) and point out every aspect I can improve

Any help will be greatly appreciated

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

class AreaForText extends JPanel implements MouseListener {

int xpos; 
int ypos;

JFrame myJFrame = new JFrame();

public void setJFrame() {

    myJFrame.setSize(300,150);
    myJFrame.setTitle("Bigger Text!");
    myJFrame.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);
    myJFrame.setVisible(true);
    myJFrame.getContentPane().add(new AreaForText());
    myJFrame.addMouseListener(new AreaForText());

}

public void mouseClicked(MouseEvent me) {
    //Save the coordinates of the click lke this. 
    xpos = MouseInfo.getPointerInfo().getLocation().x; 
    ypos = MouseInfo.getPointerInfo().getLocation().y;
    System.out.print("Click" + "  x: " + xpos + "  y: " + ypos);
    myJFrame.invalidate();
    repaint();
    revalidate();
}


public void mouseEntered(MouseEvent e){
}

public void mouseReleased(MouseEvent e) { 
}

public void mousePressed(MouseEvent e) {
}

public void mouseExited(MouseEvent e) { 
}

public void paint(Graphics g) {

    System.out.print("hello");
    g.drawString("Hello World",30,80);
    g.fillRect(20,20,20);        
    g.drawString("("+xpos+","+ypos+")",xpos,ypos);

    }
}

class EnlargeText {

    public static void main(String args[]) {

       AreaForText test = new AreaForText();

       test.setJFrame();

    }

 }

Solution

You are creating 2 areafortext instances, which is not what you want to do One is added to the JFrame and one to the listener Therefore, the object that actually gets the mouse event and is calling redraw is different from the object being displayed

Some of your code organization is not the best You have a JPanel subclass that builds its own JFrame and puts itself into the panel If you really need it, you should pass it in JFrame I made some changes below

Edit I fixed some mouse listeners. You got the wrong X / Y coordinates. Moreover, you should add the listener directly to the panel instead of JFrame, so you don't have to translate the coordinates

Edit I changed the paint method to paintcomponent, which is the preferred method to override here For more information, see swing paint tutorial

import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.MouseInfo;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JFrame;
import javax.swing.JPanel;

class AreaForText extends JPanel implements MouseListener {

    private int xpos;
    private int ypos;


    public AreaForText() {
        super();
        this.addMouseListener(this);
    }

    public void mouseClicked(MouseEvent me) {
        // Save the coordinates of the click lke this.
        xpos = me.getX();
        ypos = me.getY();
        System.out.print("Click" + "  x: " + xpos + "  y: " + ypos);
        repaint();
    }

    public void mouseEntered(MouseEvent e) {
    }

    public void mouseReleased(MouseEvent e) {
    }

    public void mousePressed(MouseEvent e) {
    }

    public void mouseExited(MouseEvent e) {
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        System.out.print("hello");
        g.drawString("Hello World",80);
        g.fillRect(20,20);
        g.drawString("(" + xpos + "," + ypos + ")",ypos);

    }
}

class EnlargeText {

    public static void main(String args[]) {

        EventQueue.invokelater(new Runnable() {
            public void run() {
                JFrame myJFrame = new JFrame("Bigger Text!");
                myJFrame.setSize(300,150);
                myJFrame.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);
                myJFrame.getContentPane().add(new AreaForText());
                myJFrame.setVisible(true);
            }
        });
    }

}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>