Graphics2D to ImageIcon problem

J

James Kimble

I'm trying to do something that should be very simple.

I want to create an applet (or application, doesn't matter) to display a set
of drawn objects (drawn with a Graphics2D object). I want to be able to display
these objects in a pane by choosing them from a combo box. Two panes, one
on top with the combo box, one on the botton with the graphic.

I managed to get the images to be drawn in a simple panel stacked next to
each other but I really want to be able to select them and have them drawn
one at a time when selected in the combo box.

My program currently just instantiatest my classes that do the Graphics2D
drawing (the classes extend JPanel and just have a paint method in them)
and puts them in a grid layout.

REAL QUESTION: I would like to create an ImageIcon and just update it
with a new image when the combo box selection changes.
The problem is I can't seem to find a way to convert a
BufferedImage or a Graphics2D object into an ImageIcon.
Should I be doing this in a different way?

I don't work with JAVA everyday (though I love the language) and GUI and
graphics things just frustrate me to no end. I still haven't got the right
model in my head.

Any help would be very apprciated. Thanks,

James Kimble

--------------------------------------------------------------------------


Here's the code I have at the minute:

Main calling class:

//v 1.3
import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;
import java.util.*;
import java.math.*;
import javax.swing.*;
import java.net.URL;

public class DSP1 implements ActionListener
{
final static int NUM_IMAGES = 8;
final static int START_INDEX = 3;

JLabel waveLabel = null;

JPanel mainPanel, noisePanel, sinePanel, noisesinePanel, stonPanel;
JPanel selectPanel, displayPanel, wavePanel;

JComboBox rateChoices = null;
JComboBox freqChoices = null;
JComboBox waveOption = null;


// Constructor
public DSP1()
{
// Create the main panel to contain the two sub panels.
mainPanel = new JPanel();
mainPanel.setLayout(new GridLayout(2,1,5,5));
mainPanel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));

selectPanel = new JPanel();
displayPanel = new JPanel();

waveLabel = new JLabel();
waveLabel.setHorizontalAlignment(JLabel.CENTER);
waveLabel.setVerticalAlignment(JLabel.CENTER);
waveLabel.setVerticalTextPosition(JLabel.CENTER);
waveLabel.setHorizontalTextPosition(JLabel.CENTER);
waveLabel.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createLoweredBevelBorder(),
BorderFactory.createEmptyBorder(5,5,5,5)));

waveLabel.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createEmptyBorder(0,0,10,0),
waveLabel.getBorder()));


waveLabel.setText("");


String[] Wave = { "Sine", "Noise", "Sine + Noise", "Signal/Noise" };
waveOption = new JComboBox( Wave );
waveOption.setSelectedIndex ( 1 );


// Add border around the select panel.
selectPanel.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createTitledBorder("Select Phase"),
BorderFactory.createEmptyBorder(5,5,5,5)));

// Add border around the display panel.
displayPanel.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createTitledBorder("Display Phase"),
BorderFactory.createEmptyBorder(5,5,5,5)));


selectPanel.add(waveOption);
displayPanel.add(waveLabel);

mainPanel.add ( selectPanel );
mainPanel.add ( displayPanel );

waveOption.addActionListener(this);
}


// Implementation of ActionListener interface.
public void actionPerformed(ActionEvent event)
{
if ( "comboBoxChanged".equals(event.getActionCommand()) )
{

System.out.println ( "In action: " +
waveOption.getSelectedIndex() );

switch ( waveOption.getSelectedIndex() )
{
case 0: wavePanel = new SineWave();
displayPanel.add(wavePanel);
break;
case 1: wavePanel = new NoiseWave();
mainPanel.add(wavePanel);
break;
case 2: wavePanel = new NoiseSineWave();
mainPanel.add(wavePanel);
break;
case 3: wavePanel = new SignaltoNoise();
mainPanel.add(wavePanel);
break;
}
}
}


// main method
public static void main(String[] args)
{
// create a new instance of DSP1
DSP1 dsp_1 = new DSP1();

// Create a frame and container for the panels.
JFrame dsp1Frame = new JFrame("Lunar Phases");


// Set the look and feel.
try
{
UIManager.setLookAndFeel(
UIManager.getCrossPlatformLookAndFeelClassName());
} catch(Exception e) {}

dsp1Frame.setContentPane(dsp_1.mainPanel);
dsp1Frame.setBounds(300,300,500,500);

// Exit when the window is closed.
dsp1Frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// Show the converter.
//dsp1Frame.pack();
dsp1Frame.setVisible(true);
}
}


Drawing class (they're all the same format):

import java.awt.*;
import java.awt.geom.*;
import java.awt.image.*;
import java.awt.event.*;
import java.util.*;
import java.math.*;
import javax.swing.*;
import java.net.URL;


class SineWave extends JPanel
{
static int rate;

public void paint (Graphics g)
{
Graphics2D g2D; // Get a Java 2D device context


super.paint(g);

g2D = (Graphics2D)g; // Get a Java 2D device context

g2D.drawString("Sine Wave", 250, 10); // Draw some text
g2D.drawString(" 100", 15, 95 ); // Draw some text
g2D.drawString(" 0", 15, 205); // Draw some text
g2D.drawString("-100", 15, 305); // Draw some text

g2D.drawLine ( 40, 200, 460, 200 );

int old_x = 50;
int old_y = 200;
int new_x = 50;
int new_y = 200;

for ( int i=1; i <= 100; i++ )
{
old_x = new_x;
old_y = new_y;

new_x += 4;
new_y = (int)(((Math.sin((6.2830/100.0)*i))*100)+200.0);

//System.out.println ( "From: " + old_x + ", " + old_y +
// " To: " + new_x + ", " + new_y );

g2D.drawLine ( old_x, old_y, new_x, new_y );
}
}


public static void setRate ( int r )
{
rate = r;
}
}
 
A

ak

REAL QUESTION: I would like to create an ImageIcon and just update it
with a new image when the combo box selection changes.
The problem is I can't seem to find a way to convert a
BufferedImage or a Graphics2D object into an ImageIcon.

BufferedImage is extends Image so you can just create ImageIcon with it:
BufferedImage img = ....;
ImageIcon ic = new ImageIcon(img);

//paint to BufferedImage:
Graphics g = img.getGraphics();
g.draw<whatever>;

____________

http://reader.imagero.com the best java image reader.
 
J

James Kimble

Thanks, that was just what I needed.

jk


ak said:
BufferedImage is extends Image so you can just create ImageIcon with it:
BufferedImage img = ....;
ImageIcon ic = new ImageIcon(img);

//paint to BufferedImage:
Graphics g = img.getGraphics();
g.draw<whatever>;

____________

http://reader.imagero.com the best java image reader.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top