hey all!
i'm trying to create a 'drawing canvas' (JPanel) and it 's quite ok. my problem is that i can either draw but duplicate all other components or not draw and have only one set of components.
basically i have a class wich has a panel with 2 buttons. and also a class that extends JPanel. in the 'JPanel extension' class i call paintComponet(Graphics g) and withing that method i call super.paintComponent(g).
if i don't call the super.paintComponet(g) then i can draw but the other panel, the one with buttons, gets duplicated. if i don't call that method then the panel with buttons doesn't get duplicated but i cannot draw ))):
the code was originally an applet but i made it a JFrame extension so it can be easily run.
here is the code. run it a you will see the problem. anybody any suggestions??
v.v.
//first class
import global.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
//
public class Draw extends JFrame implements ActionListener{
private Container cn;
private JPanel drawingPanel;
private JPanel pnlControls = new JPanel();
private JComboBox comboLine;
private JButton btnClear, btnSave;
//
//public void init(){
public Draw(){
try{
SwingUtilities.invokeAndWait(new Runnable(){
public void run(){
createGUI();
}
});
}catch(Exception e){
JOptionPane.showMessageDialog(null,e.getMessage());
}
}
private void createGUI(){
btnClear = new JButton("clear work area");
btnClear.addActionListener(this);
btnSave = new JButton("save work");
btnSave.addActionListener(this);
//
pnlControls.setLayout(new FlowLayout());
pnlControls.add(btnClear);
pnlControls.add(btnSave);
//
drawingPanel = new DrawingPanel(this);
//
cn = this.getContentPane();
cn.setLayout(new BorderLayout());
cn.add(pnlControls, BorderLayout.NORTH);
cn.add(drawingPanel,BorderLayout.CENTER);
}
//
//LISTENERS
//
public void actionPerformed(ActionEvent ae){
if(ae.getSource() == btnSave){
}
if(ae.getSource() == btnClear){
}
}
//
public static void main(String[] vik){
Draw d = new Draw();
d.setSize(400,400);
d.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
d.setVisible(true);
}
}
//second class in 'global' directory
package global;
//
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
//
public class DrawingPanel extends JPanel implements MouseListener, MouseMotionListener{
private static final int DIAMETER = 15;
//
private boolean rightMouseButton = false;
private int oldX, oldY;
//private JApplet applet;
private JFrame applet;
//
public DrawingPanel(JFrame applet){
this.applet = applet;
this.addMouseMotionListener(this);
this.addMouseListener(this);
this.setPreferredSize(new Dimension(200,200));
}
//
//LISTENRS
//
public void mouseClicked(MouseEvent me){
int x, y, clickCount;
//
x = me.getX();
y = me.getY();
clickCount = me.getClickCount();
//
//applet.showStatus("mouse clicked: " + clickCount);
this.repaint();
}
public void mousePressed(MouseEvent me){
rightMouseButton = me.isMetaDown() ? true : false;
String button = rightMouseButton ? "right button" : "left button";
//applet.showStatus("mouse pressed: " + button);
}
public void mouseReleased(MouseEvent me){
//applet.showStatus("mouse released: release");
}
public void mouseEntered(MouseEvent me){
//applet.showStatus("mouse entered: entered drawing panel");
}
public void mouseExited(MouseEvent me){
//applet.showStatus("mouse exited: exited drawing panel");
}
//
public void mouseDragged(MouseEvent me){
//applet.showStatus("mouse dragged: being dragged");
//
oldX = me.getX();
oldY = me.getY();
this.repaint();
}
public void mouseMoved(MouseEvent me){
//applet.showStatus("mouse moved: moved");
}
@Override
public void paintComponent(Graphics g){
//try to comment - uncomment the line below
//super.paintComponent(g);
if(rightMouseButton){
Color c = this.getBackground();
g.setColor(c);
g.fillOval(oldX,oldY,DIAMETER,DIAMETER);
}else{
g.setColor(Color.blue);
g.fillOval(oldX,oldY,DIAMETER,DIAMETER);
}
}
}