Labels As Pages

Search This Blog

Loading...

Monday, February 20

!ANSWER! Assignment 5

...and...

WE ARE DONE!
import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class BajrovicAssignment5 {
 
 public static void main(String[] args) {

  // Swing is not thread safe.
  // UI code should run in a event dispatching thread.
  EventQueue.invokeLater(
  /**
   * An object of an anonymous inner class that implements Runnable
   * interface is sent to event dispatching thread which executes the run
   * method.
   */
  new Runnable() {

   @Override
   public void run() {

    // construct a JFrame object
    Drawing frame = new Drawing();
    // set the new JFrame to be visible
    frame.setVisible(true);
    // exit program when the JFrame is closed
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   }
  });
 }
}


class Drawing extends JFrame {
private JLabel coordinates;
private int x=0, y=0;
private Color color;

 public Drawing() {
  //sets name & size of window
  setTitle("BajrovicAssignment5");
  setSize(500, 200);
  
  // add the panel to the content pane of the JFrame
  
  coordinates = new JLabel();
  Container contentPane = getContentPane();
  contentPane.add(coordinates, BorderLayout.SOUTH);
    
  contentPane.addMouseMotionListener(new mouseMovements());  //records user's motions on content pane
 }
   class mouseMovements extends MouseMotionAdapter{
    public void mouseMoved(MouseEvent e) {  //added functionality just for clarity 
     coordinates.setText("X: " + Integer.toString(e.getX()) + ", Y: " + Integer.toString(e.getY()));
   }
    public void mouseDragged(MouseEvent e) {
     x = e.getX(); //gets the current x & y coordinates of mouse, updated as mouse is dragged
        y = e.getY();
     repaint();  //neater to use than update() or paint()
    }
   }
   public void update(Graphics g){ //added for clarity for the repaint method, b/c the repaint method first calls update() THEN paint()
     paint(g);
    }
    
   public void paint(Graphics g) {  //is responsible for drawing the users motions on the content pane
     g.setColor(Color.black);
     g.fillRect(x,y,10,10);
    }
     }

Monday, February 13

Assignment 5

We're FINALLY here!!!!

This is the last assignment!

ELET4309
Assignment 5

Description

Write a simple Java program that allows users to draw on a panel with a mouse.

Happy CODING

Friday, February 10

Friday 10, Update, update...

So this week's post, is an update in my post schedule. I'm simply feeling unmotivated and have decided to scale back my posts to 4 times a month. I've finished uploading my ELET 4309 assignments and answers so I'm not leaving you hanging.

In all honestly, you can only write a blog for a large audience to yourself for so long. Obviously, this is my fault as its my job to promote my blog, but I never planned on this becoming an obligation so I'm taking it slowly and making sure I have content rather a bunch of followers.

In any case, I hope you enjoy the posts, even if they are less frequent.

~Minela

Monday, February 6

!ANSWER! Assignment 4

import java.awt.*;
import java.awt.event.*;
import java.text.*;
import java.util.Scanner;
import javax.swing.*;


public class BajrovicAssignment4 {
	public static void main(String[] args) {

		// Swing is not thread safe.
		// UI code should run in a event dispatching thread.
		EventQueue.invokeLater(
		/**
		 * An object of an anonymous inner class that implements Runnable
		 * interface is sent to event dispatching thread which executes the run
		 * method.
		 */
		new Runnable() {

			@Override
			public void run() {

				// construct a JFrame object
				FrameWithLabel frame = new FrameWithLabel();
				// set the new JFrame to be visible
				frame.setVisible(true);
				// exit program when the JFrame is closed
				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			}
		});
	}
}

	class FrameWithLabel extends JFrame {
		private Box tempConv;
		private JLabel label;
		private JPanel panelButtons;
		private JRadioButton radioTo;
		private JRadioButton radioFrom;
		private JLabel textLabel;
		private JLabel textLabel2;
		private static JLabel converted;
		static JTextField temperature;
		private JPanel gridPanel;
		private static String identifier;
		private static String convertedDegree;
		private JButton convertButton;
		private JPanel chensterPanel;
		private JPanel lowLevelPanel;
		
		public FrameWithLabel() {
			//sets name & size of window
			setTitle("BajrovicAssignment4");
			setSize(500, 200);
			
			//label box
			tempConv = Box.createHorizontalBox();
			label = new JLabel("Temperature Convertor");
			tempConv.add(label, BorderLayout.NORTH);
			
			//radio buttons 
			panelButtons = new JPanel();
			radioTo = new JRadioButton("To Celsius");
			radioFrom = new JRadioButton("From Celsius");
			panelButtons.setBorder(BorderFactory.createTitledBorder("Options"));
			panelButtons.add(radioTo);
			panelButtons.add(radioFrom);
			radioTo.setSelected(true);
			
			//limit of 1 radio button selected
			ButtonGroup buttonGroup = new ButtonGroup();
			buttonGroup.add(radioFrom);
			buttonGroup.add(radioTo);
			
			//associate events with each button
			radioTo.addItemListener(new Buttons ());
			radioFrom.addItemListener(new Buttons ());
			
			//text field
			textLabel = new JLabel("Input a Fahrenheit Value", SwingConstants.RIGHT);
			textLabel2 = new JLabel("Celsius Value", SwingConstants.RIGHT);
			textLabel2.setHorizontalAlignment(JLabel.RIGHT);
			converted = new JLabel ();
			converted.setHorizontalAlignment(SwingConstants.LEFT);
			temperature = new JTextField();
			
			temperature.addActionListener(new Text());
			
			//Convert Button
			lowLevelPanel = new JPanel();
			convertButton = new JButton("Convert");
			lowLevelPanel.add(convertButton);
			
			convertButton.addActionListener(new Text());
			
			//adding all components to all panels
			chensterPanel = new JPanel();
			gridPanel = new JPanel();
			chensterPanel.setLayout(new GridLayout(2, 1));
		   	gridPanel.setLayout(new GridLayout(2, 2,20,5));
		   	gridPanel.add(textLabel);
		   	gridPanel.add(temperature);
		   	gridPanel.add(textLabel2);
		   	gridPanel.add(converted);
		   	chensterPanel.add(gridPanel);
		   	chensterPanel.add(lowLevelPanel);
			
			// add the panel to the content pane of the JFrame
			Container contentPane = getContentPane();
			contentPane.add(tempConv, BorderLayout.NORTH);
			contentPane.add(panelButtons, BorderLayout.CENTER);
			contentPane.add(chensterPanel, BorderLayout.SOUTH);
			
			//focuses on the text field ONLY WHEN THE WINDOW IS SELECTED
			addWindowFocusListener(new WindowAdapter() {
				@Override
			    public void windowGainedFocus(WindowEvent e) {
			   	 FrameWithLabel.temperature.requestFocus();
			   	 //immediately selects the 'To Celsius' radio button
			   	 identifier = "to";
			    };
			});	
		}
		
		class Buttons implements ItemListener{
			@Override
			public void itemStateChanged(ItemEvent e){
				if(e.getItem() == radioTo){
					//tells the converter function which equation to use
					identifier = "to";
					textLabel.setText("Input a Fahrenheit Value");
					textLabel2.setText("Celsius Value");
					//clears the text field when the user changes the radio button
					temperature.setText(null);					
				}
				if(e.getItem() == radioFrom){
					identifier = "from";
					textLabel.setText("Input a Celsius Value");
					textLabel2.setText("Fahrenheit Value");
					temperature.setText(null);				
				}
			}
		}
		class Text implements ActionListener{
			@Override
			public void actionPerformed(ActionEvent a){
				//calls the converter function if a there is a value in the temperature text field
				converter();
			}
		}
		public static void converter(){
			if (identifier.equals("to")){
				//changes the string in the text field to a double
				double Fahrenheit = Double.parseDouble(temperature.getText());
				//the conversion equation
				double Celsius = (Fahrenheit-32)*5/9;
				//formats the double to 2 rounded decimal places
				DecimalFormat df = new DecimalFormat("0.00");
				//converts the double into a string so it can be stored in a label
				convertedDegree = String.valueOf(df.format(Celsius));
			//stores the calculated value in the label
			converted.setText(convertedDegree);
			}
				if (identifier.equals("from")){
				double Celsius = Double.parseDouble(temperature.getText());
				double Fahrenheit = (Celsius*9/5)+32;
				DecimalFormat df = new DecimalFormat("0.00");
				convertedDegree = String.valueOf(df.format(Fahrenheit));
			converted.setText(convertedDegree);
			}
		}
	}

Friday, February 3

Friday Diary 9, A Day in the Life of a Kindle Owner

Again, as you know... I got a Kindle Touch from the bf for graduation! <3

And I fell in L.O.V.E. with it! So far, I've read:

  • Confessions of a Shopaholic
  • The Hunger Game Trilogy
And am currently reading:
  • Frankenstein
My favorite thing about the Kindle Touch: lightweight.

My least favorite: No hardware page turn buttons.

Keep reading my friends,
~Minela