• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problem with GUI

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have the following problems with my Graphics Editor done using java:

1. The JPanel gets erased when ever i maximise or minimise the window and open it again.

2. More than one events are called concurrently on the click of one button. That is if we click one command button then the action event of that button is called. When i press the another JButton, both the Action events of the previous and current clicked JButton are called.

Please some one help me to overcome these problems....
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"mohammed sanaulla@NITK,"

Welcome to JavaRanch! Please check your private messages by clicking on My Private Messages. Thanks!

We also have a forum devoted to Swing/AWT issues. I'll move your post there. You might also want to post your code for the button event handling.
 
mohammed sanaulla
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code snippet for drawing solid Line is as follows:


/*This is the code snippet for SolidLine JBUtton
This button Draws a solid line on the JPanel which in this case is named as canvas*/

package actionlisteners;
import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.awt.event.*;
import shapes.*;
import graphics2dframe.*;
import java.awt.geom.*;
import shapes.*;

public class LineDrawListener implements ActionListener {
private ArrayList<Line2D> solidLineList;
private int x1,x2,y1,y2;
public void actionPerformed(ActionEvent event)
{
MainFrame.canvas.addMouseListener(new mouseHandler());//canvas is a static reference variable in the Class MainFrame

}
public class mouseHandler extends MouseAdapter
{
public void mousePressed(MouseEvent e)
{
x1=e.getX();
y1=e.getY();
}
public void mouseReleased(MouseEvent e)
{
x2=e.getX();
y2=e.getY();
LineStyles.drawSolidLine(x1,y1,x2,y2);//I have Used DDA Line Algorithm for drawing lines and not the inbuilt method drawLine()
/*drawSolidLine is a method defined in the LineStyles package. The source code of this is not that much relevent with my problem
i have not used the concept of threads any where in the program*/
}
}


}
 
mohammed sanaulla
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
/*Code snippet for drawing a dashed line on the JPanel*/
package actionlisteners;
import javax.swing.*;
import java.awt.event.*;
import shapes.*;
import graphics2dframe.*;
import java.util.*;
import java.awt.geom.*;


public class DashLineDrawListener implements ActionListener {
private ArrayList<Line2D> dashLineList;
private int x1,y1,x2,y2;
public void actionPerformed(ActionEvent event)
{
MainFrame.canvas.addMouseListener(new mouseHandler());

}
public class mouseHandler extends MouseAdapter
{
public void mousePressed(MouseEvent e)
{
x1=e.getX();
y1=e.getY();
}
public void mouseReleased(MouseEvent e)
{
x2=e.getX();
y2=e.getY();
LineStyles.drawDashLine(x1, y1, x2, y2);/* This method also uses DDA Line algorithm for drawing the Dashed Line*/
/*This method is a static method of class LineStyles and not a class of LineStyles package as said earlier*/

}
}

}
 
mohammed sanaulla
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
/*This is the code snippet describing the Class LineStyles along with its threee static methods
Actually this is the class taht contains the methods for drawing different lines onthe JPanel i.e canvas(it is a reference of type JPanle*/

package shapes;

import graphics2dframe.MainFrame;
import java.awt.Graphics;
import java.awt.Graphics2D;

public class LineStyles {
Graphics g;
static Graphics2D g2;

public LineStyles()
{
g = MainFrame.canvas.getGraphics();/*Graphics2D object is used for drawing line and also for setting the background color*/
Graphics2D g2 = (Graphics2D)g;
g2.setColor(MainFrame.selectedF);
g2.setBackground(MainFrame.selectedB);
}
/*Drawing Solid Line using DDA Line Algorithm*/

public static void drawSolidLine(int x1,int y1,int x2,int y2)
{
Graphics g = MainFrame.canvas.getGraphics();
Graphics2D g2 = (Graphics2D)g;
g2.setColor(MainFrame.selectedF);
g2.setBackground(MainFrame.selectedB);
double dx,dy,step;
double xinc,yinc,x,y;
int i;
dx = x2-x1;
dy = y2-y1;
if(Math.abs(dx)>Math.abs(dy))
{
step = Math.abs(dx);
}
else
{
step = Math.abs(dy);
}
xinc = dx/step;
yinc = dy/step;
x=x1;
y=y1;
g2.drawLine((int)x,(int)y,(int)x,(int)y);
for(i = 0;i< step;i++)
{
x=x+xinc;
y=y+yinc;
g2.drawLine((int)x,(int)y,(int)x,(int)y);
}
}
public static void drawDashLine(int x1,int y1,int x2,int y2)
{
Graphics g = MainFrame.canvas.getGraphics();
Graphics2D g2 = (Graphics2D)g;
g2.setColor(MainFrame.selectedF);
g2.setBackground(MainFrame.selectedB);
int count =4;
int j=0;
double dx,dy,step;
double xinc,yinc,x,y;
int i;
dx = x2-x1;
dy = y2-y1;
if(Math.abs(dx)>Math.abs(dy))
{
step = Math.abs(dx);
}
else
{
step = Math.abs(dy);
}
xinc = dx/step;
yinc = dy/step;
x=x1;
y=y1;
g2.drawLine((int)x,(int)y,(int)x,(int)y);
for(i = 0;i< step;i++)
{
x=x+xinc;
y=y+yinc;
if(count==4)
{
count=0;
j++;
}
if(j%2== 0)
{

}
else
{
g2.drawLine((int)x,(int)y,(int)x,(int)y);
}
count ++;

}
count=0;j=0;
}

public static void drawDotLine(int x1,int y1,int x2,int y2)
{
int count=0;
Graphics g = MainFrame.canvas.getGraphics();
Graphics2D g2 = (Graphics2D)g;
g2.setColor(MainFrame.selectedF);
g2.setBackground(MainFrame.selectedB);
int j=0;
double dx,dy,step;
double xinc,yinc,x,y;
int i;
dx = x2-x1;
dy = y2-y1;
if(Math.abs(dx)>Math.abs(dy))
{
step = Math.abs(dx);
}
else
{
step = Math.abs(dy);
}
xinc = dx/step;
yinc = dy/step;
x=x1;
y=y1;
g2.drawLine((int)x,(int)y,(int)x,(int)y);
for(i = 0;i< step;i++)
{
x=x+xinc;
y=y+yinc;
if(count%3==0)
{
g2.drawLine((int)x, (int)y, (int)x, (int)y);
}
count++;
}
}

}
 
mohammed sanaulla
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
package graphics2dframe;
import javax.swing.*;
import shapes.*;

import java.awt.*;
import java.awt.event.*;
import java.lang.reflect.InvocationTargetException;

import actionlisteners.*;
import actionlisteners.LineDrawListener.mouseHandler;

public class MainFrame extends JFrame {
public static Color selectedB,selectedF;
public static JColorChooser colorChooser;
private JDialog dialog;
public static JFileChooser chooser;
JMenuBar mainMenu;
JMenu file,edit,shapes,color,effects,look,help;
JMenuItem exit,select,cut,clear,text,fg,bg,motif,windows,metal;
JMenu line,polygon;
JMenuItem solid,dashl,dot,dashdot,dashdotdot;
public static JPanel canvas;
public static JFrame cFrame;
public Dimension screensize;
JToolBar topBar;
JToolBar mainBar;
public static LineDrawListener solidLineListener,dashDotLineListener,dashDotDotLineListener;
FreehandDrawListener curveListener;
RectDrawListener rectListener;
EllipseDrawListener ellipseListener;
CircleDrawListener circleListener;
PolygonDrawListener polygonListener;
TriangleDrawListener triangleListener;
HexagonDrawListener hexagonListener;
NewFileListener newFile;
OpenFileListener openFile;
SaveFileListener saveFile;
AboutListener about;
ZoominListener zoomIn;
ZoomoutListener zoomOut;
DashLineDrawListener dash;
DotLineDrawListener dotLineListener;
JButton solidb,dashb,dotb,dashdotb,dashdotdotb;

/*End of the fields declaration*/
/*---------------------------------------*/

public MainFrame()
{
cFrame = this;
cFrame.addMouseListener(new MouseAdapter()
{
public void mouseEntered(MouseEvent e)
{
setCursor(Cursor.getDefaultCursor());
}
});
initialiseComponents();//Initialize the Menu Items
addActionListeners();// Adding the action listeners to the menu items
topBar = new JToolBar();
topBar.addMouseListener(new MouseAdapter()
{
public void mouseEntered(MouseEvent e)
{
setCursor(Cursor.getDefaultCursor());
}
});
addToolBars();//adding tool bars to the frame

setLayout(new BorderLayout());
chooser = new JFileChooser();

add(topBar,BorderLayout.NORTH );
canvas = new JPanel();
canvas.addMouseListener(new MouseAdapter()
{
public void mouseEntered(MouseEvent e)
{
setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
}
});
canvas.setBackground(Color.white);
Toolkit kit = Toolkit.getDefaultToolkit();
screensize = kit.getScreenSize();
//Setting the size of the frame to that of the Display size
setSize(screensize.width , screensize.height );
add(canvas,BorderLayout.CENTER);

}

/*---------------------------------------*/
/*Adding all the Menus to the MainFrame*/
private void initialiseComponents()
{
mainMenu = new JMenuBar();
setJMenuBar(mainMenu);
file = new JMenu("File");
edit = new JMenu("Edit");
shapes = new JMenu("Shapes");
color= new JMenu("Color");
effects = new JMenu("Effect");
look = new JMenu("Look");
help = new JMenu("Help");

mainMenu.add(file);
mainMenu.add(edit);
mainMenu.add(shapes);
mainMenu.add(color);
mainMenu.add(effects);
mainMenu.add(look);
mainMenu.add(help);

/*Adding Menu Items to the Various menus by using AbstractAction Class*/

/*-----------------------------------*/
//Adding Menu Items to the File Menu
newFile = new NewFileListener("New",new ImageIcon("C:\\Documents and Settings\\Administrator\\Desktop\\Images\\general\\new16.gif"),"New File");
file.add(newFile);

openFile = new OpenFileListener("Open", new ImageIcon("C:\\Documents and Settings\\Administrator\\Desktop\\Images\\general\\open16.gif"),"Open File");
file.add(openFile);

saveFile = new SaveFileListener("Save",new ImageIcon("C:\\Documents and Settings\\Administrator\\Desktop\\Images\\general\\save16.gif"),"Save File");
file.add(saveFile);

exit = new JMenuItem("Exit");
file.addSeparator();
file.add(exit);

/*-----------------------------------*/
//Adding Menu Items to the Shapes Menu
line = new JMenu("Line");
shapes.add(line);


solid = new JMenuItem("Line",new ImageIcon("C:\\Documents and Settings\\Administrator\\Desktop\\Images\\line\\style_solid32.gif"));
solid.addActionListener(new LineDrawListener());
line.add(solid);



dashl = new JMenuItem("Dash Line", new ImageIcon("C:\\Documents and Settings\\Administrator\\Desktop\\Images\\line\\style_dash32.gif"));
dashl.addActionListener(new DashLineDrawListener());
line.add(dashl);


dot= new JMenuItem("Dot Line", new ImageIcon("C:\\Documents and Settings\\Administrator\\Desktop\\Images\\line\\style_dot32.gif"));
dot.addActionListener(new DotLineDrawListener());
line.add(dot);


/*-----------------------------------*/
/*Adding Menu Items to the color Menu*/
fg = new JMenuItem("Fore Ground");
bg = new JMenuItem("Back Ground");
color.add(fg);
color.add(bg);
/*-----------------------------------*/
//Adding Menu Items to the Look Menu
motif = new JMenuItem("Motif");
windows = new JMenuItem("Windows");
metal = new JMenuItem("Metal");
look.add(motif);
look.add(windows);
look.add(metal);
/*-----------------------------------*/
/*Adding Menu items to the Help Menu*/
about = new AboutListener("About", new ImageIcon("C:\\Documents and Settings\\Administrator\\Desktop\\Images\\general\\about16.gif"),"About the Package");
help.add(about);
about.setComponent(canvas);


}

/*---------------------------------------*/
/*Method to add action listeners to Look and Feel*/
public void addActionListeners()
{
exit.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
int selection = JOptionPane.showConfirmDialog(cFrame, "Do you want to really Quit?","Exit",JOptionPane.OK_CANCEL_OPTION,JOptionPane.WARNING_MESSAGE);
if(selection==JOptionPane.OK_OPTION)
{
System.exit(1);
}
else
{

}

}
});
motif.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
SwingUtilities.updateComponentTreeUI(cFrame);
}catch(Exception ex){ex.printStackTrace();}
}
}
);
windows.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
try
{
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
SwingUtilities.updateComponentTreeUI(cFrame);
}catch(Exception e){e.printStackTrace();}
}
});
metal.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
try
{
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
SwingUtilities.updateComponentTreeUI(cFrame);
}catch(Exception e){e.printStackTrace();}
}
});
/*Adding action event to the Menu item Clear*/
clear.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
int selection=JOptionPane.showConfirmDialog(cFrame, "Do you really want to Clear the Area?", "Clear",JOptionPane.OK_CANCEL_OPTION,JOptionPane.WARNING_MESSAGE);
if(selection==JOptionPane.OK_OPTION)
{
Graphics g = canvas.getGraphics();
g.setColor(getBackground());
g.fillRect(0,0,getSize().width,getSize().height);
g.dispose();
repaint();
}
else
{
}
}
});
/*Adding action Event to the Menu item Fore Ground Color*/
fg.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
Color defaultColor = canvas.getForeground();
selectedF = colorChooser.showDialog(canvas, "Fore Ground", defaultColor);
if(selectedF!=null)canvas.setForeground(selectedF);

}
});
/*Adding action Event to the Menu item Back Ground Color*/
bg.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
Color defaultColor = canvas.getBackground();
selectedB = JColorChooser.showDialog(canvas, "Back Ground", defaultColor);
if(selectedB!=null)canvas.setBackground(selectedB);
}
});

}

//Method to add Tool bar to the main Frame
public void addToolBars()
{
solidb = new JButton(new ImageIcon("C:\\Documents and Settings\\Administrator\\Desktop\\Images\\line\\style_solid32.gif"));
dashb = new JButton(new ImageIcon("C:\\Documents and Settings\\Administrator\\Desktop\\Images\\line\\style_dash32.gif"));
dotb = new JButton(new ImageIcon("C:\\Documents and Settings\\Administrator\\Desktop\\Images\\line\\style_dot32.gif"));
solidb.addActionListener(new LineDrawListener());
dashb.addActionListener(new DashLineDrawListener());
dotb.addActionListener(new DotLineDrawListener());

topBar.add(newFile);
topBar.add(openFile);
topBar.add(saveFile);
topBar.addSeparator();
topBar.add(solidb);
topBar.add(dashb);
topBar.add(dotb);
/*topBar.add(solidLineListener);
topBar.add(dash);
topBar.add(dotLineListener);*/

topBar.addSeparator();
topBar.add(curveListener);
topBar.add(rectListener);
topBar.add(ellipseListener);
topBar.add(polygonListener);
topBar.add(triangleListener);
topBar.add(hexagonListener);
}

}
 
mohammed sanaulla
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
package graphics2dframe;
import javax.swing.*;
import shapes.*;

import java.awt.*;
import java.awt.event.*;
import java.lang.reflect.InvocationTargetException;

import actionlisteners.*;
import actionlisteners.LineDrawListener.mouseHandler;

public class MainFrame extends JFrame {
public static Color selectedB,selectedF;
public static JColorChooser colorChooser;
private JDialog dialog;
public static JFileChooser chooser;
JMenuBar mainMenu;
JMenu file,edit,shapes,color,effects,look,help;
JMenuItem exit,select,cut,clear,text,fg,bg,motif,windows,metal;
JMenu line,polygon;
JMenuItem solid,dashl,dot,dashdot,dashdotdot;
public static JPanel canvas;
public static JFrame cFrame;
public Dimension screensize;
JToolBar topBar;
JToolBar mainBar;
public static LineDrawListener solidLineListener,dashDotLineListener,dashDotDotLineListener;
FreehandDrawListener curveListener;
RectDrawListener rectListener;
EllipseDrawListener ellipseListener;
CircleDrawListener circleListener;
PolygonDrawListener polygonListener;
TriangleDrawListener triangleListener;
HexagonDrawListener hexagonListener;
NewFileListener newFile;
OpenFileListener openFile;
SaveFileListener saveFile;
AboutListener about;
ZoominListener zoomIn;
ZoomoutListener zoomOut;
DashLineDrawListener dash;
DotLineDrawListener dotLineListener;
JButton solidb,dashb,dotb,dashdotb,dashdotdotb;

/*End of the fields declaration*/
/*---------------------------------------*/

public MainFrame()
{
cFrame = this;
cFrame.addMouseListener(new MouseAdapter()
{
public void mouseEntered(MouseEvent e)
{
setCursor(Cursor.getDefaultCursor());
}
});
initialiseComponents();//Initialize the Menu Items
addActionListeners();// Adding the action listeners to the menu items
topBar = new JToolBar();
topBar.addMouseListener(new MouseAdapter()
{
public void mouseEntered(MouseEvent e)
{
setCursor(Cursor.getDefaultCursor());
}
});
addToolBars();//adding tool bars to the frame

setLayout(new BorderLayout());
chooser = new JFileChooser();

add(topBar,BorderLayout.NORTH );
canvas = new JPanel();
canvas.addMouseListener(new MouseAdapter()
{
public void mouseEntered(MouseEvent e)
{
setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
}
});
canvas.setBackground(Color.white);
Toolkit kit = Toolkit.getDefaultToolkit();
screensize = kit.getScreenSize();
//Setting the size of the frame to that of the Display size
setSize(screensize.width , screensize.height );
add(canvas,BorderLayout.CENTER);

}

/*---------------------------------------*/
/*Adding all the Menus to the MainFrame*/
private void initialiseComponents()
{
mainMenu = new JMenuBar();
setJMenuBar(mainMenu);
file = new JMenu("File");
edit = new JMenu("Edit");
shapes = new JMenu("Shapes");
color= new JMenu("Color");
effects = new JMenu("Effect");
look = new JMenu("Look");
help = new JMenu("Help");

mainMenu.add(file);
mainMenu.add(edit);
mainMenu.add(shapes);
mainMenu.add(color);
mainMenu.add(effects);
mainMenu.add(look);
mainMenu.add(help);

/*Adding Menu Items to the Various menus by using AbstractAction Class*/

/*-----------------------------------*/
//Adding Menu Items to the File Menu
newFile = new NewFileListener("New",new ImageIcon("C:\\Documents and Settings\\Administrator\\Desktop\\Images\\general\\new16.gif"),"New File");
file.add(newFile);

openFile = new OpenFileListener("Open", new ImageIcon("C:\\Documents and Settings\\Administrator\\Desktop\\Images\\general\\open16.gif"),"Open File");
file.add(openFile);

saveFile = new SaveFileListener("Save",new ImageIcon("C:\\Documents and Settings\\Administrator\\Desktop\\Images\\general\\save16.gif"),"Save File");
file.add(saveFile);

exit = new JMenuItem("Exit");
file.addSeparator();
file.add(exit);

/*-----------------------------------*/
//Adding Menu Items to the Shapes Menu
line = new JMenu("Line");
shapes.add(line);


solid = new JMenuItem("Line",new ImageIcon("C:\\Documents and Settings\\Administrator\\Desktop\\Images\\line\\style_solid32.gif"));
solid.addActionListener(new LineDrawListener());
line.add(solid);



dashl = new JMenuItem("Dash Line", new ImageIcon("C:\\Documents and Settings\\Administrator\\Desktop\\Images\\line\\style_dash32.gif"));
dashl.addActionListener(new DashLineDrawListener());
line.add(dashl);


dot= new JMenuItem("Dot Line", new ImageIcon("C:\\Documents and Settings\\Administrator\\Desktop\\Images\\line\\style_dot32.gif"));
dot.addActionListener(new DotLineDrawListener());
line.add(dot);


/*-----------------------------------*/
/*Adding Menu Items to the color Menu*/
fg = new JMenuItem("Fore Ground");
bg = new JMenuItem("Back Ground");
color.add(fg);
color.add(bg);
/*-----------------------------------*/
//Adding Menu Items to the Look Menu
motif = new JMenuItem("Motif");
windows = new JMenuItem("Windows");
metal = new JMenuItem("Metal");
look.add(motif);
look.add(windows);
look.add(metal);
/*-----------------------------------*/
/*Adding Menu items to the Help Menu*/
about = new AboutListener("About", new ImageIcon("C:\\Documents and Settings\\Administrator\\Desktop\\Images\\general\\about16.gif"),"About the Package");
help.add(about);
about.setComponent(canvas);


}

/*---------------------------------------*/
/*Method to add action listeners to Look and Feel*/
public void addActionListeners()
{
exit.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
int selection = JOptionPane.showConfirmDialog(cFrame, "Do you want to really Quit?","Exit",JOptionPane.OK_CANCEL_OPTION,JOptionPane.WARNING_MESSAGE);
if(selection==JOptionPane.OK_OPTION)
{
System.exit(1);
}
else
{

}

}
});
motif.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
SwingUtilities.updateComponentTreeUI(cFrame);
}catch(Exception ex){ex.printStackTrace();}
}
}
);
windows.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
try
{
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
SwingUtilities.updateComponentTreeUI(cFrame);
}catch(Exception e){e.printStackTrace();}
}
});
metal.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
try
{
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
SwingUtilities.updateComponentTreeUI(cFrame);
}catch(Exception e){e.printStackTrace();}
}
});
/*Adding action event to the Menu item Clear*/
clear.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
int selection=JOptionPane.showConfirmDialog(cFrame, "Do you really want to Clear the Area?", "Clear",JOptionPane.OK_CANCEL_OPTION,JOptionPane.WARNING_MESSAGE);
if(selection==JOptionPane.OK_OPTION)
{
Graphics g = canvas.getGraphics();
g.setColor(getBackground());
g.fillRect(0,0,getSize().width,getSize().height);
g.dispose();
repaint();
}
else
{
}
}
});
/*Adding action Event to the Menu item Fore Ground Color*/
fg.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
Color defaultColor = canvas.getForeground();
selectedF = colorChooser.showDialog(canvas, "Fore Ground", defaultColor);
if(selectedF!=null)canvas.setForeground(selectedF);

}
});
/*Adding action Event to the Menu item Back Ground Color*/
bg.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
Color defaultColor = canvas.getBackground();
selectedB = JColorChooser.showDialog(canvas, "Back Ground", defaultColor);
if(selectedB!=null)canvas.setBackground(selectedB);
}
});

}

//Method to add Tool bar to the main Frame
public void addToolBars()
{
solidb = new JButton(new ImageIcon("C:\\Documents and Settings\\Administrator\\Desktop\\Images\\line\\style_solid32.gif"));
dashb = new JButton(new ImageIcon("C:\\Documents and Settings\\Administrator\\Desktop\\Images\\line\\style_dash32.gif"));
dotb = new JButton(new ImageIcon("C:\\Documents and Settings\\Administrator\\Desktop\\Images\\line\\style_dot32.gif"));
solidb.addActionListener(new LineDrawListener());
dashb.addActionListener(new DashLineDrawListener());
dotb.addActionListener(new DotLineDrawListener());

topBar.add(newFile);
topBar.add(openFile);
topBar.add(saveFile);
topBar.addSeparator();
topBar.add(solidb);
topBar.add(dashb);
topBar.add(dotb);
/*topBar.add(solidLineListener);
topBar.add(dash);
topBar.add(dotLineListener);*/

topBar.addSeparator();
topBar.add(curveListener);
topBar.add(rectListener);
topBar.add(ellipseListener);
topBar.add(polygonListener);
topBar.add(triangleListener);
topBar.add(hexagonListener);
}

}
 
mohammed sanaulla
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am really sorry that i have posted huge amounts of code. The first two code snippets posted are actual actionListeners. The last one is for creating a GUI of the Graphics Editor Package.The main Classes are the first two code snippets posted,,,,,,

I really want someone to help me out with this. I have tried with so many Java professional at my place but none could help me out.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got confused about the code at first and had to delete a post because I had written rubbish.

You seem to be adding lots of Listeners to the top bar, which will mean you have multiple actions from one mouse click.
Try moving those Listeners to individual buttons or menu items, and see whether that makes things any better.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In your posting which I have down as 20.46 in my time zone (GMT); it will probably have .46 wherever you are, you have an actionPerformed method which adds a Listener elsewhere. It's in the LineDrawListener class. If you do that, then you will multiply the events happening whenever you click anywhere. Haev you done the same anywhere else? If you have, then there will be even more Listeners.
 
mohammed sanaulla
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually i have Menu items and also a tool bar. So i have added actionListeners to both of them. And i have grouped all the action Listeners in one group under the name Action Listeners. And i have added the action Listeners in MainFrame class, whcih runs when an object of the frame is formed.So MainFrame is the class which will add all the actionListeners to all the menu items and also to tool bar items.

I also tried using anonymous Inner classes in the Main Frame but the problem still persisted.

I also have one more problem- The JPanel gets repainted when ever i minimise or maximise the window.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think it is normal for the panels to be repainted whenever the window is un-minimised.
You only want the Listeners to be added to one Component each. Don't add them to several each. Try correcting that, then tell us what happens.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic