简体   繁体   中英

JAVA, GUI JPanel, JFrame, paintComponent, Graphics

Regarding your comment to change add public display(Graphics g)

[link] http://www3.canyons.edu/Faculty/biblej/project6.html

1.)Project6 class will have to extend the JFrame class 2.)Project6 constructor will have to set up the GUI window. 3.)A new abstract method: public void display(Graphics g); should be added to the base and derived classes 4.)A custom JPanel must be set up with a paintComponent method 5.)The new display(Graphics g) method will have to draw the shapes on the GUI window and be called from a loop in the paintComponent method

public class Project6 extends JFrame { 

//project6 constructor without parameters to set up new JFrame
public Project6() {
add(new NewPanel());
}
class NewPanel extends JPanel {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);

//So do I need to add Graphics g here? or no?

for(int i = 0; i < thearray.length && thearray[i] != null; i++) {
thearray[i].display(**Graphics g**); 
}}}

public static void main (String [] args) {
JFrame frame = new JFrame();
frame.setSize(800, 700);                           
frame.setTitle("Shapes");
frame.setLocationRelativeTo(null);                 //Center Frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);

Here is one of classes for example, Do I add it to the end like this? and do I need to add a public abstract void display(Graphics g) to the Shape parent class? and how would it call in the project6 class?

public class Rectangle extends Shape {
private int width;
private int height;

public Rectangle() {
    setWidth(0);
    setHeight(0);
    setXPos(0);
    setYPos(0);}

public Rectangle(int xPos, int yPos, int height, int width) {
    setWidth(xPos);
    setHeight(yPos);
    setXPos(height);
    setYPos(width);}

public int getWidth() {
    return width;}

public void setWidth(int width) {
    this.width = width;}

public int getHeight() {
    return height;}

public void setHeight(int height) {
    this.height = height;}

@Override
public void display() {
    System.out.println("Rectangle: (" + getXPos() + ", " + getYPos() + ") " + " Height:  " + height + " Width: " + width);}

@Override
public void display(Graphics g) {
  g.drawRect(getXPos(), getYPos(), width, height); }

A new abstract method: public void display(Graphics g); should be added to the base and derived classes

You haven't done this step correctly because I notice that you are calling thearray[i].display(); when display is intended to have a parameter.

If you create the display method correctly, then you are handed the Graphics object that you can use, for example:

class Line extends Shape {
    int x1, y1, x2, y2;

    @Override
    public void display(Graphics g) {
        g.drawLine(x1, y1, x2, y2);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM