マウスクリックでパネルを再描画する方法を見つけようとしています。マウスのクリックは問題なくキャプチャできますが、一生コンポーネントを描画することはできません。これは、簡単にするために設定した単純化されたテスト クラスです。私の Test クラスは JComponent を拡張しています。
何か案は?
これが私のメインです:
public static void main(String[] args) {
JFrame frame = new JFrame("Point Capture Test");
frame.setContentPane(new Test().panelMain);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
ここに私のテストコンストラクターがあります:
public Test() {
mPanel.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
JOptionPane.showMessageDialog(panelMain, e.getPoint());
JOptionPane.showMessageDialog(panelMain, "clicked "+ e.getSource() + " at " + e.getPoint());
//removed a bunch of stuff here that captures the clicked coordinates so I can use them to draw lines on the panel
repaint();
}
});
}
私の Test paintComponent メソッドは次のとおりです。
public void paintComponent(Graphics g){
Graphics2D g2 = (Graphics2D) g;
if (this.mClicks > 2) { //draw polygon
Polygon polyTest = new Polygon();
for (Point point : this.mPntPoints){
polyTest.addPoint(point.x, point.y);
}
g2.setColor(Color.RED);
g2.fill(polyTest);
g2.draw(polyTest);
}
//just added this as a test and it doesn't draw either
g2.drawLine(10, 30, 20, 40);
}