Results 1 to 1 of 1
- 12-11-2011, 02:34 PM #1
Member
- Join Date
- Dec 2011
- Posts
- 1
- Rep Power
- 0
how to add graphic diagrams to the arraylist
My code is that am able to draw different diagrams on mouse drag to the panel but i cant see the diagram drawn if i draw another diagram . i.e i want all the diagrams to be displayed .
if i had drawn rectangle and now if i press the button ellipse to draw a ellipse then the rectangle is vanishing ,but i want all to be remained on the screen.please help me with this
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.awt.Graphics;
class drawing extends JPanel
{
int x1,x2,y2,y1,s,l,b;
public drawing()
{
setBackground(Color.CYAN);
addMouseMotionListener(new MouseMotionAdapter()
{
public void mouseDragged(MouseEvent m)
{
x2=m.getX();
y2=m.getY();
repaint();
}
});
addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent m)
{
x1=m.getX();
y1=m.getY();
repaint();
}
public void mouseReleased(MouseEvent m)
{
x2=m.getX();
y2=m.getY();
repaint();
}
});
}
public void paint(Graphics g)
{
super.paint(g);
l=Math.abs(this.x1-this.x2);
b=Math.abs(this.y1-this.y2);
if(s==2)
{
g.drawRect(x1,y1,l,b);
}
else if(s==3)
{
g.drawOval(x1,y1,l,b);
}
else if(s==4)
{
g.drawLine(x1,y1,x2,y2);
}
else
{
g.drawString("",10,10);
}
}
}
public class Proj extends JFrame
{
JLabel lab=new JLabel();
drawing d=new drawing();
public Proj()
{
ImageIcon rect=new ImageIcon("image/rect.gif");
ImageIcon ellip=new ImageIcon("image/elli.gif");
JPanel p=new JPanel();
JButton rectangle=new JButton(rect);
JButton ellipse=new JButton(ellip);
JButton line=new JButton("Line");
rectangle.setSize(80,17);
ellipse.setSize(100,17);
line.setSize(100,17);
rectangle.setToolTipText("Entity");
ellipse.setToolTipText("attributes");
line.setToolTipText("links");
p.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
p.add(rectangle);
p.add(ellipse);
p.add(line);
rectangle.setMnemonic('r');
ellipse.setMnemonic('e');
line.setMnemonic('l');
add(p,BorderLayout.NORTH);
add(d,BorderLayout.CENTER);
rectangle.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent a)
{
d.s=2;
}
});
ellipse.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent a)
{
d.s=3;
}
});
line.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent a)
{
d.s=4;
}
});
}
public static void main(String args[])
{
Proj pr=new Proj();
pr.setTitle("sample");
pr.setSize(500,500);
pr.setVisible(true);
pr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pr.pack();
}
}
Similar Threads
-
In TabbedPane Diagrams do not appear
By greenhorn in forum New To JavaReplies: 1Last Post: 04-04-2011, 10:38 PM -
drag and drop for diagrams - help!
By zoz in forum AWT / SwingReplies: 10Last Post: 12-20-2009, 07:59 PM -
Exporting UML diagrams to MS Visio
By lettyh in forum NetBeansReplies: 0Last Post: 10-30-2008, 06:19 PM -
Eclipse plugin for UML diagrams
By nitingupta183 in forum EclipseReplies: 3Last Post: 03-26-2008, 11:08 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks