Hi,

I wrote a simple program that should display the text (with specific fonts), one color background and drawing. I tried to run the program but it didn't work. Any suggestions?

What is the method of Graphics that allow you to draw within the program? eg java.awt.Graphics but I don't know how to add the code into that program.


DrawGraphics.java

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

public class DrawGraphics extends JFrame {
	private JLabel label1;
	private JLabel label2;
	private JLabel label3;
	
	public DrawGraphics() {
		super("Testing JLabel");
		setLayout(new FlowLayout());
		
		label1 = new JLabel("Workshop 3: Graphics");
		label1.setFont(new Font("monospaced", Font.BOLD+Font.ITALIC, 18));
		Color background = new Color(70, 80, 70);
		label1.setBackground(background);
		add(label1);


		}
	}

LabelFrame.java

import javax.swing.JFrame;

public class LabelFrame {
	public static void main(String[] args) {
        DrawGraphics labelFrame = new DrawGraphics();
		labelFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		labelFrame.setSize(300, 250);
		labelFrame.setVisible(true);
		}
	}

it didn't work. Any suggestions?

Copy and paste here the full text of any error messages you get.

If no errors, describe what the program does and why that is NOT WORKING.

What is the method of Graphics that allow you to draw within the program?

There are several methods in the Graphics class that draw. Which one(s) are you wondering about?

how to add the code into that program.

Normally you create an instance of a JPanel class and override its paintComponent() method and do your drawing there. The JPanel object is added to a container like a JFrame in the normal way. Do a Search for paintComponent to find code samples on the forum.

I have modified your code by adding the paint() method to draw string, oval, and rectangle, from which you may start to taste the Graphics methods. Read the API to find more methods of Graphics.

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

public class DrawGraphics extends JFrame {
	private JLabel label1;
	private JLabel label2;
	private JLabel label3;
	
	public DrawGraphics() {
		super("Testing JLabel");
		setLayout(new FlowLayout());
		
		label1 = new JLabel("Workshop 3: Graphics");
		label1.setFont(new Font("monospaced", Font.BOLD+Font.ITALIC, 18));
		label1.setForeground(Color.orange);
		add(label1);
		}
		
		public void paint(Graphics g){
			super.paint(g);
			g.setFont(new Font("ARAIL", Font.BOLD, 30)); // set Font
			g.setColor(Color.blue); // set the color of the drawing pencil.
			g.drawString("GWorkshop 3: Graphics", 20,150);// draw a string
			g.setColor(Color.cyan);
			g.fillOval(100,160,100,200); // fill Oval/circle/ellipse
			g.setColor(Color.gray);
			g.fillRect(250,160,100,200); // fill rectangle/squre
		}
	public static void main(String args[]){
		DrawGraphics dg = new DrawGraphics();
		dg.setSize(400,400);
		dg.setVisible(true);
                  dg.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	}

Normally the labels, buttons, and other components are placed in different sub-container from the drawing canvas. Therefore I have further modified the code so that the drawing part is defined as an inner class: Mydraw.
Also the Graphics2D has more function than Graphics, such as setPaint(), texture paint, coordinate transformation(rotation), ann so on. Read API about Graphics2D as well.

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

public class DrawGraphics extends JFrame {
	private JLabel label1;
	private JLabel label2;
	private JLabel label3;
	
	public DrawGraphics() {
		super("Testing JLabel"); // the title for the window
//setLayout(new BorderLayout()); // the default LayoutManager for JFrame is BorderLayout. Therefore this line of code is deleted.
		JPanel p = new JPanel();
		label1 = new JLabel("Workshop 3: Graphics");
		label1.setFont(new Font("monospaced", Font.BOLD+Font.ITALIC, 18));
		label1.setForeground(Color.blue); // set the Font color
		label1.setBorder(BorderFactory.createRaisedBevelBorder()  ); //set up the label border raised
		p.add(label1);
		p.setBackground(Color.pink);
		add(p,BorderLayout.NORTH);
		add(new Mydraw(),BorderLayout.CENTER);
		}
	class Mydraw extends JPanel{
		
	BufferedImage texture(){  // make a texture unit pattern
	BufferedImage buffImage = new BufferedImage( 10, 10, BufferedImage.TYPE_INT_RGB );  //create a space 10*10 to store buffImage
      Graphics g = buffImage.createGraphics();   // the pencil g for the buffImage
      g.setColor( Color.YELLOW ); // draw in yellow, find a yellow pencil
      g.fillRect( 0, 0, 10, 10 ); // draw a filled rectangle     
      g.setColor( Color.RED );   // draw in red
      g.fillRect( 1, 1, 8, 8 );  // draw a filled slightly smaller rectangle
      return buffImage; 
      }

		public void paint(Graphics g){

			g.setFont(new Font("Arial Black", Font.BOLD, 60)); // set Font
			g.setColor(Color.blue); // set the color of the drawing pencil.
			g.drawString("Workshop", 20,50);// draw a string
			g.setColor(Color.cyan);
			g.fillOval(20,60,100,200); // fill Oval/circle/ellipse
			g.setColor(Color.gray);
			g.fillRect(140,60,100,200); // fill rectangle/squre
			Graphics2D g2d = (Graphics2D)g;
			g2d.setPaint( new TexturePaint( texture(), new Rectangle( 10, 10 ) ) ); // draw with texture pattern, the unit size is 10x10
			g2d.fillArc(260,60,100,200,0,-270); // fill the Arc from 0 to -270. A negative degree is counted clockwise
			g2d.drawString("Graphics", 20,310);
		}
	}
	public static void main(String args[]){
		DrawGraphics dg = new DrawGraphics();
		dg.setSize(400,400);
		dg.setVisible(true);
		dg.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
}
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.