开发者

Using a variable with paintComponent

开发者 https://www.devze.com 2023-03-17 00:41 出处:网络
For a homework assignment, I am trying to paint a box with paintComponent using user inputed variables. I have been able to build what I am need to do using fixed numbers. I have been working on this

For a homework assignment, I am trying to paint a box with paintComponent using user inputed variables. I have been able to build what I am need to do using fixed numbers. I have been working on this all day and have not been able to find a way to implement variables. Here is a stripped down version of what I am working on:

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

public class Problem3 extends JFrame{

  public static void main(String[] args) {  
    int xCoord = Integer.parseInt(
            JOptionPane.showInputDialog("Enter an X cord."));

    JFrame gd = new JFrame();
    gd.setLocationRelativeTo(null);
    gd.setSize(300, 300);
    gd.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    gd.setVisible(true);

    gd.add(new NewPanel());   
  }

  public int getX(){
    return xCoord;
  }
}

class NewPanel extends JPanel {

  int xCoord = getX();

  protected void paintComponent(Graphics g){
    super.paintComponent(g);

    g.drawRect(10,10,xCoord,50);
  }
}

EDIT

Since posting here I have tried using a setter method inside the NewPanel class

public void setX() {
    xCoord = Integer.parseInt(
            JOptionPane.showInputDialog("Enter an X cord."));
}

Running this lead to a strange error I have never seen before: I get a StackOverFlow error and then the dialog box pops up multiple/hundreds of times and won't stop popping up.

EDIT 3 Using Hovercraft Full Of Eels solution, I got it to work! Thank you Full Of Eels for your time, help, and patience.

public class Problem3 extends JFrame{
    public static void main(String[] args) {  

        JFrame gd = new JFrame();
        gd.setLocationRelativeTo(null);
        gd.setSize(300, 300);
        gd.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        gd.setVisible(true);

        NewPanel panel = new NewPanel();

        panel.setX(50); 
        gd.add(panel);
    } 
}
class NewPanel extends JPanel {

    int xCoord;
开发者_StackOverflow中文版
    public void setX(int x){
        xCoord = x;
    }

    public int getX(){
        return xCoord;
    }

    protected void paintComponent(Graphics g){
        super.paintComponent(g);

        g.drawRect(10,10,xCoord,50);
    }

}


Just like more basic Java classes you often use mutator and accessor methods, also known as setters and getters, to change the state of an object. In your situation you need to give the NewPanel class a setter method, public void setXCoor(int x) {...} and perhaps a similar one for a yCoord if need be so that outside classes (the GUI that holds the NewPanel object) can change the NewPanel's values. Once the xCoord has been changed, you'll want to call repaint() on the same NewPanel object so that its paintComponent method can be called by the JVM displaying the effects of the changed xCoord value.

Edit 1
Also a question: why does your Problem3 class extend JFrame? That seems unnecessary here.

Edit 2
Also, I would not do this code in a main method:

int xCoord = Integer.parseInt(
        JOptionPane.showInputDialog("Enter an X cord."));

JFrame gd = new JFrame();
gd.setLocationRelativeTo(null);
gd.setSize(300, 300);
gd.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gd.setVisible(true);

gd.add(new NewPanel()); 

But rather in a constructor of a full-fledged class.

Also, I'd give that class a NewPanel class field, and I'd put the object held by that field into the JFrame allowing me to have a reference to the NewPanel object so I can call it's methods elsewhere in the class.

Edit 3
Regarding this code:

public void setX() {
    xCoord = Integer.parseInt(
            JOptionPane.showInputDialog("Enter an X cord."));
}

I wouldn't do user input in the drawing class. Instead I'd the setter method a true setter method, similar to ones I'm sure you've made many times before and in this form:

public void setX(int xCoord) {
   // set your field in here like you always do
}

Then do your user interaction, be it a JOptionPane or whatever in the main GUI or elsewhere in your program. Once the user gives his input, then call the setter method above on your NewPanel variable passing in the user's input (as an int of course).

Edit 4

Regarding this code:

public static void main(String[] args) {
    // ....
    gd.add(new NewPanel());   

    NewPanel.setX(50); 
}

You're calling the setX method on the NewPanel class, not on a NewPanel object, which is why the compiler is appropriately complaining. This is why I suggested in one of my edits above to create a NewPanel variable in your GUI class, to create your GUI in the GUI class's constructor, not in the main method (which should mainly call the GUI class's constructor), and to use the same NewPanel variable to place into the GUI's JFrame and to call the setX(...) method on.


import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;


public class BerksSnakeMain extends JPanel{
    int xCoord;

    public void berk() {
        xCoord = Integer.parseInt(
            JOptionPane.showInputDialog("Enter an X cord."));
    }

    public void paintComponent(Graphics g){
    super.paintComponent(g);

    g.setColor(Color.red);
    g.fillOval(xCoord,10,10,10);
  }



    public static void main(String[] args) {

        BerksSnakeMain ad = new BerksSnakeMain();
        ad.berk();
        JFrame frame = new JFrame();
        frame.setSize(800, 600);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        frame.add(ad);

    }

}
0

精彩评论

暂无评论...
验证码 换一张
取 消