1 Attachment(s)
Drawing with the mouse in the same frame as swing components
Hi - I can't find anything on the internet to help with this.
Any help or suggestions will be greatly appreciated!
I have a frame with swing components (Jbuttons) and two different classes with logical gate drawings..
Attachment 3401
I want to be able to draw with the mouse on the same panel as these logical gates:
The below works fine if I take super.paint(g) out, but then the lightweight components mess up, I can only have it one way or the other..
Is there any way around this?
Code:
//Class to display gates to screen:
class DrawCanvas extends JPanel implements MouseMotionListener
{
public DrawCanvas()
{
addMouseMotionListener(this);
//setOpaque(false);
setVisible(true);
}
public void paint(Graphics g)
{
super.paint(g); //<----
setBackground(Color.gray);
System.out.println("Painting the screen");
//Display my AND/OR gate drawing:
if (bAnd == true)
{
System.out.println("And gate has been drawn!");
andDisplay.andGraf(g);
}
if (bOr == true)
{
System.out.println("Or gate has been drawn!");
orDisplay.OrGraf(g);
}
//Draw Continuous line:
g.setColor(drawingColour);
g.fillRect(mx1,my1, 5, 5);
}
//Continuous drawing: (works without super.paint)
public void mouseDragged(MouseEvent e)
{
mouseMoved(e);
repaint();
}
public void mouseMoved(MouseEvent e)
{
mx1 = (int) e.getPoint().getX();
my1 = (int) e.getPoint().getY();
System.out.println("x = " + mx1);
System.out.println("y = " + my1);
}
}
Thanks.
Re: Drawing with the mouse in the same frame as swing components
You will want to search this forum, as there are many examples of how to draw. For example:
Re: Drawing with the mouse in the same frame as swing components
Sweet, just finished the program - thanks for the reply.. the links were very helpful!