Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Michelle Sheu
- * 3.13.19
- * ChangeFont.java*/
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.*;
- public class ChangeFont extends JFrame
- {
- public ChangeFont()
- {
- super("ChangeFont.java");
- setSize( 800, 500 );
- setLocation ( 50, 50 );
- setDefaultCloseOperation( DISPOSE_ON_CLOSE );
- setResizable( true );
- Accessibility access = new Accessibility(); // instance of panel
- setContentPane( access );
- setVisible( true );
- }
- public static void main(String[] args)
- {
- ChangeFont cf = new ChangeFont();
- }
- }
- // JPanel File: Accessibility.java
- class Accessibility extends JPanel implements KeyListener, MouseListener
- {
- private int xMouse,yMouse;
- private boolean changeColor1,changeColor2;
- private int xloc, yloc;
- private Color backgroundColor;
- private int fontSize, colorParameterVar;
- private boolean boxColor;
- private Rectangle rectStop;
- private boolean dragging;
- private Rectangle rectBlue;
- private Rectangle rectBlack;
- public Accessibility() //add listeners as appropriate
- {
- colorParameterVar = 100;
- backgroundColor = new Color(100, 100, 100); // or backgroundColor = new Color(colorParameterVar, colorParameterVar, colorParameterVar);
- fontSize = 18;
- addKeyListener(this);
- addMouseListener(this);
- boxColor = true;
- dragging=false;
- changeColor1=false;
- changeColor2=false;
- }
- public void drawRectanglesWithLabels(Graphics g) //This method is complete
- {
- g.setColor(Color.blue);
- g.setColor(Color.BLACK);
- //g.fillRect(335,10,300,100);
- g.setColor(Color.BLACK);
- if(changeColor1)
- {
- g.setColor(Color.green);
- g.fillRect(10,10,300,100);
- }
- else
- {
- g.setColor(Color.blue);
- g.fillRect(10,10,300,100);
- }
- if(changeColor2)
- g.setColor(Color.green);
- else
- g.setColor(Color.black);
- g.fillRect(335,10,300,100);
- g.setColor(Color.cyan);
- g.fillRect(400,450,50,50);
- }
- public void medicalPageContent(Graphics g)
- {
- /*Assume this method is complete user sees medical information. You do not need to write this method. */
- }
- public void writeDirections(Graphics g) // This was taken off to make test shorter.
- { /* Write directions for how the user is meant to interact with the screen.
- Go ahead and enter the content as a single line that begins at 80, 300. You can
- assume some magic allows the words to wrap so the text is not off of the screen. */
- g.drawString ("To increase the font size, press the letter \"u\". " +
- "To decrease the font size, press the letter \"d\".", 50, 300);
- }
- public void paintComponent(Graphics g)
- {
- setBackground(backgroundColor);
- super.paintComponent(g); //draw background color
- drawRectanglesWithLabels(g);
- g.setColor(Color.BLACK);
- Font font = new Font("Serif", Font.PLAIN, fontSize);
- g.setFont(font); // OR g.setFont( new Font("Serif", Font.PLAIN, fontSize) ); works, but it is not as nice to me!!!
- writeDirections(g); // OR paste a the sent from this method here!
- medicalPageContent(g);
- int yoffset = 24;
- rectBlue = new Rectangle(10, 10+yoffset, 300, 100);
- requestFocus();
- rectBlack= new Rectangle(335, 10+yoffset, 300, 100);
- rectStop = new Rectangle(400,450,50,50);
- }
- /////////// 3 methods for KeyListener //////////////
- public void keyPressed(KeyEvent e)
- {
- /*determine if the user used the keyboard appropriately to elicit a graphical
- response, record or change information as appropriate, and call the method to make
- the appropriate graphical response */
- if ( e.getKeyChar() == 'u' )
- fontSize += 5;
- else if ( e.getKeyChar() == 'd')
- fontSize -=5;
- if (fontSize < 8)
- fontSize = 8;
- else if (fontSize > 80)
- fontSize = 80;
- repaint();
- }
- public void keyTyped (KeyEvent e)
- {
- } //Fill in blank.
- public void keyReleased (KeyEvent e){} //Fill in blank.
- /////////// 5 methods for MouseListener //////////////
- public void mousePressed(MouseEvent e)
- {
- xMouse = e.getX();
- yMouse = e.getY();
- if (rectBlue.contains(xMouse, yMouse))
- changeColor1=true;
- else
- changeColor1=false;
- if(rectBlack.contains(xMouse,yMouse))
- changeColor2=true;
- else
- changeColor2=false;
- //System.out.print(xMouse+yMouse+""+changeColor1);
- repaint(); //make it so the keyboard will work!
- requestFocus();
- }
- public void mouseClicked(MouseEvent e)
- {
- if(rectStop.contains(xMouse,yMouse))
- System.exit(0);
- }
- public void mouseReleased(MouseEvent e){
- changeColor1=false;
- changeColor2=false;
- repaint();
- } //This method is complete.
- public void mouseEntered(MouseEvent e){} //This method is complete.
- public void mouseExited(MouseEvent e){}//This method is complete.
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement