I am having trouble printing onto a JPanel. I am able to print any string that i want to. here my code:

import javax.swing.JFrame;
import java.awt.BorderLayout;


import javax.swing.JLabel;

public class TestDraw 
{

	public static void main(String[] args) 
	{
		final int WINDOW_WIDTH = 300, WINDOW_HEIGHT = 300;
		DrawPanel panel = new DrawPanel (WINDOW_WIDTH, WINDOW_HEIGHT);
		JFrame application = new JFrame();
		JLabel label = new JLabel();
		
		label.setText("test");
		
		application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		application.add(panel);
		application.setSize( 300, 300);
		application.setVisible( true);
		application.add(label, BorderLayout.SOUTH);
	}
}

Note "Test", what i really want to print is a random number generated from a class. Those numbers are generated here:

import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;
import javax.swing.JPanel;

public class DrawPanel extends JPanel 
{
	private Random randomNumbers = new Random();
	private MyLine lines [];
	private MyRectangle rectangles[];
	private MyOval ovals[];
	static final long serialVersionUID = 0;
	

	public String toString()
	{
		return String.format("Lines: %f, Ovals: %f, Rectangles: %f",lines,rectangles,ovals);
	}	
	//constructor, creates a panel with random shapes
	public DrawPanel(int windowWidth, int windowHeight)
	{
		// declare local variables
		int x1, y1, x2, y2;
		Color color;
		boolean fill;
		
		setBackground( Color.WHITE);
		
		lines = new MyLine[1+ randomNumbers.nextInt(5)];
		rectangles = new MyRectangle[1 + randomNumbers.nextInt(5)];
		ovals = new MyOval[1 + randomNumbers.nextInt(5)];		
		
		//create lines
		for (int count = 0; count < lines.length; count++)
		{
			//generate random coordinates
			x1 = randomNumbers.nextInt (windowWidth);
			y1 = randomNumbers.nextInt (windowHeight);
			x2 = randomNumbers.nextInt (windowWidth);
			y2 = randomNumbers.nextInt (windowHeight);
			
			//generate a random color
			color = new Color( randomNumbers.nextInt (256),
					randomNumbers.nextInt(256), randomNumbers.nextInt(256));
			
			fill = randomNumbers.nextBoolean ();
			
			//add to the list to be displayed
			lines[count] = new MyLine( x1, y1, x2, y2, color, fill);
		}
		
		//create rectangles
		for (int count = 0; count < rectangles.length; count++)
		{
			//generate random coordinates
			x1 = randomNumbers.nextInt (windowWidth);
			y1 = randomNumbers.nextInt (windowHeight);
			x2 = randomNumbers.nextInt (windowWidth);
			y2 = randomNumbers.nextInt (windowHeight);
			
			//generate a random color
			color = new Color( randomNumbers.nextInt (256),
					randomNumbers.nextInt(256), randomNumbers.nextInt(256));
			
			// generate a random fill
			fill = randomNumbers.nextBoolean ();
			
			//add to the list to be displayed
			rectangles[count] = new MyRectangle( x1, y1, x2, y2, color, fill);
		}
		
		//create ovals
		for (int count = 0; count < ovals.length; count++)
		{
			//generate random coordinates
			x1 = randomNumbers.nextInt (windowWidth);
			y1 = randomNumbers.nextInt (windowHeight);
			x2 = randomNumbers.nextInt (windowWidth);
			y2 = randomNumbers.nextInt (windowHeight);
			
			//generate a random color
			color = new Color( randomNumbers.nextInt (256),
					randomNumbers.nextInt(256), randomNumbers.nextInt(256));
			
			// generate a random fill
			fill = randomNumbers.nextBoolean ();
			
			//add to the list to be displayed
			ovals[count] = new MyOval( x1, y1, x2, y2, color, fill);
		}
	}
	
	public void paintComponent( Graphics g)
	{
		super.paintComponent (g);
		
		for (MyLine line : lines) 
			line.draw (g);
		for (MyRectangle rect : rectangles)
			rect.draw (g);
		for (MyOval oval : ovals)
			oval.draw (g);  
	}
}

Im trying to do is get the actually number of randomly generated numbers, the red, and be able to print those numbers on the JPanel. Like i said, i am able to print a string, but i am uncertain on how to move those numbers from the DrawPanel class to the TestDraw class.

why are you defining the label in TestDraw? if it is going onto the DrawPanel, why not add it to that instead?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.