Sonius
Sonius

Reputation: 1657

I have a code. It works fine till I put an extra method in it

I have my code with two classes. If I start the code, I get an Image. The Image has that long a white bar on the down till I have the two last methods getHeight and getWidth.

Now my question: Why everything works fine without these two methods? I was told that I should have these two API methods to to test later with JUnit.

Sry, for my bad english ;)

package mydraw;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;


public class DrawImageMini {

public static void main(String[] args) throws ColorException {new DrawImageMini();}

/** Application constructor:  create an instance of our GUI class */
public DrawImageMini() throws ColorException { window = new DrawMiniGUI(this); }

protected JFrame window;
}

class DrawMiniGUI extends JFrame {
DrawImageMini app;
Container       cp;
NavigationPanel navigationPanel;
JPanel          drawPanel;

/**
 * The GUI constructor does all the work of creating the GUI and setting
 * up event listeners.  Note the use of local and anonymous classes.
 */
public DrawMiniGUI(DrawImageMini application) throws ColorException {
    super("Draw");        // Create the window
    app = application;    // Remember the application reference

    // selector for drawing modes
    JComboBox shape_chooser = new JComboBox();
    shape_chooser.addItem("Scribble");
    shape_chooser.addItem("Rectangle");
    shape_chooser.addItem("Oval");

    // selector for drawing colors
    JComboBox color_chooser = new JComboBox();
    color_chooser.addItem("Black");
    color_chooser.addItem("Blue");
    color_chooser.addItem("Red");
    color_chooser.addItem("Green");

    // Create two buttons
    JButton clear = new JButton("Clear");
    JButton quit = new JButton("Quit");

    // Set a LayoutManager, and add the choosers and buttons to the window.
    cp = this.getContentPane();
    cp.setLayout(new BorderLayout());

    // Setzt einen Panel, die Buttons in einer Leiste hat.
    navigationPanel = new NavigationPanel(new FlowLayout());
    navigationPanel.add(new JLabel("Shape:"));
    navigationPanel.add(shape_chooser);
    navigationPanel.add(new JLabel("Color:"));
    navigationPanel.add(color_chooser);
    navigationPanel.add(quit);
    navigationPanel.add(clear);
    navigationPanel.setBackground(Color.magenta);

    // Setzt den Panel, auf dem gemalt wird
    drawPanel = new JPanel();

    cp.add(navigationPanel, BorderLayout.NORTH, 0);
    cp.add(drawPanel, BorderLayout.CENTER, 1);

    // Handle the window close request similarly
    this.addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            app.window.dispose();
            System.exit(0);
        }
    });

    // Finally, set the size of the window, and pop it up
    drawPanel.setPreferredSize(new Dimension(600, 600));
    this.pack();
    drawPanel.setBackground(Color.red);
    this.setVisible(true);
}
public int getHeight(){
    return drawPanel.getHeight();
}

public int getWidth(){
    return drawPanel.getWidth();
}
}

Upvotes: 1

Views: 56

Answers (1)

ControlAltDel
ControlAltDel

Reputation: 35096

getWidth and getHeight are already defined in Component, a super class of JFrame. You should not override these methods. Rather, you should name your methods something different

Upvotes: 7

Related Questions