Click on the screen and change the values of the variables

0

I need to implement in the code functionalities so that when I click on the screen, I change 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 other 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 wondering how I can change and resolve this.

    
asked by anonymous 30.06.2016 / 15:33

1 answer

2

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

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

The solution would be to transform both ic s into a single instance variable of class Interface and instantiate it only once (that is, not instantiate it within mouseClicked ).

In addition, naming classes like Interface and ClasseInterface inside a package named Interface is a bad idea, these are one of the worst possible names.

Other mouse methods should not throw UnsupportedOperationException - this is a very damaging thing (even if something is automatically generated by IDE). These methods should only do nothing.

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

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

Also, remember to indent the code properly to avoid confusion. The if blocks within the mouseClicked method have multiple rows of your body with an idle level of less.

    
11.11.2016 / 15:11