lab10
This commit is contained in:
27
lab/lab10/Question6/MyFrame.java
Normal file
27
lab/lab10/Question6/MyFrame.java
Normal file
@@ -0,0 +1,27 @@
|
||||
|
||||
/*
|
||||
* Author: CHEN Yongyuan (Walter) from OOP(1007)
|
||||
* Date: 2022-05-01
|
||||
* Description: This is the Main Frame class.
|
||||
*/
|
||||
import java.awt.BorderLayout;
|
||||
import javax.swing.JFrame;
|
||||
|
||||
public class MyFrame extends JFrame {
|
||||
|
||||
public MyFrame() {
|
||||
setTitle("MyFrame");
|
||||
setSize(400, 300);
|
||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
|
||||
// center the frame
|
||||
setLocationRelativeTo(null);
|
||||
|
||||
setLayout(new BorderLayout());
|
||||
|
||||
// my draw panel
|
||||
add(new MyPanel());
|
||||
|
||||
setVisible(true);
|
||||
}
|
||||
}
|
||||
15
lab/lab10/Question6/MyPanel.java
Normal file
15
lab/lab10/Question6/MyPanel.java
Normal file
@@ -0,0 +1,15 @@
|
||||
import java.awt.Color;
|
||||
import java.awt.Graphics;
|
||||
import javax.swing.JPanel;
|
||||
|
||||
public class MyPanel extends JPanel {
|
||||
@Override
|
||||
protected void paintComponent(Graphics g) {
|
||||
// clean the panel
|
||||
super.paintComponent(g);
|
||||
// draw
|
||||
g.setColor(Color.RED);
|
||||
g.drawString("hello", (int) Math.round(Math.random() * this.getWidth()),
|
||||
(int) Math.round(Math.random() * this.getHeight()));
|
||||
}
|
||||
}
|
||||
15
lab/lab10/Question6/Start.java
Normal file
15
lab/lab10/Question6/Start.java
Normal file
@@ -0,0 +1,15 @@
|
||||
/**
|
||||
* Answer to question.
|
||||
*
|
||||
*/
|
||||
public class Start {
|
||||
public static void main(String[] args) {
|
||||
// Dispather
|
||||
javax.swing.SwingUtilities.invokeLater(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
new MyFrame();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user