Advertisement
Guest User

Untitled

a guest
Mar 19th, 2019
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.58 KB | None | 0 0
  1. /*Michelle Sheu
  2. * 3.13.19
  3. * ChangeFont.java*/
  4.  
  5. import javax.swing.*;
  6. import java.awt.*;
  7. import java.awt.event.*;
  8.  
  9. public class ChangeFont extends JFrame
  10. {
  11. public ChangeFont()
  12. {
  13. super("ChangeFont.java");
  14. setSize( 800, 500 );
  15. setLocation ( 50, 50 );
  16. setDefaultCloseOperation( DISPOSE_ON_CLOSE );
  17. setResizable( true );
  18. Accessibility access = new Accessibility(); // instance of panel
  19. setContentPane( access );
  20. setVisible( true );
  21. }
  22. public static void main(String[] args)
  23. {
  24. ChangeFont cf = new ChangeFont();
  25. }
  26. }
  27.  
  28.  
  29. // JPanel File: Accessibility.java
  30. class Accessibility extends JPanel implements KeyListener, MouseListener
  31. {
  32. private int xMouse,yMouse;
  33. private boolean changeColor1,changeColor2;
  34. private int xloc, yloc;
  35. private Color backgroundColor;
  36. private int fontSize, colorParameterVar;
  37. private boolean boxColor;
  38. private Rectangle rectStop;
  39. private boolean dragging;
  40. private Rectangle rectBlue;
  41. private Rectangle rectBlack;
  42.  
  43.  
  44. public Accessibility() //add listeners as appropriate
  45. {
  46. colorParameterVar = 100;
  47. backgroundColor = new Color(100, 100, 100); // or backgroundColor = new Color(colorParameterVar, colorParameterVar, colorParameterVar);
  48. fontSize = 18;
  49. addKeyListener(this);
  50. addMouseListener(this);
  51. boxColor = true;
  52. dragging=false;
  53. changeColor1=false;
  54. changeColor2=false;
  55. }
  56.  
  57. public void drawRectanglesWithLabels(Graphics g) //This method is complete
  58. {
  59. g.setColor(Color.blue);
  60. g.setColor(Color.BLACK);
  61. //g.fillRect(335,10,300,100);
  62. g.setColor(Color.BLACK);
  63. if(changeColor1)
  64. {
  65. g.setColor(Color.green);
  66. g.fillRect(10,10,300,100);
  67. }
  68. else
  69. {
  70. g.setColor(Color.blue);
  71. g.fillRect(10,10,300,100);
  72. }
  73. if(changeColor2)
  74. g.setColor(Color.green);
  75. else
  76. g.setColor(Color.black);
  77. g.fillRect(335,10,300,100);
  78. g.setColor(Color.cyan);
  79. g.fillRect(400,450,50,50);
  80. }
  81.  
  82. public void medicalPageContent(Graphics g)
  83. {
  84. /*Assume this method is complete user sees medical information. You do not need to write this method. */
  85. }
  86.  
  87. public void writeDirections(Graphics g) // This was taken off to make test shorter.
  88. { /* Write directions for how the user is meant to interact with the screen.
  89. Go ahead and enter the content as a single line that begins at 80, 300. You can
  90. assume some magic allows the words to wrap so the text is not off of the screen. */
  91. g.drawString ("To increase the font size, press the letter \"u\". " +
  92. "To decrease the font size, press the letter \"d\".", 50, 300);
  93.  
  94. }
  95.  
  96. public void paintComponent(Graphics g)
  97. {
  98. setBackground(backgroundColor);
  99. super.paintComponent(g); //draw background color
  100. drawRectanglesWithLabels(g);
  101. g.setColor(Color.BLACK);
  102. Font font = new Font("Serif", Font.PLAIN, fontSize);
  103. g.setFont(font); // OR g.setFont( new Font("Serif", Font.PLAIN, fontSize) ); works, but it is not as nice to me!!!
  104. writeDirections(g); // OR paste a the sent from this method here!
  105. medicalPageContent(g);
  106. int yoffset = 24;
  107. rectBlue = new Rectangle(10, 10+yoffset, 300, 100);
  108. requestFocus();
  109. rectBlack= new Rectangle(335, 10+yoffset, 300, 100);
  110. rectStop = new Rectangle(400,450,50,50);
  111. }
  112.  
  113. /////////// 3 methods for KeyListener //////////////
  114. public void keyPressed(KeyEvent e)
  115. {
  116. /*determine if the user used the keyboard appropriately to elicit a graphical
  117. response, record or change information as appropriate, and call the method to make
  118. the appropriate graphical response */
  119.  
  120. if ( e.getKeyChar() == 'u' )
  121. fontSize += 5;
  122. else if ( e.getKeyChar() == 'd')
  123. fontSize -=5;
  124. if (fontSize < 8)
  125. fontSize = 8;
  126. else if (fontSize > 80)
  127. fontSize = 80;
  128. repaint();
  129. }
  130.  
  131. public void keyTyped (KeyEvent e)
  132. {
  133.  
  134. } //Fill in blank.
  135. public void keyReleased (KeyEvent e){} //Fill in blank.
  136.  
  137. /////////// 5 methods for MouseListener //////////////
  138.  
  139. public void mousePressed(MouseEvent e)
  140. {
  141. xMouse = e.getX();
  142. yMouse = e.getY();
  143. if (rectBlue.contains(xMouse, yMouse))
  144. changeColor1=true;
  145. else
  146. changeColor1=false;
  147. if(rectBlack.contains(xMouse,yMouse))
  148. changeColor2=true;
  149. else
  150. changeColor2=false;
  151. //System.out.print(xMouse+yMouse+""+changeColor1);
  152. repaint(); //make it so the keyboard will work!
  153. requestFocus();
  154. }
  155.  
  156. public void mouseClicked(MouseEvent e)
  157. {
  158. if(rectStop.contains(xMouse,yMouse))
  159. System.exit(0);
  160.  
  161.  
  162. }
  163. public void mouseReleased(MouseEvent e){
  164. changeColor1=false;
  165. changeColor2=false;
  166. repaint();
  167. } //This method is complete.
  168. public void mouseEntered(MouseEvent e){} //This method is complete.
  169. public void mouseExited(MouseEvent e){}//This method is complete.
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement