Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. Create a JFrame class that includes a JPanel with a JTextField (assume a numb

ID: 3907597 • Letter: 1

Question

1. Create a JFrame class that includes a JPanel with a JTextField (assume a number can be input into it) and a Button on it. 2. When the user hits the Button it tries to read the value of the number in the JTextField and then draws that amount of Circles (any color) randomly in your JFrame. Randomize the colors of each Circle you draw For example, if a person enters the number 8, pressing the button will draw 8 circles in random X/Y positions on the JFrame. For this assignment you do not need to clear the board. 3. Add a button to clear the JFrame each time the button is hit. You can cheat and just draw a large rectangle that has the same color as your background. 4. Add a 3rd button that draws 1-5 Squares all of the same color when you hit it.

Explanation / Answer

NewClass.java

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package jframe;

/**
*
* @author Imran
*/
public class NewClass {
public static void main(String args[]){
NewJFrame frame = new NewJFrame();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
//frame.setDefaultCloseOperation(0));
}
}

NewJFrame.java

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package jframe;

import java.awt.Color;
import java.awt.Graphics2D;

/**
*
* @author Imran
*/
public class NewJFrame extends javax.swing.JFrame {
public void drawCircle(int x, int y, int noOfCircles)
{
int i =0;
String[] color={"0XFFFFFF","0x142b44","0xFF0096","#C0C0C0","#808080","#994635","#40C120","#C1B720","#2430E5"};
while(i<noOfCircles){
Graphics2D g2d = (Graphics2D)jPanel1.getGraphics();
g2d.setColor(Color.decode(color[i]));
g2d.fillOval(x*i, y*i, 60, 60);
i++;
}
}
public void drawSquare(int x, int no){
int i=1;
if (no>5)
System.out.println("Not greater than 5");
else
while(i<=no && i<5){
Graphics2D g2d = (Graphics2D)jPanel1.getGraphics();
g2d.setColor(Color.red);
g2d.fillRect(25*i,25*i,25,25);
i++;
}
}
/**
* Creates new form NewJFrame
*/
public NewJFrame() {
initComponents();
}

/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">   
private void initComponents() {

jPanel1 = new javax.swing.JPanel();
jTextField1 = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jTextField1.setText("1");

jButton1.setText("Draw Circle");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});

jButton2.setText("Clear");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});

jButton3.setText("Draw Square");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});

javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(26, 26, 26)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jButton1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton3)
.addContainerGap(74, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton1)
.addComponent(jButton2)
.addComponent(jButton3))
.addContainerGap(266, Short.MAX_VALUE))
);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);

pack();
}// </editor-fold>   

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
int noOfCircles=Integer.parseInt(jTextField1.getText());
drawCircle(60, 60, noOfCircles);
// int i=1;
// while(i<=noOfCircles){
// drawCircle(i*60, i*60);
// i++;
// }
  
}   

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
//jPanel1.removeAll();
jPanel1.repaint();
  
}   

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
int i=0;
  
drawSquare(45, Integer.parseInt(jTextField1.getText()));
i++;
  
}   

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>

/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame().setVisible(true);
}
});
}

// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JPanel jPanel1;
private javax.swing.JTextField jTextField1;
// End of variables declaration
}

DrawCircles.java

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package jframe;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import javax.swing.JPanel;

public class DrawCircles extends JPanel {
Image im = new BufferedImage(500, 500, HEIGHT);
public void drawCircle(int x, int y) {
im.getGraphics().drawOval(x,y,500,500); //TODO: should really dispose the Graphics object after drawing the oval
repaint();
}

public void paintComponent(Graphics g) {
g.drawImage(im,0,0,null);
}
}

DrawRect.java


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

class MyPanel extends JPanel {

private int squareX = 50;
private int squareY = 50;
private int squareW = 20;
private int squareH = 20;

protected void paintComponent(Graphics g) {
super.paintComponent(g); // do your superclass's painting routine first, and then paint on top of it.
g.setColor(Color.RED);
g.fillRect(squareX,squareY,squareW,squareH);
}
}