Click on the screen and change the values of the variables

Asked

Viewed 89 times

0

I need to implement in the code functionalities so that when I click on the screen, changes the values of some variables that are in the code. However, when I click it stays the same.

My main class:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Interface;

import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JFrame;

/**
 *
 * @author Pessoal
 */
public class Interface extends JFrame implements MouseListener{
    static Point p = new Point();
    public static void main(String[] args) {
        JFrame j = new JFrame("Interface");
        InterfaceClasse ic = new InterfaceClasse();
        j.add(ic);
        j.setSize(1300, 768);
        j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        j.setVisible(true);
        j.setResizable(false);

        ic.setTemperatura(30.0f);
        ic.setTensao(24.0f);
        ic.setVelocidade(10.0f);

    }
    @Override
    public void mouseClicked(MouseEvent e) {
        InterfaceClasse ic = new InterfaceClasse();
        p=getMousePosition();
        if(p.x>=50 && p.x <=350 && p.y>=284 && p.y<=484){
        ic.setTensao(20.0f);
        ic.setTemperatura(25.5f);
        ic.setVelocidade(3.5f);
            System.out.println("Deu certo");
        }
        if(p.x>=950 && p.x <=1250 && p.y>=284 && p.y<=484){
        ic.setTensao(27.0f);
        ic.setTemperatura(33.5f);
        ic.setVelocidade(14.0f);
            System.out.println("Deu certo");
        }
        if(p.x>=500 && p.x <=800 && p.y>=50 && p.y<=250){
        ic.setTensao(24.0f);
        ic.setTemperatura(35.5f);
        ic.setVelocidade(10.0f);
            System.out.println("Deu certo");
        }
        if(p.x>=500 && p.x <=800 && p.y>=518 && p.y<=718){
        ic.setTensao(17.0f);
        ic.setTemperatura(17.3f);
        ic.setVelocidade(2.0f);
            System.out.println("Deu certo");
        }
    }

    @Override
    public void mousePressed(MouseEvent e) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void mouseEntered(MouseEvent e) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void mouseExited(MouseEvent e) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
}

My another class:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Interface;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 *
 * @author Pessoal
 */
public class InterfaceClasse extends JPanel{
    private float temperatura, velocidade, tensao;
    JLabel j;

    public InterfaceClasse(){
        int width, height;
        j = new JLabel("Temperatura");
        this.setSize(1200, 800);
    }
    @Override
    public void paintComponent(Graphics g){

        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        g2d.setColor(new Color(43,132,156));
        g2d.setFont(new Font("Serif", Font.BOLD, 20));
        g2d.drawString("Velocidade: ", 30, 20);
        String vel = Float.toString(velocidade);
        g2d.drawString(vel, "Velocidade: ".length()*10 + 15, 20);
        g2d.drawString("Temperatura: ", 30, 60);
        String temp = Float.toString(temperatura);
        g2d.drawString(temp, "Temperatuda: ".length() *11+10, 60);
        g2d.drawString("Tensão de entrada: ", 30, 100);
        String tens = Float.toString(tensao);
        g2d.drawString(tens, "Tensao de entrada: ".length() *10+10, 100);
    }

    public float getTemperatura() {
        return temperatura;
    }

    public void setTemperatura(float temperatura) {
        this.temperatura = temperatura;
    }

    public float getVelocidade() {
        return velocidade;
    }

    public void setVelocidade(float velocidade) {
        this.velocidade = velocidade;
    }

    public float getTensao() {
        return tensao;
    }

    public void setTensao(float tensao) {
        this.tensao = tensao;
    }

}

I’m having doubts about how I can change and fix this.

  • Add the code directly to the question, some may not have access to the links.

1 answer

2

This question is old, abandoned and poorly formulated, but it is still responsive. The problem is that basically there are two variables called ic.

The ic from within the main is what is rendered on JFrame. In the method mouseClicked, another ic is created, altered and nothing is done with it, leaving it with the garbage collector and leaving the ic unchanged original.

The solution would be to transform both ics in a single class instance variable Interface and instate it only once (i.e., do not instancite it within the mouseClicked).

Also, give class names like Interface and ClasseInterface within a package called Interface is a bad idea, these are one of the worst names possible.

Other mouse methods should not launch UnsupportedOperationException - this is something very harmful (even if it is automatically generated by the IDE). These methods should just do nothing.

The variable Point does not need to be static. And not instance. It should be a local variable to the method mouseClicked.

Regarding the implementation of main, I cannot but refer to this question - Do not manipulate Swing components within the main thread of the program.

Also, remember to ident the code properly to avoid confusion. The blocks if within the method mouseClicked have several lines of your body with a level of identation less.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.