Chris Reck
Posts: 7
Nickname: creck
Registered: Nov, 2002
|
|
Re: Multiple event handler and how to select
|
Posted: Nov 22, 2002 4:15 AM
|
|
Here is the code in question. It's not real pretty but it's doing most of what I want.
class IconButtonEdit extends JDialog implements MouseInputListener, ComponentListener, ActionListener {
Insets IBEinsets = null;
MouseEvent me = null;
int oldSize = 0;
String editMode = "Edit";
JButton[] buttons = new JButton[32*32];
Object pix = null;
public IconButtonEdit(ImageTry mParent, int iconW, int iconH, Insets MFinsets) {
super (mParent, "Icon Editing panel", true);
addWindowListener(new DialogWindowMonitor());
setModal(false);
setLocation(MFinsets.left, MFinsets.top+23);
pix = iconPallet.pg.getPixels();
JButton jb;
for (int i = 0; i < iconW*iconH; i++) {
jb = new JButton();
jb.setBackground(new Color(((int[])pix)[i]));
jb.setBorderPainted(false);
jb.addMouseListener(this);
jb.setName(""+i);
getContentPane().add(jb);
buttons[i] = jb;
pixels[i] = ((int[])pix)[i];
}
addComponentListener(this);
//addMouseListener(this);
//addMouseMotionListener(this);
JMenuBar editMenuBar = new JMenuBar();
setJMenuBar(editMenuBar);
JMenu icon = new JMenu("Icon");
icon.setMnemonic('I');
JMenu view = new JMenu("View");
view.setMnemonic('V');
JMenuItem clearIcon = new JMenuItem("Clear");
clearIcon.addActionListener(this);
JMenuItem reloadIcon = new JMenuItem("Reload");
reloadIcon.addActionListener(this);
JMenuItem getColorIcon = new JMenuItem("Get Color");
getColorIcon.addActionListener(this);
JMenuItem saveIcon = new JMenuItem("Save");
saveIcon.addActionListener(this);
JMenuItem saveAtIcon = new JMenuItem("Save-At");
saveAtIcon.addActionListener(this);
JMenuItem exitIcon = new JMenuItem("Exit");
exitIcon.addActionListener(this);
icon.add(clearIcon);
icon.add(reloadIcon);
icon.add(getColorIcon);
icon.add(saveIcon);
icon.add(saveAtIcon);
icon.add(exitIcon);
editMenuBar.add(icon);
getContentPane().setLayout(new GridLayout(iconW,iconH));
pack();
IBEinsets = getInsets();
editSize = screenHeight-cp.getHeight()-MFinsets.top-MFinsets.bottom-80;
editSize = (editSize/32)*32;
oldSize = editSize;
setSize(editSize+IBEinsets.left+IBEinsets.right, editSize+IBEinsets.top);
setVisible(true);
}
public void actionPerformed(ActionEvent ae) {
System.out.println(ae.getActionCommand());
if(ae.getActionCommand() == "Clear")
{
for (int i = 0; i < 32*32; i++) {
pixels[i] = Color.black.getRGB();
}
icon = createImage(new MemoryImageSource(32, 32, pixels, 0, 32));
iconPV.repaint();
}
if(ae.getActionCommand() == "Reload")
{
for (int i = 0; i < 32*32; i++) {
buttons[i].setBackground(new Color(((int[])pix)[i]));
pixels[i] = ((int[])pix)[i];
}
icon = createImage(new MemoryImageSource(32, 32, pixels, 0, 32));
iconPV.repaint();
}
if(ae.getActionCommand() == "Get Color")
{
editMode = "getColor";
}
if(ae.getActionCommand() == "Save")
{
gImg.drawImage(icon,
iconOriginX,
iconOriginY,
iconOriginX+32,
iconOriginY+32,
0,0,32,32,null);
img = backImg;
try {
GIFEncoder encode = new GIFEncoder(img);
OutputStream output = new BufferedOutputStream(
new FileOutputStream("test1.dat"));
encode.Write(output);
}
catch (AWTException awte) {}
catch (FileNotFoundException FNF) {}
catch (IOException ie) {}
}
if(ae.getActionCommand() == "Exit")
{
showIconPallet();
setVisible(false);
dispose();
closeIconPreview();
closeColorPanel();
}
}
public void mouseEntered(MouseEvent me) {
System.out.println("test");
if ((me.getModifiers() & InputEvent.BUTTON1_MASK) !=0 )
{ c = ColorPanel.getPriColor();
me.getComponent().setBackground(c);
pixels[Integer.valueOf(me.getComponent()
.getName()).intValue()] = c.getRGB();
icon = createImage(new MemoryImageSource(32, 32, pixels, 0, 32));
iconPV.repaint();
}
if ((me.getModifiers() & InputEvent.BUTTON3_MASK) !=0 )
{ c = ColorPanel.getSecColor();
me.getComponent().setBackground(c);
pixels[Integer.valueOf(me.getComponent()
.getName()).intValue()] = c.getRGB();
icon = createImage(new MemoryImageSource(32, 32, pixels, 0, 32));
iconPV.repaint();
}
}
public void mousePressed(MouseEvent me) {
if(editMode == "Edit") {
if ( (me.getModifiers() & InputEvent.BUTTON1_MASK) !=0 )
c = ColorPanel.getPriColor();
else
c = ColorPanel.getSecColor();
me.getComponent().setBackground(c);
pixels[Integer.valueOf(me.getComponent()
.getName()).intValue()] = c.getRGB();
icon = createImage(new MemoryImageSource(32, 32, pixels, 0, 32));
iconPV.repaint();
}
if(editMode == "getColor") {
if ( (me.getModifiers() & InputEvent.BUTTON1_MASK) !=0 ) {
ColorPanel.setPriColor(me.getComponent().getBackground()) ;
ColorPanel.preView1.setBackground(ColorPanel.getPriColor());
}
else {
ColorPanel.setSecColor(me.getComponent().getBackground());
ColorPanel.preView2.setBackground(ColorPanel.getSecColor());
}
editMode = "Edit";
}
}
public void mouseMoved(MouseEvent me) {}
public void mouseClicked(MouseEvent me) {}
public void mouseReleased(MouseEvent me) {}
public void mouseDragged(MouseEvent me) {}
public void mouseExited(MouseEvent me) {}
public void componentResized(ComponentEvent ce) {
if(getWidth() != oldSize+IBEinsets.left+IBEinsets.right)
{
editSize = getWidth();
if (editSize > screenHeight-(int)(screenHeight*.1))
editSize = screenHeight-(int)(screenHeight*.1);
editSize = (editSize/32)*32;
oldSize = editSize;
}
else if(this.getHeight() != oldSize+IBEinsets.top)
{
editSize = getHeight();
if (editSize > screenHeight-(int)(screenHeight*.1))
editSize = screenHeight-(int)(screenHeight*.1);
editSize = (editSize/32)*32;
oldSize = editSize;
}
setSize(editSize+IBEinsets.left+IBEinsets.right, editSize+IBEinsets.top-5);
}
public void componentMoved (ComponentEvent ce) {}
public void componentShown (ComponentEvent ce) {}
public void componentHidden (ComponentEvent ce) {}
}
|
|