1

I'm new to Java and teaching myself and trying to build this program. It will create dots when the mouse clicks, but won't draw a line when the button is pushed. (eventually the line will connect the dots, but I'm not there yet.) First I just need to get it to draw something when pushed and work from there. I've tried multiple things and I can't get it to work. Here is my code:

public void init()
{
    LineDrawListener listener = new LineDrawListener ();
    addMouseListener (listener);

    Button lineGraph = new Button("Graph Line");
    lineGraph.addActionListener (this);
    add (lineGraph, BorderLayout.EAST);

    setBackground (Color.yellow);
    setSize (APPLET_WIDTH, APPLET_HEIGHT);
}

public void paint(Graphics g)
{
    // draws dots on screen
    g.setColor(Color.blue);
    if (current != null)
    {
        xcoordinates.addElement (current.x);
        ycoordinates.addElement (current.y);
        count++;
        g.fillOval (current.x-4, current.y-4, 10, 10);
        repaint();
    }
    if (push == true)
    {
        g.drawLine (5, 5, 30 , 30);
        repaint();
    }
}

class LineDrawListener extends MouseAdapter
{
    public void mouseClicked (MouseEvent event)
   {
       current = event.getPoint();

    repaint();
}

}
public void actionPerformed (ActionEvent event)
{
    Object action = event.getSource();
    if(action==lineGraph)
    {
        push = true;
    }
}

Any help on how to get the button to work would be much appreciated. Thanks in advance.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • 2
    1) For better help sooner, post an [SSCCE](http://sscce.org/). 2) Please don't forget to add a '?' to questions! Some people do a search in the page for '?' and if none exists in the 'question' go directly to the next (actual) question in line. 3) Please use code formatting for code, input/output & structured documents like HTML or XML. To do that, select the sample and click the `{}` button above the messaged posting/editing form. – Andrew Thompson Dec 31 '13 at 17:54
  • 1
    Wrong tags. This is awt, not swing, and it is button, not jbutton. – MultiplyByZer0 Dec 31 '13 at 17:59
  • 2
    `public void paint(Graphics g) { ... repaint();` Will cause an infinite loop. Whatever resources you are using to learn programming, get rid of them and go direct to the Java Tutorial. As best I can figure, you need to look especially at [Performing Custom Painting](http://docs.oracle.com/javase/tutorial/uiswing/painting/).. – Andrew Thompson Dec 31 '13 at 17:59
  • 1
    `setSize (APPLET_WIDTH, APPLET_HEIGHT);` Don't do that either. An applet's size is set in the HTML that loads it. – Andrew Thompson Dec 31 '13 at 18:00
  • BTW - 1) Why code an applet? If it is due to spec. by teacher, please refer them to [Why CS teachers should stop teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) Why AWT rather than Swing? See my answer on [Swing extras over AWT](http://stackoverflow.com/a/6255978/418556) for many good reasons to abandon using AWT components. If you need to support older AWT based APIs, see [Mixing Heavyweight and Lightweight Components](http://www.oracle.com/technetwork/articles/java/mixing-components-433992.html). – Andrew Thompson Dec 31 '13 at 18:00

3 Answers3

3
  1. Don't paint on top-level containers like JApplet
  2. Rather paint on a JPanel and override the paintComponent method and call super.paintComponet(g)

    @Override
    protected void paintComponent(Graphic g){
        super.paintComponent(g);
        ...
    }
    
  3. Don't call repaint() from inside the paint() method. Call it in your actionPerformed()

  4. Learn to post an SSCCE

Number 3 is your most dire problem

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • @MistressDavid do you have sources to make a valid argument for that? I'd truly like to see them. Not saying you're wrong, but I would really to like see some valid sources – Paul Samsotha Dec 31 '13 at 18:03
  • 1
    @MistressDavid those _aren't_ valid sources. That's a Google search. And a couple of those results lead to a question that I asked myself about the benefits, which was never clearly answered. So if you have a valid argument I suggest you attempt to answer [this question](http://stackoverflow.com/questions/20787800/what-are-the-benefits-to-painting-on-a-jpanel-vs-jcomponent). And for future reference, if someone asks you for a valid source, don't link them a search query. It just makes you look like you don't know what you're talking about. – Paul Samsotha Dec 31 '13 at 18:11
  • 1
    @MistressDavid, that is not an answer. One of those thousands of links points to the Swing tutorial on Custom Painting and those examples do extend JPanel. I'm not saying all the Swing tutorials are 100% perfect, but they are a good place to start unless you know of some specific reasons for not doing so. – camickr Dec 31 '13 at 18:14
3

Don't override paint(). Don't invoke repaint() in a painting method, this can cause in infinite loop.

Check out Custom Painting Approaches for working examples of the two common ways to do custom paintint:

  1. by using a List to track the objects to be painted
  2. by painting to a BufferedImage
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
camickr
  • 321,443
  • 19
  • 166
  • 288
3

The reason you can not get the line to draw is that repaint() posts a request to repaint the component. You are using it as if it refreshes the view somehow. You need to change three things in your code, all in paint():

  1. Do not call repaint() in paint()
  2. Override paintComponent() rather than paint() so your borders get drawn if you should ever have them at a later time.
  3. Call super.paintComponent() in paintComponent() to do some initialization for you. It is probably OK not to for something this simple, but good practice for the future.

Here is what the code would look like:

public void paintComponent(Graphics g)
{
    super.paintComponent(g);

    // draws dots on screen
    g.setColor(Color.blue);
    if (current != null)
    {
        xcoordinates.addElement (current.x);
        ycoordinates.addElement (current.y);
        count++;
        g.fillOval (current.x-4, current.y-4, 10, 10);
    }
    if (push == true)
    {
        g.drawLine (5, 5, 30 , 30);
    }
}
Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
  • Applets don't have a `paintComponent` method. You can tell its an Applet form the `init()` method. you would need to subclass `JPanel` or `JComponent` to use that method. No top-level containers have a `paintComponent` method – Paul Samsotha Dec 31 '13 at 19:19