Created
May 27, 2017 20:37
-
-
Save tps2015gh/0f5826650c49ab7b62f06e55fa764928 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Author Thitipong Samranvanich | |
// Since 2017-05-28 | |
// For: Demo Java 2D Painting + Swing GUI | |
// STILL ERROR , CAN NOT MOUSE MOVE , By Mouse Click is OK | |
import javax.swing.JFrame; | |
import javax.swing.JLabel; | |
import javax.swing.JPanel; | |
import javax.swing.JTextField; | |
// for Graphics 2D | |
import java.awt.* ; | |
import java.awt.geom.*; // <== for Line2D | |
// for MouseEvent | |
import java.awt.event.*; | |
public class Form1 extends JFrame implements MouseMotionListener{ | |
JLabel lb ; | |
JPanel pan ; | |
int x2 = 200, y2 = 200; | |
public Form1(){ | |
//===================== | |
pan = new JPanel(); | |
pan.setBackground(new Color(170,180,170)); | |
//this.add(pan); | |
getContentPane().add(pan); | |
this.addComp(pan); | |
//=== add event == | |
this.addMouseListener( new MouseAdapter(){ | |
public void mouseEnter(MouseEvent e){ | |
System.out.println("MouseENTER"); | |
} | |
public void mouseClicked(MouseEvent e){ | |
x2 = e.getX(); | |
y2 = e.getY(); | |
repaint(); | |
System.out.println(" mouse clicked"); | |
} | |
public void mouseMoved(MouseEvent e){ | |
lb.setText(" mouse x,y = "+ e.getX() | |
+ "," + e.getY() | |
); | |
x2 = e.getX(); | |
y2 = e.getY(); | |
repaint(); | |
System.out.println(" mouse moved"); | |
} | |
}); | |
//===== set property == | |
this.setSize(300,400); | |
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
this.setVisible(true); | |
} | |
public void mouseDragged(MouseEvent e){ | |
System.out.println(" mouse draged , Implemented "); | |
} | |
public void mouseMoved(MouseEvent e){ | |
System.out.println(" mouse moved , Implemented "); | |
} | |
private void addComp(JPanel pan){ | |
pan.setLayout(null); | |
lb = new JLabel("Demo Label"); | |
lb.setBounds(10,10,200,30); | |
pan.add(lb); | |
JTextField tx = new JTextField(40); | |
tx.setText("Demo Text"); | |
tx.setBounds( 20 ,30 , 200, 20 ); | |
pan.add(tx); | |
pan.invalidate(); | |
} | |
public void paint(Graphics g){ | |
super.paint(g); // must do | |
Graphics2D g2 = (Graphics2D)g ; | |
Line2D lin = new Line2D.Float(100,100,x2,y2 ); | |
g2.draw(lin); | |
//this.invalidate(); | |
getContentPane().invalidate(); | |
} | |
public static void main(String[] args){ | |
System.out.println("Form1 Started "); | |
Form1 f1 = new Form1(); | |
System.out.println("Form1 Created "); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment