Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
251 views
in Technique[技术] by (71.8m points)

java - Passing Graphics g through method call

I'm a beginner to java so I was wondering if I could get some help with passing the Graphics g. I'm asking the user to pick one of the options using the JOptionPane, and if they pick the first option, I want to call on the "Letter" class I made and then call on the method "drawLetter" which will essentially draw the letter "A". However, in main, I'm not sure how to call upon the "drawLetter" method itself. Thanks in advance! :)

//Main class
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class AlphabetLetter {


public static void main(String[] args) {
    String [] buttons = {"Default", "Choose Letter", "Choose Colour", "Quit"};
    
     int option = JOptionPane.showOptionDialog(null,  "Choose your option:", "ALPHABET DRAWING", JOptionPane.INFORMATION_MESSAGE, 0, null, buttons, null);
     
     Letter letter = new Letter();
     
     
     if (option == 0)
     {
        **//I want to call on the drawLetter method here but I'm not sure what to pass through the parameters**
        letter.drawLetter(g2);
     }
}

}

 //letter class
 import java.awt.Color;
 import java.awt.Graphics2D;
 import javax.swing.JFrame;

 public class Letter extends JFrame{


public Letter()
{
    setTitle("Alphabet Drawing");
    setSize(500, 500);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setResizable(false);
    setLocationRelativeTo(null);
    setVisible(true);
}


public void drawLetter (Graphics2D g)
{
    super.paint(g);
    
    Graphics2D g2 = (Graphics2D) g;
    
    g2.drawString("A", 100, 100);
    
    
}

}


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Note : This answers has been edited as it was showing a bad way on how the painting in Java Swing is done.

As pointed by camickr, you don't need to pass the Graphics Object to your drawLetter() function as your don't even need a drawLetter() function.

All you need to do is to create a JLabel, add it to the frame and use the setText(String) function from your JLabel Object.

So your code should look like this :

  • AlphabetLetter class :
//Main class
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class AlphabetLetter {

    public static void main(String[] args) {
        String [] buttons = {"Default", "Choose Letter", "Choose Colour", "Quit"};
    
        int option = JOptionPane.showOptionDialog(null,  "Choose your option:", "ALPHABET DRAWING", JOptionPane.INFORMATION_MESSAGE, 0, null, buttons, null);
     
        Letter letter = new Letter();
        JLabel label = new JLabel();
     
        //Add the Label to the JFrame
        letter.add(label);
        if (option == 0)
        {
            //Change the text of the label
            label.setText("A");
        }
    }
}
  • Letter class :
//letter class
import java.awt.Color;
import java.awt.Graphics2D;
import javax.swing.JFrame;

public class Letter extends JFrame{

    public Letter()
    {
        setTitle("Alphabet Drawing");
        setSize(500, 500);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setResizable(false);
        setLocationRelativeTo(null);
        setVisible(true);
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
...