Frage

For my CS11 class, I have to write a small applet that draws a horizontal and vertical line on the screen that intersect in the middle of the canvas. One part of the assignment is to be able to move one or both lines when the user clicks on a line and performs a drag. That part is working.

The second part involves handling window resizing. If the window gets resized, the two lines need to be adjusted so they extend from edge to edge of the window.

Our instructions want us to use the Java appletviewer instead of a web browser to launch the program so that the window can be resized while the program is running. We are told to use the following .html file.

ResizableBallController.html

<html>
  <body>
    <applet
      code="ResizableBallController.class"
      archive="objectdraw.jar"
      width="500"
      height="500">
    </applet>
  </body>
</html>

When I run the program and resize the window the canvas area stays fixed at 500x500 pixels, even as I make the window bigger, and the enlarged area is filled with gray. According to my instructor, the canvas should automatically adjust to extend from edge to edge of the newly resized window.

To test the issue, I am overloading the paint method to add a System.out.println message so I can see how often it is being called and it appears the only time the method is invoked is when the program is launched.

I'm wondering if this could have something to do with Awesome Window Manager or perhaps something else. I say this because the instructor said when you resize the window, the canvas should automatically resize as well.

Here is a portion of the code so you can see what I'm doing:

import objectdraw.*;
import java.awt.*;

public class ResizableBallController extends WindowController
{
  // Canvas separators
  private Line xAxis,
               yAxis;

 boolean xAxisGrabbed,
          yAxisGrabbed;

  public void begin()
  {
    int canvasWidth              = canvas.getWidth(),
        canvasHeight             = canvas.getHeight(),
        canvasHorizontalMidPoint = canvasWidth / 2,
        canvasVerticalMidpoint   = canvasHeight / 2;

    xAxis = new Line(0, canvasVerticalMidpoint,
                     canvasWidth, canvasVerticalMidpoint,
                     canvas);

    yAxis = new Line(canvasHorizontalMidPoint, 0,
                     canvasHorizontalMidPoint, canvasHeight,
                     canvas);
  }

  public void onMousePress(Location p)
  {
    xAxisGrabbed = xAxis.contains(p);
    yAxisGrabbed = yAxis.contains(p);
  }

  public void onMouseDrag(Location p)
  {
    if (xAxisGrabbed)
    {
      xAxis.moveTo(0, p.getY());
    }

    if (yAxisGrabbed)
    {
      yAxis.moveTo(p.getX(), 0);
    }
  }

  public void paint(java.awt.Graphics g)
  {
    super.paint(g);
    System.out.println("paint called");
  }
} // End of public class ResizableBallController extends WindowController
War es hilfreich?

Lösung

Very strange, but the Java JDK seems to have problems with some window managers. A solution was accidentally found. JFrame's position and pack() in Awesome WM

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top