1 Reply Latest reply: Mar 9, 2013 9:14 AM by rp0428 RSS

    Java Graphics -- Drawing Strings on JFrame

    992949
      Hello and thank you. I'm relatively young and I apologize for asking noob questions. The problem I am having is that I realized after 3 hours of research and then writing this code that when multiple JPanels are added to a JFrame, they overlap (I think) over each other. As a result, the output of this program below is at the bottom right of the frame, the number 841 appears (the last number in my array) and everything else is blank.
      And I could not at all understand how to create graphics objects. It kept giving me an error of not being able to be instantiated. And to be honest, I don't even really understand whats going on when I add the text to my JPanel. I never call paintComponents. And definately I could never even make a graphics object to provide it the parameter required. Anyway, I resolved to use Jpanels because they can be removed using the remove function of the JFrame. And It is vitally important that I can delete the strings off my frame. Here is my code:
      package main;
      
      import java.awt.*;
      import javax.swing.*;
      import java.util.*;
      
      public class VisualFrame extends JFrame{
          
          ArrayList<JPanel> numbers = new ArrayList<JPanel>();
          
          public VisualFrame(){
              setSize(1000, 500);
              setTitle("Binary Search");
              setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              setVisible(true);
          }
          
          public void createPanels(int[] array){
              int x = 10;
              int y = 20;
              int xOffset = 50;
              int yOffset = 50;
              
              for(int i=0; i<array.length; i++){
                  String num = Integer.toString(array);
      numPanel panel = new numPanel(num, x, y);
      numbers.add(panel);
      add(panel);
      x = x+xOffset;
      if(x>this.getWidth()){
      x = 10;
      y = y+yOffset;
      }
      }

      }
      }

      class numPanel extends JPanel{

      String num;
      int x;
      int y;

      public numPanel(String str, int x, int y){
      num = str;
      this.x = x;
      this.y = y;
      }

      public void paintComponent(Graphics g){
      g.setColor(Color.black);
      g.setFont(new Font("Times", Font.BOLD, 12));
      g.drawString(num, x, y);
      }
      }



      My main method is inside of a different class:
          public static void main(String[] args){
              VisualFrame frame = new VisualFrame();
              frame.createPanels(array);
          }
      Firstly, do I even have the problem I'm having right? If so,
      Is there a way to restrict the size of the panels to the length and width of what it contains?
      Or can someone give me a good, very detailed link to how to use Graphics? Or perhaps someone could prove to me a good method.

      Edited by: 989946 on Mar 8, 2013 6:45 PM
        • 1. Re: Java Graphics -- Drawing Strings on JFrame
          rp0428
          Why don't you start by learning from the experts?

          The Java Tutorial has sections that show how to use Java functionality.
          http://docs.oracle.com/javase/tutorial/

          The Graphiics section covers GUI and Swing
          http://docs.oracle.com/javase/tutorial/uiswing/index.html
          >
          Creating Graphical User Interfaces

          Creating a GUI with Swing — A comprehensive introduction to GUI creation on the Java platform.
          >
          And that section has links for trails such as how to use ALL of the different swing components including frames and panels
          http://docs.oracle.com/javase/tutorial/uiswing/components/index.html
          >
          Using Swing Components tells you how to use each of the Swing components — buttons, tables, text components, and all the rest. It also tells you how to use borders and icons.