How to Put Pictures in Java BlueJ

Techwalla may earn compensation through affiliate links in this story. Learn more about our affiliate and product review process here.

BlueJ, an integrated development platform for the coding, editing, compiling and running Java-based programs, can be used to create graphic design programs, game programs and animations. For these programs you will often need to include pictures. Use BlueJ to create JAVA programs that will automatically draw pictures or import pictures from jpeg and gif files. For beginners, knowing how to put pictures with JAVA's graphic classes is a relatively easy task. With these classes, you can put custom pictures of simple objects in BlueJ with just a few lines of code.

Advertisement

Step 1

Start the BlueJ program. Click the "New Project" option from the " Project" menu. Save the project as a folder named "put-pictures" in the "save" dialog box that appears.

Video of the Day

Step 2

Click the "New Class" button. Type in the name "Draw_Picture" for the class name in the dialog box that appears. Click "OK."

Advertisement

Step 3

Double-click the "Draw_Picture" icon to open up the text editor to type the code for the Class "Draw_Picture." Type the JAVA code that imports the necessary JAVA graphic classes to work with graphic objects, the javax swing class and the java awt (Abstract Web Toolbox) class.

Advertisement

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

Step 4

Type in the code starting at the next line of the text editor to declare a class named "Frame" that extends the JPanel class, a subclass of the swing class. Use the JFrame method of the swing class to create a Jframe container named "frame" that will be used to draw or place pictures into. Set the width argument to 640 pixels and the height argument to 480 pixels for the JFrame container with the "setSize" method.

Advertisement

public class Frame extends JPanel {

public Frame() { JFrame frame=new JFrame(); frame.add(this); frame.setSize(640, 480); frame.setVisible(true); }

Advertisement

Step 5

Type in the code starting at the next line of the text editor that will render a picture of a rectangle within the "frame" created using the paint method. Set the x-position parameter to 100 pixels and the y-position argument to 100 pixels in the "drawRect" method to position the upper left corner of a rectangle. Set the rectangular width argument to 200 pixels and the rectangular height argument to 200 pixels for the boundaries of the rectangle (drawRect method). Set the "Color.BLACK" property in the setColor method to set the color of the rectangle's border to black.

Advertisement

public void paint(Graphics g) { g2.setColor(Color.BLACK); g2.drawRect(100,100,200,200); }

Type in the closing curly bracket to close the Frame class code.

}

Advertisement

Step 6

Click the "New Class" button. Type in the name "main_program" for the class name in the dialog box that appears. Click "OK."

Step 7

Double-click the "main_program" icon to open up the text editor to type the code for the "main_program" class. Type the JAVA code that instantiates the main_program class. Create a Frame object, named "drawFrame," of the Frame class with the "new" command

Advertisement

public class main_program { public static void main(String[] args) { Frame drawFrame= new Frame(); }

}

Step 8

Click the "Compile" button. Click the "Close" button. Right-click the "main_program" icon and select the "void main(String[] args)" option in the menu that appears. Observe that a rectangle has been drawn on a white screen with a black border.

Video of the Day

Advertisement

Advertisement

references & resources