public void mouseEntered(MouseEvent e) {
int x = e.getX();
int y = e.getY();
m_View = new View(m_Mod);
int maxY = m_Mod.y1 m_Mod.oheight;
int maxX = m_Mod.x1 m_Mod.owidth;
if (x > m_Mod.x1 & x < maxX & y > m_Mod.y1 & x < maxY) {
Graphics g = m_Mod.frame.getGraphics();
m_View.draw(g);
System.out.print("IM KREIS");
}
}
不知何故,我的數學在這里不起作用。一旦滑鼠進入它,我想重新繪制我的橢圓。所以我的圖形物件“g”是我的橢圓形。m_Mod.x1 / M_Mod.y1 是我的橢圓的 x 和 y,是我的橢圓m_Mod.width / m_Mod.heigth 的寬度和高度。
uj5u.com熱心網友回復:
如評論中所述,解決方案的關鍵是:
- 在單個繪圖 JPanel 中進行所有繪圖、所有圖形和滑鼠監聽
- 在 JPanel 的 paintComponent 方法覆寫內專門繪制
- 為您的 GUI 提供一個設定為 true 的布爾欄位
- 使用 Ellipse2D 物件定義橢圓
- 將 MouseMotionListener 添加到繪圖 JPanel
- 在此 MouseMotionListener 中,檢查滑鼠點是否在橢圓內
- 然后用這個結果設定布爾欄位并呼叫
repaint() - 然后,paintComponent 方法將根據布林值的狀態確定要繪制的內容
例如,在下面的代碼中,我有一個 Ellise2D 欄位,
private Ellipse2D myOval = new Ellipse2D.Double(OVAL_X, OVAL_Y, OVAL_W, OVAL_H);
然后有一個布爾欄位,private boolean insideOval = false;設定為 false,如果滑鼠點在橢圓內(這里稱為 ),則在 MouseMotionListener 內將其更改為 true myOval:
addMouseMotionListener(new MouseAdapter() {
@Override
public void mouseMoved(MouseEvent e) {
// true if the mouse point is within the oval. Otherwise false
insideOval = myOval.contains(e.getPoint());
// redraw the JPanel to draw the state of insideOval
repaint();
}
});
然后paintComponent方法將使用狀態insideOval來確定用什么顏色填充橢圓,如果布林值為真,則為“活動顏色”,否則為清晰顏色:
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g); // does house-keeping drawing
Graphics2D g2 = (Graphics2D)g;
// allow for smooth curves
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
// vary the oval fill color depending on the state of the insideOval field
Color c = insideOval ? ACTIVE_COLOR : INACTIVE_COLOR;
g2.setColor(c);
// fill the oval
g2.fill(myOval);
// then draw the oval's border
g2.setColor(OVAL_BORDER);
g2.setStroke(BORDER_STROKE);
g2.draw(myOval);
}
例如:
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Stroke;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Ellipse2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
@SuppressWarnings("serial")
public class DrawingPanel extends JPanel {
// main JPanel dimensions
private static final int PREF_W = 600;
private static final int PREF_H = 600;
// oval dimensions
private static final double OVAL_X = 100;
private static final double OVAL_Y = OVAL_X;
private static final double OVAL_W = 400;
private static final double OVAL_H = OVAL_W;
// oval colors
private static final Color INACTIVE_COLOR = new Color(0, 0, 0, 0); // clear
private static final Color ACTIVE_COLOR = Color.PINK;
private static final Color OVAL_BORDER = Color.LIGHT_GRAY;
// thickness of mouse border
private static final Stroke BORDER_STROKE = new BasicStroke(4f);
// Oval to draw and boolean to tell if mouse within the oval
private Ellipse2D myOval = new Ellipse2D.Double(OVAL_X, OVAL_Y, OVAL_W, OVAL_H);
private boolean insideOval = false;
public DrawingPanel() {
// add a mouse motion listener to this same drawing JPanel
// and inside of it, check if the mouse point is inside or outside
// of the oval.
addMouseMotionListener(new MouseAdapter() {
@Override
public void mouseMoved(MouseEvent e) {
// true if the mouse point is within the oval. Otherwise false
insideOval = myOval.contains(e.getPoint());
// redraw the JPanel to draw the state of insideOval
repaint();
}
});
}
@Override
public Dimension getPreferredSize() {
if (isPreferredSizeSet()) {
return getPreferredSize();
} else {
return new Dimension(PREF_W, PREF_H);
}
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g); // does house-keeping drawing
Graphics2D g2 = (Graphics2D)g;
// allow for smooth curves
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
// vary the oval fill color depending on the state of the insideOval field
Color c = insideOval ? ACTIVE_COLOR : INACTIVE_COLOR;
g2.setColor(c);
// fill the oval
g2.fill(myOval);
// then draw the oval's border
g2.setColor(OVAL_BORDER);
g2.setStroke(BORDER_STROKE);
g2.draw(myOval);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
DrawingPanel mainPanel = new DrawingPanel();
JFrame frame = new JFrame("GUI");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(mainPanel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
});
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/491721.html
