Slow drawing

Discussion in 'Java' started by AHM, May 12, 2004.

  1. AHM

    AHM Guest

    Hi

    I've got a problem with the code below and I'm hoping someone can help
    me optimize it. What I want is to draw a horizontal line where the mouse
    cursor is located in the panel. Unfortunately the drawing is a bit slow
    resulting in the line being a little delayed as to where the mouse
    cursor is. Can anyone help me sort this out?

    [TestApplet.java]

    import java.applet.Applet;
    import java.awt.*;

    public class TestApplet extends Applet {
    TestPanel testPanel = new TestPanel();

    public void init() {
    this.setLayout(new BorderLayout());
    add(testPanel, BorderLayout.CENTER);
    }
    }

    [TestPanel.java]

    import java.awt.*;
    import java.awt.event.MouseMotionListener;
    import java.awt.event.MouseEvent;

    public class TestPanel extends Panel implements MouseMotionListener {
    Graphics graphics;
    Image image;
    Dimension dimension;

    int currentMousePositionY;

    TestPanel() {
    this.addMouseMotionListener(this);
    this.setBackground(Color.white);
    }

    public Dimension getPreferredSize() {
    return new Dimension(250, 300);
    }
     
    AHM, May 12, 2004
    #1
    1. Advertising

  2. AHM

    VisionSet Guest

    "AHM" <> wrote in message
    news:...
    > Hi
    >
    > I've got a problem with the code below and I'm hoping someone can help
    > me optimize it. What I want is to draw a horizontal line where the mouse
    > cursor is located in the panel. Unfortunately the drawing is a bit slow
    > resulting in the line being a little delayed as to where the mouse
    > cursor is. Can anyone help me sort this out?
    >
    > [TestApplet.java]
    >
    > import java.applet.Applet;
    > import java.awt.*;
    >
    > public class TestApplet extends Applet {
    > TestPanel testPanel = new TestPanel();
    >
    > public void init() {
    > this.setLayout(new BorderLayout());
    > add(testPanel, BorderLayout.CENTER);
    > }
    > }
    >
    > [TestPanel.java]
    >
    > import java.awt.*;
    > import java.awt.event.MouseMotionListener;
    > import java.awt.event.MouseEvent;
    >
    > public class TestPanel extends Panel implements MouseMotionListener {
    > Graphics graphics;
    > Image image;
    > Dimension dimension;
    >
    > int currentMousePositionY;
    >
    > TestPanel() {
    > this.addMouseMotionListener(this);
    > this.setBackground(Color.white);
    > }
    >
    > public Dimension getPreferredSize() {
    > return new Dimension(250, 300);
    > }


    optimise it... or write it?
    I see no drawing code or listener code.

    But you aren't likely to need Graphics as an attribute.

    --
    Mike W
     
    VisionSet, May 12, 2004
    #2
    1. Advertising

  3. AHM wrote:

    > Hi
    >
    > I've got a problem with the code below and I'm hoping someone can help
    > me optimize it. What I want is to draw a horizontal line where the mouse
    > cursor is located in the panel. Unfortunately the drawing is a bit slow
    > resulting in the line being a little delayed as to where the mouse
    > cursor is. Can anyone help me sort this out?
    >
    > [TestApplet.java]
    >
    > import java.applet.Applet;
    > import java.awt.*;
    >
    > public class TestApplet extends Applet {
    > TestPanel testPanel = new TestPanel();
    >
    > public void init() {
    > this.setLayout(new BorderLayout());
    > add(testPanel, BorderLayout.CENTER);
    > }
    > }
    >
    > [TestPanel.java]
    >
    > import java.awt.*;
    > import java.awt.event.MouseMotionListener;
    > import java.awt.event.MouseEvent;
    >
    > public class TestPanel extends Panel implements MouseMotionListener {
    > Graphics graphics;
    > Image image;
    > Dimension dimension;
    >
    > int currentMousePositionY;
    >
    > TestPanel() {
    > this.addMouseMotionListener(this);
    > this.setBackground(Color.white);
    > }
    >
    > public Dimension getPreferredSize() {
    > return new Dimension(250, 300);
    > }


    Your code was cut off (both times).

    If you're subclassing Panel then it makes more sense to hook the
    processMouseMotionEvent method to do your drawing than to register
    objects as listeners on themselves. You might find it a little faster
    (but don't forget to invoke the superclass' version either at the
    beginning or at the end). I suspect the real problem is with the actual
    drawing code, however, and that didn't make it here.


    John Bollinger
     
    John C. Bollinger, May 12, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    651
    Kevin Spencer
    Jan 9, 2006
  2. Replies:
    3
    Views:
    3,047
  3. AHM

    Slow drawing

    AHM, May 12, 2004, in forum: Java
    Replies:
    0
    Views:
    387
  4. phil

    Canvas drawing slow

    phil, May 2, 2005, in forum: Python
    Replies:
    0
    Views:
    325
  5. defn noob
    Replies:
    1
    Views:
    406
    Mark Space
    Jun 28, 2008
Loading...

Share This Page