Jean Valjean
Jean Valjean

Reputation: 736

Java: Overriding methods from non inherited classes

How do/can you override methods from non-inherited class? Secondly is there a better term than "non-inherited classes"?

I have a class that "extends" JFrame and needs to override paintComponent from JPanel. How? Or it can extend JPanel and needs to access methods like setTitle(), setResizable(), and setDefaultCloseOperation();

In response to the latest answer:

i've done this:

public class Chess extends JPanel {

boolean du, dd, dl, dr;
double x, y;
public class AL extends KeyAdapter {

    public void keyPressed(KeyEvent e) {
        int keyC = e.getKeyCode();
        switch(keyC) {
            case KeyEvent.VK_LEFT:
                dl = true;
                break;
            case KeyEvent.VK_RIGHT:
                dr = true;
                break;
            case KeyEvent.VK_DOWN:
                dd = true;
                break;
            case KeyEvent.VK_UP:
                du = true;
                break;
        }

    }
    public void keyReleased(KeyEvent e) {
         int keyC = e.getKeyCode();
        switch(keyC) {
            case KeyEvent.VK_LEFT:
                dl = false;
                break;
            case KeyEvent.VK_RIGHT:
                dr = false;
                break;
            case KeyEvent.VK_DOWN:
                dd = false;
                break;
            case KeyEvent.VK_UP:
                du = false;
                break;
        }
    }
}

public void Chess() {
    JFrame frame = new JFrame();
    frame.addKeyListener(new AL());
    frame.setTitle("Chess");
    frame.setSize(500, 500);
    frame.setResizable(false);
    frame.setVisible(true);

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    du = dl = dd = dr = false;
    x = y = 150;

}

public void paintComponent(Graphics g) {

    super.paintComponent(g);

    setBackground(Color.CYAN);


    double i = .25;
    if (du) {
        y -= i;
    }
    if (dr) {
        x += i;
    }
    if (dd) {
        y += i;
    }
    if (dl) {
        x -= i;
    }

    if (x < 0) {
        x = 0;
    }
    if (x > getWidth() - 25) {
        x = getWidth() - 25;
    }
    if (y < 25) {
        y = 25;
    }
    if (y > getHeight() - 25) {
        y = getHeight() - 25;
    }

    g.drawOval( (int) x, (int) y, 25, 25);
    repaint();

}
public static void main(String[] args) {
    new Chess();
}

}

It should move the oval around the screen (I'm trying to get the basics down) but when i run it, a box doesn't appear. It worked when was using jFrame and overrode paint and called a non-overridden paintComponent() from paint(). not what i want to do. Why does a box no longer appear?

Upvotes: 1

Views: 1189

Answers (1)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285405

  1. You're allowed to use more than one class when creating Java programs.
  2. So you can have one class extend JFrame
  3. And another extend JPanel -- and have that class's paintComponent method overridden.
  4. Having said that, note that you may be painting yourself in a corner by having your class extend JFrame, forcing you to create and display JFrames, when often more flexibility is called for. In fact, I would venture that most of the Swing GUI code that I've created and that I've seen does not extend JFrame, and in fact it is rare that you'll ever want to do this. More commonly your GUI classes will be geared towards creating JPanels, which can then be placed into JFrames or JDialogs, or JTabbedPanes, or swapped via CardLayouts, wherever needed. This will greatly increase the flexibility of your GUI coding.

For example, here's a simple class that displays mouse position on mouse click:

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.*;

@SuppressWarnings("serial")
public class MousePosition extends JPanel {
    private static final int PREF_W = 600;
    private static final int PREF_H = PREF_W;
    // format String for display String
    protected static final String FORMAT = "(%d, %d)";
    private int xPos = -40;
    private int yPos = -40;
    private String displayText = "";

    public MousePosition() {
        addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                xPos = e.getX();
                yPos = e.getY();
                // use FORMAT String to create our display text
                displayText = String.format(FORMAT, xPos, yPos);
                repaint();
            }
        });
    }

    @Override
    public Dimension getPreferredSize() {
        if (isPreferredSizeSet()) {
            return super.getPreferredSize();
        }
        return new Dimension(PREF_W, PREF_H);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawString(displayText, xPos, yPos);

    }

    private static void createAndShowGui() {
        MousePosition mainPanel = new MousePosition();

        JFrame frame = new JFrame("MousePosition");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.getContentPane().add(mainPanel);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGui();
            }
        });
    }
}

Regarding

I have a class that "extends" JFrame and needs to override paintComponent from JPanel. How?

As noted above. Note that I avoid setting sizes but instead override the JPanel's getPreferredSize() if I want to force it to a certain size. Then the layout managers do the rest when I call pack() on the JFrame.

Or it can extend JPanel and needs to access methods like setTitle(), setResizable(), and setDefaultCloseOperation()...

Those methods can be simply called on the JFrame instance after you've created it as per my example above.

Upvotes: 4

Related Questions