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

Help needed urgently ! Thanx :) /* * Grafix_Thing.java */ import java.awt.*; /**

ID: 3872859 • Letter: H

Question

Help needed urgently ! Thanx :)

/*
* Grafix_Thing.java
*/

import java.awt.*;

/**
* Template code file for basic Java graphics
* Make sure to add all graphics code in the paint method,
* NOT the main method.
*
* @author Dr. Norman J. Bashias
*/
@SuppressWarnings("serial")
public class Grafix_Thing extends javax.swing.JFrame
{

public void paint(Graphics g)
{
// draw the window title, menu, buttons
super.paint(g);

// get the Graphics context for the canvas
Graphics pen = canvas.getGraphics();

// *** add pen Graphics method calls after this line ***

/**
* *********************************************************
* WARNING!!! DO NOT ADD OR MODIFY CODE BELOW THIS LINE!!! *
* *********************************************************
*/
} // end paint

/**
* Creates new form NB_Grafix_Thing2
*/
public Grafix_Thing()
{
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 NB_Grafix_Thing2 Editor.
*/
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents()
{

canvas = new javax.swing.JPanel();
resetBtn = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("My Drawing Form");

canvas.setBackground(new java.awt.Color(255, 255, 255));
canvas.setLayout(new java.awt.BorderLayout());
getContentPane().add(canvas, java.awt.BorderLayout.CENTER);

resetBtn.setText("Reset");
resetBtn.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent evt)
{
resetBtnActionPerformed(evt);
}
});
getContentPane().add(resetBtn, java.awt.BorderLayout.PAGE_END);

setSize(new java.awt.Dimension(816, 639));
setLocationRelativeTo(null);
}// </editor-fold>//GEN-END:initComponents

private void resetBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_resetBtnActionPerformed
repaint();
}//GEN-LAST:event_resetBtnActionPerformed

/**
* @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.
*/
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(Grafix_Thing.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex)
{
java.util.logging.Logger.getLogger(Grafix_Thing.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex)
{
java.util.logging.Logger.getLogger(Grafix_Thing.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex)
{
java.util.logging.Logger.getLogger(Grafix_Thing.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
//</editor-fold>

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

// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JPanel canvas;
private javax.swing.JButton resetBtn;
// End of variables declaration//GEN-END:variables
} // end Grafix_Thing class

Notes > 2-D Graphics in Java), design and write a program (in the Grafix_Thing's paint method, using the pen Graphics object) to draw a colorful winking face similar to the following one:

Explanation / Answer

import java.applet.Applet; import java.awt.BorderLayout; import java.awt.Canvas; import java.awt.Color; import java.awt.Graphics; public class A extends Applet { private static final long serialVersionUID = -1152278362796573663L; public class MyCanvas extends Canvas { private static final long serialVersionUID = -4372759074220420333L; private int flag = 0; public void paint(Graphics g) { draw(); } public void draw() { Graphics g = this.getGraphics(); g.setColor(Color.BLACK); super.paint(g); if (flag == 0) { System.out.println(flag); g.drawOval(40, 40, 120, 150);// face g.drawRect(57, 75, 30, 5);// left eye shut g.drawRect(110, 75, 30, 20);// right eye g.drawOval(85, 100, 30, 30);// nose g.fillArc(60, 125, 80, 40, 180, 180);// mouth g.drawOval(25, 92, 15, 30);// left ear g.drawOval(160, 92, 15, 30);// right ear flag = 1; } else { System.out.println(flag); g.drawOval(40, 40, 120, 150);// face g.drawOval(57, 75, 30, 20);// left eye g.drawOval(110, 75, 30, 20);// right eye g.fillOval(68, 81, 10, 10);// left pupil g.fillOval(121, 81, 10, 10);// right pupil g.drawOval(85, 100, 30, 30);// nose g.fillArc(60, 125, 80, 40, 180, 180);// mouth g.drawOval(25, 92, 15, 30);// left ear g.drawOval(160, 92, 15, 30);// right ear flag = 0; } try { Thread.sleep(900); } catch (Exception e) { System.out.println("killed while sleeping"); } this.repaint(100); } } public void init() { this.C = new MyCanvas(); this.setLayout(new BorderLayout()); this.add(C, BorderLayout.CENTER); C.setBackground(Color.GRAY); } private MyCanvas C; }