Advertisement
Guest User

Untitled

a guest
Dec 9th, 2016
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.87 KB | None | 0 0
  1. import javax.swing.JFrame;
  2. import javax.swing.JPanel;
  3.  
  4.  
  5. public class Window extends JFrame {
  6.  
  7. /**
  8. *
  9. */
  10. private static final long serialVersionUID = -8255319694373975038L;
  11.  
  12. public static void main(String[] args){
  13.  
  14. new Window();
  15. }
  16.  
  17.  
  18. public Window(){
  19.  
  20.  
  21. // Adds the custom panel
  22. Panel Panel = new Panel();
  23. this.add(Panel);
  24.  
  25.  
  26. // Basic Window Features
  27. this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  28. this.setSize(800,800);
  29. this.setLocationRelativeTo(null);
  30. this.setVisible(true);
  31. }
  32.  
  33.  
  34. }
  35.  
  36. public class Panel extends JPanel{
  37.  
  38. public Panel(){
  39. Button testButton = new Button("Test");
  40. DrawBoard drawBoard = new DrawBoard();
  41. Listener listener = new Listener();
  42.  
  43.  
  44.  
  45. this.add(testButton);
  46. this.add(drawBoard);
  47.  
  48.  
  49. }
  50. }
  51.  
  52. import java.awt.Dimension;
  53.  
  54. import javax.swing.JButton;
  55.  
  56. public class Button extends JButton{
  57.  
  58.  
  59. public Button(String name) {
  60. this.setText(name);
  61. this.setName((String) name);
  62. buttonSettings();
  63. }
  64.  
  65. private void buttonSettings(){
  66. Listener listener = new Listener();
  67. this.addActionListener(listener);
  68.  
  69. int width = 200;
  70. int height = 50;
  71. Dimension dim = new Dimension(width,height);
  72. this.setPreferredSize(dim);
  73. }
  74.  
  75. }
  76.  
  77. import java.awt.Color;
  78. import java.awt.Dimension;
  79.  
  80. import javax.swing.JPanel;
  81.  
  82. public class DrawBoard extends JPanel{
  83.  
  84. public DrawBoard(){
  85. DrawBoardSettings();
  86. }
  87.  
  88. private void DrawBoardSettings(){
  89. int width = 600;
  90. int height = 600;
  91. Dimension dim = new Dimension(width,height);
  92. this.setPreferredSize(dim);
  93. this.setBackground(Color.WHITE);
  94. }
  95. }
  96.  
  97. import java.awt.event.ActionEvent;
  98. import java.awt.event.ActionListener;
  99. import java.awt.event.MouseEvent;
  100. import java.awt.event.MouseListener;
  101.  
  102. import javax.swing.JComponent;
  103.  
  104. public class Listener implements ActionListener, MouseListener {
  105.  
  106. @Override
  107. public void mouseClicked(MouseEvent e) {
  108. **// Draw a dot at the mouse location of the DrawBoard JPanel that was added by the Panel JPanel that was added by the Window JFrame**
  109.  
  110. }
  111.  
  112. @Override
  113. public void mousePressed(MouseEvent e) {
  114. // TODO Auto-generated method stub
  115.  
  116. }
  117.  
  118. @Override
  119. public void mouseReleased(MouseEvent e) {
  120. // TODO Auto-generated method stub
  121.  
  122. }
  123.  
  124. @Override
  125. public void mouseEntered(MouseEvent e) {
  126. // TODO Auto-generated method stub
  127.  
  128. }
  129.  
  130. @Override
  131. public void mouseExited(MouseEvent e) {
  132. // TODO Auto-generated method stub
  133.  
  134. }
  135.  
  136. @Override
  137. public void actionPerformed(ActionEvent e) {
  138. String name = ((JComponent) e.getSource()).getName();
  139. **// Draw a rectangle on the DrawBoard JPanel that was added by the Panel JPanel that was added by the Window JFrame**
  140. }
  141.  
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement