2

I'm new with Java and I'm trying to a class that extends JPanel. I have a updateView() method that is suppose to draw rounded rectangles with text in the middle inside the JPanel. Also, I would need to stock the textfield and the rounded rectangle in a LinkedHashMap. I already got a var for this, I just don't know witch type of var I need to use.

I'have been searching the solution for a while, and all the answers I found are either to complicated for me to understand or just doesn't apply to my case.

I know that it's almost nothing, but here is what I got so far...

package game;

import javax.swing.JPanel;

public class GameNumView extends JPanel
{

    private Map<Integer,Integer> backgroundText = new LinkedHashMap<"My rounded rectangle","My textfield">();

    public GameNumView()
    {

    }

    public void UpdateView(String[] pNumbers)
    {
        //Create the background

        //Create the text

    }   
}
Kévin Duguay
  • 761
  • 3
  • 12
  • 29
  • What's wrong with what you have? If you're looking for implementation details, we'll need to know what library you are using to do the rendering in order to offer solutions. – Cypher Sep 18 '15 at 20:23
  • I just don't know where to from that point. What class should mine extends to make me able to just use the method add() in my JFrame class. How do you draw a rounded rectangle in such a class? I don't see how I could be more precise. – Kévin Duguay Sep 18 '15 at 20:29
  • Creating your own button from scratch is going to be difficult for you then. Not only do you have to render the button (which means learning how to draw in Java), but also intercepting user input to activate that button. Good luck. Start here: http://docs.oracle.com/javase/tutorial/2d/geometry/primitives.html – Cypher Sep 18 '15 at 20:32
  • Actually, I really just want to draw my button, but not detect when he click it or anything like that. I'm in a special situation where I just need my button to look like a button, but not act like one. It's really just a visual element, nothing more. – Kévin Duguay Sep 18 '15 at 20:34
  • 1
    Possible [example](http://stackoverflow.com/questions/13386749/how-to-add-to-this-round-button-metal-background-in-java/13389263#13389263) and [example](http://stackoverflow.com/questions/15846937/painting-a-particular-button-from-a-grid-of-buttons/15847188#15847188) – MadProgrammer Sep 18 '15 at 21:43

1 Answers1

2

I'll give you the right path. It's up to you to adapt it to your existing code.

The idea is to use the Graphics object with the paintComponent method of an extended class of JPanel.

Here is the code, it is clear enough I think but if you have any question, do not hesitate.

The MyFrame class :

public class MyFrame extends JFrame {
  public MyFrame(){             
    this.setTitle("Hello");
    this.setSize(200, 200);
    this.setLocationRelativeTo(null);               

    MyPanel pan = new MyPanel();
    pan.setBackground(Color.ORANGE);        
    this.setContentPane(pan);               
    this.setVisible(true);
  }      

  public static void main(String[] args) {
        MyFrame f = new MyFrame();
    }
}

And here is the MyPanel class :

public class MyPanel extends JPanel{

    @Override
    public void paintComponent(Graphics g) {
        g.setColor(Color.black);
        g.drawRoundRect(10, 10, this.getWidth()-20, this.getHeight()-20, 15, 15);
        g.setColor(Color.black);
        g.drawString("Hello", 75, 75);
    }

}

And here is a picture of what you should have :

My Fake Button !

Yassin Hajaj
  • 21,337
  • 9
  • 51
  • 89
  • Youve broken the paint chain, which is going to cause no end of issues. You should,generally, avoid extending from JFrame, it's locking you into a single use case and you're not really adding new functionality to the class., you should be start the ui from wiin the content of the EDT – MadProgrammer Sep 18 '15 at 21:40
  • I understand it takes basic knowledge to bring up this solution. I'm new to swing and have not began learning about EDT but I'll keep that in mind. Thanks for the advices. Have you got an idea about this problem? – Yassin Hajaj Sep 18 '15 at 21:42
  • Yes, I add a couple of links to previously similar questions in the comments – MadProgrammer Sep 18 '15 at 22:04
  • Thank you. Indeed, very interesting. – Yassin Hajaj Sep 18 '15 at 22:06
  • My situation just changed a bit. My class extends JPanel, and I have a updateView() metod witch while draw some rectangles inside my JPanel. How do I do the same thing you did, but inside that function. – Kévin Duguay Sep 19 '15 at 00:50
  • I sincerely do not know but I would do it by drawing my rectangles in the paintComponent method. Check out the answers of @MadProgrammer. He's an expert in swing. – Yassin Hajaj Sep 19 '15 at 13:12
  • @KévinDuguay You might want to have a look at [Painting in AWT and Swing](http://www.oracle.com/technetwork/java/painting-140037.html) and [Performing Custom Painting](http://docs.oracle.com/javase/tutorial/uiswing/painting/) for more details into how painting works in Swing – MadProgrammer Sep 19 '15 at 22:15
  • Hey @KévinDuguay , do not forget to validate the answer if it helped you in any way :) – Yassin Hajaj Sep 20 '15 at 18:54