Showing posts with label face.. Show all posts
Showing posts with label face.. Show all posts

Create a bouncing face in java

Problem:

Create a bouncing face.

Output:



Prompt Picture Description




Solution:

//****************BouncingFaceFrame.java******************/
import javax.swing.JFrame;

public class BouncingFaceFrame {


public static void main(String[] args) {

JFrame frame = new JFrame("Happy Face");

frame.getContentPane().add(new BouncingFacePanel());
frame.pack();
frame.setVisible(true);

}

}
//****************BouncingFacePanel.java******************.
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.Timer;

public class BouncingFacePanel extends JPanel{


private final int WIDTH = 300, HEIGHT = 300;
private final int DELAY = 20, IMAGE_SIZE = 35;

private ImageIcon icon;
private int x, y, moveX, moveY;
private Timer timer;

public BouncingFacePanel() {
timer = new Timer(DELAY, new ReboundListener());

icon = new ImageIcon("images.jpg");

x = 5; y=40;
moveX = moveY = 3;

setBackground(Color.WHITE);
setPreferredSize(new Dimension(WIDTH, HEIGHT));
timer.start();
timer.addActionListener(new ReboundListener());

}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
icon.paintIcon(this, g, x, y);
}

private class ReboundListener implements ActionListener {

@Override
public void actionPerformed(ActionEvent arg0) {

x += moveX;
y += moveY;

if ( x <= 0 || x >= (WIDTH-IMAGE_SIZE)) moveX *= -1;
if ( y <= 0 || y >= (WIDTH-IMAGE_SIZE)) moveY *= -1;

repaint();
}

}
}
Read More

GUI Interest calculator in Java

Problem:

Create a GUI interest calculator in java.

Output:



Prompt Picture Description




Solution:

import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JSpinner;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SpinnerNumberModel;

public class Calculator extends JFrame{

 private JLabel amountLabel, rateLabel, yearsLabel, outputLabel;
 private JTextField amountTextField, rateTextField;
 private JButton calculateButton; 
 private JSpinner durationSpinner;
 private JTextArea outputTextArea;
 private JScrollPane scrollPane;
 private JButton clearButton;

 public Calculator() {
  createGUI();
 }
 
 private void createGUI() {
 
  Container contentPane = getContentPane();
  contentPane.setLayout(null);
  
  
  amountLabel = new JLabel();
  amountLabel.setText("Principal");
  amountLabel.setBounds(16, 16, 76, 24);
  contentPane.add(amountLabel);
  
  amountTextField = new JTextField();
  amountTextField.setBounds(108, 16, 100, 24);
  amountTextField.setHorizontalAlignment(JTextField.RIGHT);
  contentPane.add(amountTextField);
  
  calculateButton = new JButton("Calculate");
  calculateButton.setBounds(224, 16, 100, 24);
  calculateButton.addActionListener(new ActionListener() {
  

   @Override
   public void actionPerformed(ActionEvent e) {
    processData();
   }

   private void processData() {
   
    if (amountTextField.getText().length()!=0 && 
      rateTextField.getText().length()!=0) { 
     double principal = 
       Double.parseDouble(amountTextField
       .getText());
     double rate = 
       Double.parseDouble(rateTextField.getText());
     Integer yearsInteger = (Integer) durationSpinner.getValue();
     int yearsint = yearsInteger.intValue();

     DecimalFormat fmt = new DecimalFormat("$#.##");
     double amount;
     outputTextArea.setText("Year\tAmount");

     for (int i = 1; i <= yearsint; i++) {
      amount = principal * Math.pow(1 + rate / 100, i);
      outputTextArea.append("\n" + i + "\t" + fmt.format(amount));
     }
     outputTextArea.setCaretPosition(0);
    }
    
    
   }
  });
  

  contentPane.add(calculateButton);
  
  rateLabel = new JLabel("Interest rate");
  rateLabel.setBounds(16, 56, 76, 24);
  contentPane.add(rateLabel);
  
  rateTextField = new JTextField();
  rateTextField.setBounds(108, 56, 100, 24);
  rateTextField.setHorizontalAlignment(JTextField.RIGHT);
  contentPane.add(rateTextField);
   
  clearButton = new JButton("Clear Data");
  clearButton.setBounds(224, 56, 100, 24);
  clearButton.addActionListener(new ActionListener() {
  
  
   @Override
   public void actionPerformed(ActionEvent arg0) {
    clearData();
   }

   private void clearData() {
    outputTextArea.setText("");
    amountTextField.setText("");
    rateTextField.setText("");
    
    durationSpinner.setValue(new Integer(1));
   }
   
  });
  contentPane.add(clearButton);
  
  
  yearsLabel = new JLabel("Years:");
  yearsLabel.setBounds(16, 96, 76, 24);
  contentPane.add(yearsLabel);
  
  durationSpinner = new JSpinner(new SpinnerNumberModel(1, 1, 10, 1));
  durationSpinner.setBounds(108, 96, 100, 24);
  contentPane.add(durationSpinner);
  
  outputLabel = new JLabel();
  outputLabel.setText("Yearly Account Balance:");
  outputLabel.setBounds(16, 136, 250, 24);
  contentPane.add(outputLabel);
  
  outputTextArea = new JTextArea();
  outputTextArea.setEditable(false);
  scrollPane = new JScrollPane(outputTextArea);
  scrollPane.setBounds(16, 165, 300, 90);
  scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
  contentPane.add(scrollPane);
  
  
  
  setSize(340 , 300);
  setVisible(true);
  setResizable(false);
  setLocationRelativeTo(null);
 }
 
 public static void main(String[] args) {
   
  Calculator demoFrame = new Calculator();
  demoFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  
 }

}
Read More

Follow Me

If you like our content, feel free to follow me to stay updated.

Subscribe

Enter your email address:

We hate spam as much as you do.

Upload Material

Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?

Upload

Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact