Re: Need help just inserting an image in a JPanel
<mcanncb@auburn.edu> wrote:
I want to know the quickest easiest way to insert an image in a JPanel
Here is my code so far
package graphingcalculator;
public class GraphingCalculator {
public static void main(String[] args) {
MainFrame frame = new MainFrame();
}
}
package graphingcalculator;
import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
public class MainFrame extends JFrame {
public MainFrame() {
setTitle("Graphing Calculator");
setSize(640,480);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
CalculatePanel calculate = new CalculatePanel();
PlotPanel plot = new PlotPanel();
plot.setBackground(Color.white);
add(calculate,BorderLayout.NORTH);
add(plot,BorderLayout.CENTER);
setVisible(true);
}
}
package graphingcalculator;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JPanel;
import javax.swing.border.BevelBorder;
public class PlotPanel extends JPanel{
public PlotPanel() {
setBorder(new BevelBorder(BevelBorder.LOWERED));
}
public void paint(Graphics graphics) {
super.paint(graphics);
Graphics2D g = (Graphics2D)graphics;
// TODO Draw image here.
// See Andrews post.
}
}
package graphingcalculator;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.BevelBorder;
//Panel containing components
public class CalculatePanel extends JPanel{
//1) Declare components to be used
private JLabel inputLabel, resultLabel;
private JTextField xvar;
private int x; private double ans;
private JButton calcButton;
//2) Sets up the GUI
public CalculatePanel(){
//2a) Create two labels
inputLabel = new JLabel ("Enter posotive value of x:");
resultLabel = new JLabel ("Value of expression");
//2b) Create text field
xvar = new JTextField (10);
//2c) Add action listener to the text field
xvar.addActionListener (new TextFieldListener());
//2d) Add components to the panel
add (inputLabel);
add (xvar);
add (resultLabel);
//2e) set buttons and listeners
calcButton = new JButton("Calculate Expression");
calcButton.addActionListener(new CalcButtonListener());
setPreferredSize (new Dimension(30, 45));
setBorder(new BevelBorder(BevelBorder.LOWERED));
setBackground (Color.green);
add(calcButton);
}
//3) Create the action listener for the text field
private class TextFieldListener implements ActionListener {
//--------------------------------------------------------
// Performs the conversion when the enter key is pressed in the text
field
//--------------------------------------------------------
public void actionPerformed (ActionEvent event){
String text = xvar.getText();
x = Integer.parseInt (text);
double ans = Math.sqrt (Math.abs(3 * (Math.pow((x), 5))
- (12 * (Math.pow((x), 4))) - (9 * (Math.pow((x), 2))) + (2
*x)));
DecimalFormat df = new DecimalFormat ("0.###");
String result = "Value of expression = " + df.format(ans);
resultLabel.setText (result);
}
}
private class CalcButtonListener implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
String text = xvar.getText();
x = Integer.parseInt (text);
double ans = Math.sqrt (Math.abs(3 * (Math.pow((x), 5))
- (12 * (Math.pow((x), 4))) - (9 * (Math.pow((x), 2))) +
(2 * x)));
String result = "Value of expression = " + ans;
DecimalFormat df = new DecimalFormat ("0.###");
String result1 = "Value of expression = " + df.format(ans);
resultLabel.setText (result1);
}
}
}