AJ P.
AJ P.

Reputation: 1

Eclipse program JPanel wont run

I'm not sure why but when I try to run this the only thing that shows is the last program I ran, I even added the serialVersionUID and it still wont show. Anyone have an i dea on why, maybe some setup problem on my eclipse or coomputer?

import java.awt.Graphics;
import javax.swing.JPanel;

public class Shapes extends JPanel {
    private static final long serialVersionUID = 1L;
    private int choice;

    public Shapes(int userChoice) {
        choice = userChoice;
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        for (int i = 0; 1 < 10; i++) {
            switch (choice) {
            case 1:
                g.drawRect(10 + i * 10, 10 + i * 10, 50 + i * 10, 50 + i * 10);
            case 2:
                g.drawOval(10 + i * 10, 10 + i * 10, 50 + i * 10, 50 + i * 10);
                break;
            }
        }
    }
}

Upvotes: 0

Views: 525

Answers (1)

user5549921
user5549921

Reputation:

First of all, you have no entry-point to your program. You can't just expect a program in Java, or basically any other compiled language to run without an entry point.

Inside your class, you need to have the following for the program to begin:

public static void main(String[] args) {
    // Entry-point began. Put code in here.
}

Secondly, it is not a good practise (correct me if I'm wrong) to extend any of the JComponent derivatives. Without explaining again, a good reason can be found here:

Excerpt:

  • It makes it harder to change things later - if you've made a class public, swapping the superclass is going to break subclasses - it's a choice which, once you've made the code public, you're married to. So if you're not altering the real functionality to your superclass, you get much more freedom to change things later if you use, rather than extend the thing you need. Take, for example, subclassing JPanel - this is usually wrong; and if the subclass is public somewhere, you never get a chance to revisit that decision. If it's accessed as JComponent getThePanel() , you can still do it (hint: expose models for the components within as your API). This especially applies because your class is a Shape. So when Shape extends JPanel, every single shape will.
  • Object hierarchies don't scale (or making them scale later is much harder than planning ahead) - this is the classic "too many layers" problem. I'll go into this below, and how the AskTheOracle pattern can solve it (though it may offend OOP purists).

Especially since you are extending JPanel, and aren't even initialising a JFrame to begin with. Before starting out with Swing applications in Java, you should read the JavaDoc and Oracles examples beforehand...

JavaDoc: JFrame / JPanel
Example: How to make Frames.

So, revising on what I have said, a basic Swing class would look as follows:

import javax.swing.*;

public class Shapes {

    public Shapes() {
        JFrame frame = new JFrame();
        JPanel window = new JPanel();

        frame.setSize(600, 400); // Width: 600, Height: 400.
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        frame.add(window);

        window.setLayout(new BorderLayout());

        // Perform graphics drawing.

        frame.revalidate();
        frame.repaint();
    }

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

}

Upvotes: 1

Related Questions