Need help just inserting an image in a JPanel  
Author Message
mcanncb





PostPosted: 2007-2-13 5:00:00 Top

java-programmer, Need help just inserting an image in a JPanel I want to know the quickest easiest way to insert an image in a JPanel

Here is my code so far

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.DecimalFormat;

//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(300, 450));
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);
}
}
}

 
Andrew Thompson





PostPosted: 2007-2-13 5:21:00 Top

java-programmer >> Need help just inserting an image in a JPanel On Feb 13, 8:00 am, email***@***.com wrote:
> I want to know the quickest easiest way to insert an image in a JPanel

<sscce>
import javax.swing.*;
import java.net.URL;

class ImageInPanel extends JPanel {

public ImageInPanel() {
URL url = this.
getClass().
// assumes 'current directory'
getResource("moonphase.jpg");
ImageIcon ii = new ImageIcon( url );
add( new JLabel( ii ) );
}

public static void main(String[] args) {
JOptionPane.
showMessageDialog(
null, new ImageInPanel() );
}
}
</sscce>

..at least, that is the quickest
and easiest way I know.

HTH

Andrew T.

 
Jeff Higgins





PostPosted: 2007-2-13 6:33:00 Top

java-programmer >> Need help just inserting an image in a JPanel
<email***@***.com> 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);
}
}
}


 
 
Jeff Higgins





PostPosted: 2007-2-13 6:46:00 Top

java-programmer >> Need help just inserting an image in a JPanel
Jeff Higgins wrote:
>
> <email***@***.com> wrote:
>>I want to know the quickest easiest way to insert an image in a JPanel
>>
>> Here is my code so far
>

Also:
private class CalcButtonListener implements ActionListener {
public void actionPerformed (ActionEvent event) {
String text = xvar.getText();
try {
x = Integer.parseInt (text);
}
catch (NumberFormatException e) {
// case of no entry in xvar
e.printStackTrace();
}
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);
}
}



 
 
Andrew Thompson





PostPosted: 2007-2-13 6:56:00 Top

java-programmer >> Need help just inserting an image in a JPanel On Feb 13, 9:33 am, "Jeff Higgins" <email***@***.com> wrote:
> <email***@***.com> wrote:
> >I want to know the quickest easiest way to insert an image in a JPanel
>
> > Here is my code so far
>
> package graphingcalculator;
...
> package graphingcalculator;

This is beginning to seem like a self contained
example based upon the OP's original code
(which I did not bother to compile, once
I understood 'load image'). So while I do
not have the overall understanding of this
code that someone might if they had compiled/
run it, I will point out one thing I noticed..

...
> public class MainFrame extends JFrame {
...
> public class PlotPanel extends JPanel{
..
> public void paint(Graphics graphics) {

Swing components should overide
paintComponent(), rather than paint().

> super.paint(graphics);
> Graphics2D g = (Graphics2D)graphics;
> // TODO Draw image here.
> // See Andrews post.

(frowns) ..but while I am here, note that
my example did not need to overide paint,
and that if the OP *needs* to draw the image
from within paint(/Component) then it should
probably be loaded and cached once, with
the reference stored, and simply drawn
to screen each time paint() is called.

..and it is too late for me to think
straight, so I am going to bed.

Andrew T.