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.
– user28595