2
I wonder how to change the color of a drawRectangle()
of Graphics2d
java, out of the way @Override paintComponent()
.
It turns out that this change should be temporary, only when the mouse pointer passes over the drawn area and when it leaves that area, it should return to the default color of the drawing. In the algorithm I used, I can’t change the color of the rectangle the way I described it. If you wish to attempt to recompile my code, it may be easier to understand the problem.
package desenhoteste;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
public class DesenhoTeste extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
private JPanel contentPane;
protected int xi;
protected int xf;
protected int yi;
protected int yf;
public Graphics2D g1;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
DesenhoTeste frame = new DesenhoTeste();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public DesenhoTeste() {
setTitle("Desenho Teste");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JPanel areaDeEdicao = new AlteraGrafico();
contentPane.add(areaDeEdicao, BorderLayout.CENTER);
areaDeEdicao.setLayout(null);
addMouseMotionListener(new MouseMotionListener() {
// MouseMotionListener
@Override
public void mouseMoved(MouseEvent e) {
// Essas coordenadas, que são as mesmas do metodo drawRectangle, servem para simular uma área sensível em areaDeEdicao
xi = 150;
xf = 250;
yi = 150;
yf = 250;
// Esse teste verifica se os valores de x e y, captados do mouse, estão dentro das coordenadas do retângulo, ou seja,
// se e.getX() E e.getY() estão entre xi e xy; que equivale a: xi <= e.getX() <= xf ao mesmo tempo que yi <= e.getY() <= yf.
while ((xi <= e.getX() && e.getX() <= xf) && (yi <= e.getY() && e.getY() <= yf)) {
// Uma tentativa de mudar o valor do campo g1 fora do metodo override paintComponent.
g1.setColor(Color.RED);
areaDeEdicao.repaint();
// System.out.println verifica se o while está funcionando, monitorando os valores de e.getX() E e.getY().
System.out.println("Mouse passou dentro do retângulo: " + "e.getXd(): " + e.getX() + " " + "e.getY(): " + e.getY());
break;
}
}
@Override
public void mouseDragged(MouseEvent e) {
// TODO Auto-generated method stub
}
});
}
public class AlteraGrafico extends JPanel {
/**
*
*/
private static final long serialVersionUID = 1L;
// Override gerado pelo atalho ctrl + space e clicando em paintComponent
@Override
protected void paintComponent(Graphics g) {
// TODO Auto-generated method stub
super.paintComponent(g);
g1 = (Graphics2D) g;
g1.drawRect(150, 150, 100, 100);
}
}
}
diegofm, thanks for your help!
– Phrank
@Did Phrank work? If yes, you can accept the answer by clicking on
v
the left. Thus it will serve as an example for other people with similar problem :)– user28595