Early Stages, please help. Thanks

I'm currently a java student (Early Stages) and having problems with a small paint program. I dont expect a direct answer as it is part of assignment, but i am having trouble, the line does not draw now..
I have tryed the paint(Graphics g) method that does work when i draw a line, but its not what i want. i am now creating a Graphics component directly in the mouseDragged() method.
Also if you could tell me why the mouse is off when i draw the line.
import javax.swing.*;*
*import java.awt.*;
import java.awt.event.*;*
*import java.awt.Window.*;
class RevisedColourGUI extends JFrame {
     int oldX;
     int oldY;
     int newX;
     int newY;
     Color colour = new Color(0,0,0);
     JButton red = new JButton("Red");
     JButton green = new JButton("Green");
     JButton blue = new JButton("Blue");
     JButton black = new JButton("Black");
     JButton erase = new JButton("Erase");
     JScrollBar redBar = new JScrollBar(JScrollBar.HORIZONTAL, 0,1,0,255);
     JScrollBar greenBar = new JScrollBar(JScrollBar.HORIZONTAL, 0,1,0,255);
     JScrollBar blueBar = new JScrollBar(JScrollBar.HORIZONTAL, 0,1,0,255);
     JLabel redLabel = new JLabel("Red");
     JLabel greenLabel = new JLabel("Green");
     JLabel blueLabel = new JLabel("Blue");
     JPanel scrollPane = new JPanel();
     JPanel colourPane = new JPanel();
     JPanel drawPane = new JPanel();
     JPanel buttonPane = new JPanel();
     JPanel labelPane = new JPanel();
     JPanel southPane = new JPanel();
     ButtonListener btn = new ButtonListener();
     ScrollListener scrl = new ScrollListener();
     PointerListener pointer = new PointerListener();
     MotionListener motion = new MotionListener();
     RevisedColourGUI() {
          super("TestGUI");
          setupGUI();
     public void setupGUI() {
          Container c = getContentPane();
          drawPane.addMouseListener(pointer);
          drawPane.addMouseMotionListener(motion);
          c.setLayout(new BorderLayout());
          red.addActionListener(btn);
          green.addActionListener(btn);
          blue.addActionListener(btn);
          black.addActionListener(btn);
          erase.addActionListener(btn);
          buttonPane.setLayout(new GridLayout(6,1));
          buttonPane.add(red);
          buttonPane.add(green);
          buttonPane.add(blue);
          buttonPane.add(black);
          buttonPane.add(erase);
          buttonPane.add(colourPane);
          //drawPane.setDoubleBuffered(true);
          colourPane.setDoubleBuffered(true);
          colourPane.setBackground(Color.BLACK);
          scrollPane.setLayout(new GridLayout(3,1));
          redBar.addAdjustmentListener(scrl);
          greenBar.addAdjustmentListener(scrl);
          blueBar.addAdjustmentListener(scrl);
          redBar.setBackground(Color.RED);
          greenBar.setBackground(Color.GREEN);
          blueBar.setBackground(Color.BLUE);
          scrollPane.add(redBar);
          scrollPane.add(greenBar);
          scrollPane.add(blueBar);
          labelPane.setLayout(new GridLayout(3,1));
          labelPane.add(redLabel);
          labelPane.add(greenLabel);
          labelPane.add(blueLabel);
          southPane.setLayout(new BorderLayout());
          southPane.add("Center", scrollPane);
          southPane.add("East", labelPane);
          c.add("East", buttonPane);
          c.add("Center", drawPane);
          c.add("South", southPane);
          drawPane.setBackground(Color.WHITE);
          setSize(400,450);
          setVisible(true);
          setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     /*public void paint(Grapgics g) {
     class ButtonListener implements ActionListener {
          public void actionPerformed(ActionEvent e) {
               if(e.getSource() == red) {
                    redBar.setValue(255);
                    greenBar.setValue(0);
                    blueBar.setValue(0);
               else if(e.getSource() == green) {
                    redBar.setValue(0);
                    greenBar.setValue(255);
                    blueBar.setValue(0);
               else if(e.getSource() == blue) {
                    redBar.setValue(0);
                    greenBar.setValue(0);
                    blueBar.setValue(255);
               else if(e.getSource() == black) {
                    redBar.setValue(0);
                    greenBar.setValue(0);
                    blueBar.setValue(0);
               else if(e.getSource() == erase) {
                    redBar.setValue(255);
                    greenBar.setValue(255);
                    blueBar.setValue(255);
               repaint();
     class ScrollListener implements AdjustmentListener {
          int r = 0;
          int g = 0;
          int b = 0;
          public void adjustmentValueChanged(AdjustmentEvent e) {
               r = redBar.getValue();
               g = greenBar.getValue();
               b = blueBar.getValue();
               colour = new Color(r,g,b);
               colourPane.setBackground(colour);
     class PointerListener implements MouseListener{
          public void mouseClicked(MouseEvent e) {
          public void mouseEntered(MouseEvent e) {
          public void mouseExited(MouseEvent e) {
          public void mousePressed(MouseEvent e) {
               oldX = e.getX();
               oldY = e.getY();
          public void mouseReleased(MouseEvent e) {
     class MotionListener implements MouseMotionListener {
          public void mouseDragged(MouseEvent e) {
               newX = e.getX();
               newY = e.getY();
               Graphics g = drawPane.getGraphics();
               g.setColor(colour);
               g.drawLine(oldX,oldY,newX,newY);
               oldX = newX;
               oldY = newY;
               repaint();
          public void mouseMoved(MouseEvent e) {
     public static void main(String[] args) {
          new RevisedColourGUI();
}Do i need a super.paint(g) somwhere?
Many thanks
Will

There are three problems here. First off, you're reassigning oldX and oldY in the mouseDragged() method. This method is called every time the mouse is moved while having a button down - not the behaviour we want (I assume we just want to draw a straight line between (oldX, oldY) and (newX, newY). This can be fixed by simply deleting the appropriate lines (i.e. "oldX = newX;" and "oldY = newY;") in the mouseDragged method.
Once you do this and run it, you'll find you have a second problem - the line disappears as soon as you stop moving the mouse. Again in your mouseDragged method, you're calling repaint(), which is a method in the JFrame class and actually repaints the entire frame from scratch - again, not what we want. Instead, all you need to do is erase the line by replacing repaint with a call to fillRect() that paints over the panel in the background colour, before painting the new line. By now, your mouseDragged() method should look something like:
public void mouseDragged(MouseEvent e) {
newX = e.getX();
newY = e.getY();
Graphics g = drawPane.getGraphics();
// get rid of the previous line...
g.setColor(Color.white);
g.fillRect(0, 0, drawPane.getWidth(), drawPane.getHeight());
// ...and draw the new one
g.setColor(colour);
g.drawLine(oldX,oldY,newX,newY);
}Now the third problem is that your line disappears when you press one of the colour buttons. This is because in the actionPerformed() method of your ButtonListener class, you are again repainting the entire frame. By replacing the call to repaint() with the following:
Graphics g = drawPane.getGraphics();
g.setColor(Color.white);
g.fillRect(0, 0, drawPane.getWidth(), drawPane.getHeight());
g.setColor(colour);
g.drawLine(oldX,oldY,newX,newY);You'll have fixed the problem. Better still, put these lines of code into a private method and call it from the actionPerformed() and mouseDragged() methods.

Similar Messages

Maybe you are looking for

  • ** SOAP to SOAP call - HTTP 404 not found - invalid content type

    Hi friends, We have one scenario SOAP to SOAP call. It is synchronous interface. We are calling the WSDL. For some of the messages (not all), we are getting the error in SXMB_MONI com.sap.engine.interfaces.messaging.api.exception.MessagingException:

  • Router problem...

    hey i recently bought a linksys router which works...but the internet comes and goes. But im still connected to online games and messeging applications. But i cant go on any websites. i usally plug and unplug the router and i can go on the web again

  • Tiger Classic-ified the OS9 on the other partition.

    Hi, I recently bought this G4 QS and a 5-license DVD of Tiger (black disk with a silver 'X'), both second-hand. Being a newbie to OS X I also bought about 9 bookshelf inches of Tiger books. I fitted the G4 with a new 140 Gb hard drive and partitioned

  • Desktop/Finder

    I think people have asked about this topic, but I didn't see anything specifically relevant to my problem. I have had the desktop icons disappear, as other people have, but I can't open Finder to work out the problem either. Nor can I open any progra

  • Usb automount stopped working

    Hi I'm using udev to automount usb devices, with the code from the wiki. Everything worked so far, but today it suddenly stopped. I have 2 usbsticks, both were working. Now I wanted to make a bootable usb stick with new firmware for my intel hard dri