***This is a JAVA question***
------------------------------------------------------------------------------------------------
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.imageio.*;
import javax.swing.*;
import javax.swing.event.*;
public class DrawingPanel implements ActionListener {
public static final int DELAY = 50; // delay between repaints in
millis
private static final String DUMP_IMAGE_PROPERTY_NAME =
"drawingpanel.save";
private static String TARGET_IMAGE_FILE_NAME = null;
private static final boolean PRETTY = true; // true to
anti-alias
private static boolean DUMP_IMAGE = true; // true to write
DrawingPanel to file
private int width, height; // dimensions of window frame
private JFrame frame; // overall window frame
private JPanel panel; // overall drawing surface
private BufferedImage image; // remembers drawing commands
private Graphics2D g2; // graphics context for painting
private JLabel statusBar; // status bar showing mouse
position
private long createTime;
static {
TARGET_IMAGE_FILE_NAME =
System.getProperty(DUMP_IMAGE_PROPERTY_NAME);
DUMP_IMAGE = (TARGET_IMAGE_FILE_NAME != null);
}
// construct a drawing panel of given width and height enclosed
in a window
public DrawingPanel(int width, int height) {
this.width = width;
this.height = height;
this.image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_ARGB);
this.statusBar = new JLabel(" ");
this.statusBar.setBorder(BorderFactory.createLineBorder(Color.BLACK));
this.panel = new JPanel(new FlowLayout(FlowLayout.CENTER, 0,
0));
this.panel.setBackground(Color.WHITE);
this.panel.setPreferredSize(new Dimension(width, height));
this.panel.add(new JLabel(new ImageIcon(image)));
// listen to mouse movement
MouseInputAdapter listener = new MouseInputAdapter() {
public void mouseMoved(MouseEvent e) {
DrawingPanel.this.statusBar.setText("(" + e.getX() + ", " +
e.getY() + ")");
}
public void mouseExited(MouseEvent e) {
DrawingPanel.this.statusBar.setText(" ");
}
};
this.panel.addMouseListener(listener);
this.panel.addMouseMotionListener(listener);
this.g2 = (Graphics2D)image.getGraphics();
this.g2.setColor(Color.BLACK);
if (PRETTY) {
this.g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
this.g2.setStroke(new BasicStroke(1.1f));
}
this.frame = new JFrame("Building Java Programs - Drawing
Panel");
this.frame.setResizable(false);
this.frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
if (DUMP_IMAGE) {
DrawingPanel.this.save(TARGET_IMAGE_FILE_NAME);
}
System.exit(0);
}
});
this.frame.getContentPane().add(panel);
this.frame.getContentPane().add(statusBar, "South");
this.frame.pack();
this.frame.setVisible(true);
if (DUMP_IMAGE) {
createTime = System.currentTimeMillis();
this.frame.toBack();
} else {
this.toFront();
}
// repaint timer so that the screen will update
new Timer(DELAY, this).start();
}
// used for an internal timer that keeps repainting
public void actionPerformed(ActionEvent e) {
this.panel.repaint();
if (DUMP_IMAGE && System.currentTimeMillis() >
createTime + 4 * DELAY) {
this.frame.setVisible(false);
this.frame.dispose();
this.save(TARGET_IMAGE_FILE_NAME);
System.exit(0);
}
}
// obtain the Graphics object to draw on the panel
public Graphics2D getGraphics() {
return this.g2;
}
// set the background color of the drawing panel
public void setBackground(Color c) {
this.panel.setBackground(c);
}
// show or hide the drawing panel on the screen
public void setVisible(boolean visible) {
this.frame.setVisible(visible);
}
// makes the program pause for the given amount of time,
// allowing for animation
public void sleep(int millis) {
try {
Thread.sleep(millis);
} catch (InterruptedException e) {}
}
// take the current contents of the panel and write them to a
file
public void save(String filename) {
String extension = filename.substring(filename.lastIndexOf(".") +
1);
// create second image so we get the background color
BufferedImage image2 = new BufferedImage(this.width, this.height,
BufferedImage.TYPE_INT_RGB);
Graphics g = image2.getGraphics();
g.setColor(panel.getBackground());
g.fillRect(0, 0, this.width, this.height);
g.drawImage(this.image, 0, 0, panel);
// write file
try {
ImageIO.write(image2, extension, new java.io.File(filename));
} catch (java.io.IOException e) {
System.err.println("Unable to save image:\n" + e);
}
}
// makes drawing panel become the frontmost window on the
screen
public void toFront() {
this.frame.toFront();
}
}
------------------------------------------------------------------------------------------------
import java.awt.*;
public class Facebook{
private String name;
private String content;
DrawingPanel panel;
private Graphics g;
public Facebook(String nm){
content = "undefined";
name = nm;
// Create the drawing panel
panel = new DrawingPanel(200, 150);
g = panel.getGraphics();
// display name
g.drawString(name+"'s mood is undefined.", 20, 75);
}
public void setContent(String newContent){
content = newContent;
if(content.equals("happy")){
g.setColor(Color.red);
g.fillRect(0, 0, 200, 150);
g.setColor(Color.black);
// display mood
g.drawString(name+"'s mood is:"+ "happy", 20, 75);
}
else{
g.setColor(Color.white);
g.fillRect(0, 0, 200, 150);
g.setColor(Color.black);
g.drawString(name+"'s mood is:"+ content, 20, 75);
}
}
public String getContent(){
return content;
}
}
------------------------------------------------------------------------------------------------
public class FacebookPerson{
private String myName;
protected String myMood;
protected Facebook myfacebook;
public FacebookPerson(String name){
myName = name;
myfacebook = new Facebook(myName);
//System.out.println("FacebookPerson's constructor");
}
public FacebookPerson(){
}
public String getName(){
return myName;
}
public void setMood(String newMood){
myMood = newMood;
myfacebook.setContent(myMood);
}
public String getMood(){
return myMood;
}
}
------------------------------------------------------------------------------------------------
public class simple_test{
public static void main (String[] args){
Facebook fb = new Facebook("Dr. Hu");
fb.setContent("happy");
} // end main
}
------------------------------------------------------------------------------------------------
import java.util.*;
public class testFacebook{
public static void main (String[] args){
// Prompt user to enter the number of facebookpresons
Scanner userInput = new Scanner(System.in);
System.out.println("Enter the number of facebookpresons to be created: ");
int numP=0;
while(true){
try{
String inputString = userInput.nextLine();
numP = Integer.parseInt(inputString);
if(numP>0 && numP<=5) // accept the number if it is within range. Here we define the range to be from 1 to 5.
break;
else
System.out.println("the number is out of range [1, 5]! enter again");
} catch (NumberFormatException e){
System.out.println("invalid input. Enter an integer number!");
}
}
FacebookPerson[] fbp = new FacebookPerson[numP];
//Ask the user to enter the name for each person, and create the persons
for(int i=0; i< numP; i++){
System.out.println("Enter the name for person "+ (i+1)+ ":");
String name = userInput.nextLine();
fbp[i] = new FacebookPerson(name);
}
System.out.println("-------select a person and type the mood below--------");
//Ask the user to set the mood for a person, and update the mood, enter "####" to exit
while(true){
System.out.println("Enter the name for a person (enter #### to exit):");
String name = userInput.nextLine();
if(name.equals("####"))
System.exit(0);
int personID = -1;
for(int i=0; i< numP; i++){
if(fbp[i].getName().equals(name)){
personID = i;
break; // break the for loop
}
}
if(personID!=-1){ // found the person, otherwise personID should still be -1
System.out.println("Enter the mood for the person:");
String mood = userInput.nextLine();
fbp[personID].setMood(mood);
}
else
System.out.println("unrecognized name!");
} // end while
} // end main
}
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.imageio.*;
import javax.swing.*;
import javax.swing.event.*;
public class DrawingPanel implements ActionListener {
public static final int DELAY = 50; // delay between repaints in
millis
private static final String DUMP_IMAGE_PROPERTY_NAME =
"drawingpanel.save";
private static String TARGET_IMAGE_FILE_NAME = null;
private static final boolean PRETTY = true; // true to
anti-alias
private static boolean DUMP_IMAGE = true; // true to write
DrawingPanel to file
private int width, height; // dimensions of window frame
private JFrame frame; // overall window frame
private JPanel panel; // overall drawing surface
private BufferedImage image; // remembers drawing commands
private Graphics2D g2; // graphics context for painting
private JLabel statusBar; // status bar showing mouse
position
private long createTime;
static {
TARGET_IMAGE_FILE_NAME =
System.getProperty(DUMP_IMAGE_PROPERTY_NAME);
DUMP_IMAGE = (TARGET_IMAGE_FILE_NAME != null);
}
// construct a drawing panel of given width and height enclosed
in a window
public DrawingPanel(int width, int height) {
this.width = width;
this.height = height;
this.image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_ARGB);
this.statusBar = new JLabel(" ");
this.statusBar.setBorder(BorderFactory.createLineBorder(Color.BLACK));
this.panel = new JPanel(new FlowLayout(FlowLayout.CENTER, 0,
0));
this.panel.setBackground(Color.WHITE);
this.panel.setPreferredSize(new Dimension(width, height));
this.panel.add(new JLabel(new ImageIcon(image)));
// listen to mouse movement
MouseInputAdapter listener = new MouseInputAdapter() {
public void mouseMoved(MouseEvent e) {
DrawingPanel.this.statusBar.setText("(" + e.getX() + ", " +
e.getY() + ")");
}
public void mouseExited(MouseEvent e) {
DrawingPanel.this.statusBar.setText(" ");
}
};
this.panel.addMouseListener(listener);
this.panel.addMouseMotionListener(listener);
this.g2 = (Graphics2D)image.getGraphics();
this.g2.setColor(Color.BLACK);
if (PRETTY) {
this.g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
this.g2.setStroke(new BasicStroke(1.1f));
}
this.frame = new JFrame("Building Java Programs - Drawing
Panel");
this.frame.setResizable(false);
this.frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
if (DUMP_IMAGE) {
DrawingPanel.this.save(TARGET_IMAGE_FILE_NAME);
}
System.exit(0);
}
});
this.frame.getContentPane().add(panel);
this.frame.getContentPane().add(statusBar, "South");
this.frame.pack();
this.frame.setVisible(true);
if (DUMP_IMAGE) {
createTime = System.currentTimeMillis();
this.frame.toBack();
} else {
this.toFront();
}
// repaint timer so that the screen will update
new Timer(DELAY, this).start();
}
// used for an internal timer that keeps repainting
public void actionPerformed(ActionEvent e) {
this.panel.repaint();
if (DUMP_IMAGE && System.currentTimeMillis() >
createTime + 4 * DELAY) {
this.frame.setVisible(false);
this.frame.dispose();
this.save(TARGET_IMAGE_FILE_NAME);
System.exit(0);
}
}
// obtain the Graphics object to draw on the panel
public Graphics2D getGraphics() {
return this.g2;
}
// set the background color of the drawing panel
public void setBackground(Color c) {
this.panel.setBackground(c);
}
// show or hide the drawing panel on the screen
public void setVisible(boolean visible) {
this.frame.setVisible(visible);
}
// makes the program pause for the given amount of time,
// allowing for animation
public void sleep(int millis) {
try {
Thread.sleep(millis);
} catch (InterruptedException e) {}
}
// take the current contents of the panel and write them to a
file
public void save(String filename) {
String extension = filename.substring(filename.lastIndexOf(".") +
1);
// create second image so we get the background color
BufferedImage image2 = new BufferedImage(this.width, this.height,
BufferedImage.TYPE_INT_RGB);
Graphics g = image2.getGraphics();
g.setColor(panel.getBackground());
g.fillRect(0, 0, this.width, this.height);
g.drawImage(this.image, 0, 0, panel);
// write file
try {
ImageIO.write(image2, extension, new java.io.File(filename));
} catch (java.io.IOException e) {
System.err.println("Unable to save image:\n" + e);
}
}
// makes drawing panel become the frontmost window on the
screen
public void toFront() {
this.frame.toFront();
}
}
------------------------------------------------------------------------------------------------
import java.awt.*;
public class Facebook{
private String name;
private String content;
DrawingPanel panel;
private Graphics g;
public Facebook(String nm){
content = "undefined";
name = nm;
// Create the drawing panel
panel = new DrawingPanel(200, 150);
g = panel.getGraphics();
// display name
g.drawString(name+"'s mood is undefined.", 20, 75);
}
public void setContent(String newContent){
content = newContent;
if(content.equals("happy")){
g.setColor(Color.red);
g.fillRect(0, 0, 200, 150);
g.setColor(Color.black);
// display mood
g.drawString(name+"'s mood is:"+ "happy", 20, 75);
}
else{
g.setColor(Color.white);
g.fillRect(0, 0, 200, 150);
g.setColor(Color.black);
g.drawString(name+"'s mood is:"+ content, 20, 75);
}
}
public String getContent(){
return content;
}
}
------------------------------------------------------------------------------------------------
public class FacebookPerson{
private String myName;
protected String myMood;
protected Facebook myfacebook;
public FacebookPerson(String name){
myName = name;
myfacebook = new Facebook(myName);
//System.out.println("FacebookPerson's constructor");
}
public FacebookPerson(){
}
public String getName(){
return myName;
}
public void setMood(String newMood){
myMood = newMood;
myfacebook.setContent(myMood);
}
public String getMood(){
return myMood;
}
}
------------------------------------------------------------------------------------------------
public class simple_test{
public static void main (String[] args){
Facebook fb = new Facebook("Dr. Hu");
fb.setContent("happy");
} // end main
}
------------------------------------------------------------------------------------------------
import java.util.*;
public class testFacebook{
public static void main (String[] args){
// Prompt user to enter the number of facebookpresons
Scanner userInput = new Scanner(System.in);
System.out.println("Enter the number of facebookpresons to be created: ");
int numP=0;
while(true){
try{
String inputString = userInput.nextLine();
numP = Integer.parseInt(inputString);
if(numP>0 && numP<=5) // accept the number if it is within range. Here we define the range to be from 1 to 5.
break;
else
System.out.println("the number is out of range [1, 5]! enter again");
} catch (NumberFormatException e){
System.out.println("invalid input. Enter an integer number!");
}
}
FacebookPerson[] fbp = new FacebookPerson[numP];
//Ask the user to enter the name for each person, and create the persons
for(int i=0; i< numP; i++){
System.out.println("Enter the name for person "+ (i+1)+ ":");
String name = userInput.nextLine();
fbp[i] = new FacebookPerson(name);
}
System.out.println("-------select a person and type the mood below--------");
//Ask the user to set the mood for a person, and update the mood, enter "####" to exit
while(true){
System.out.println("Enter the name for a person (enter #### to exit):");
String name = userInput.nextLine();
if(name.equals("####"))
System.exit(0);
int personID = -1;
for(int i=0; i< numP; i++){
if(fbp[i].getName().equals(name)){
personID = i;
break; // break the for loop
}
}
if(personID!=-1){ // found the person, otherwise personID should still be -1
System.out.println("Enter the mood for the person:");
String mood = userInput.nextLine();
fbp[personID].setMood(mood);
}
else
System.out.println("unrecognized name!");
} // end while
} // end main
}
***This is a JAVA question*** ------------------------------------------------------------------------------------------------ import java.awt.*; import java.awt.event.*; import java.awt.image.*; import javax.imageio.*; import javax.swing.*;...
There 2 parts to complete: import javax.swing.*; import java.awt.event.*; import java.awt.*; public class Q4 extends JFrame { private int xPos, yPos; public Q4() { JPanel drawPanel = new JPanel() { @Override public void paintComponent(Graphics g) { super.paintComponent(g); // paint parent's background //complete this: } }; drawPanel.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent evt) { //complete this :- set the xPos, yPos } }); setContentPane(drawPanel); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setTitle("Mouse-Click Demo"); setSize(400, 250); setVisible(true); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() {...
Modify the NervousShapes program so that it displays equilateral triangles as well as circles and rectangles. You will need to define a Triangle class containing a single instance variable, representing the length of one of the triangle’s sides. Have the program create circles, rectangles, and triangles with equal probability. Circle and Rectangle is done, please comment on your methods so i can understand */ package NervousShapes; import java.awt.*; import java.awt.event.*; import java.awt.image.*; import javax.imageio.*; import javax.swing.*; import javax.swing.event.*; public class...
Simple java GUI language translator. English to Spanish, French, or German import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class translatorApp extends JFrame implements ActionListener { public static final int width = 500; public static final int height = 300; public static final int no_of_lines = 10; public static final int chars_per_line = 20; private JTextArea lan1; private JTextArea lan2; public static void main(String[] args){ translatorApp gui = new translatorApp();...
This is all I have regarding the question import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Quiz8_GUI { private JFrame frame; /** * Create an Quiz8_GUI show it on screen. */ public Quiz8_GUI() { makeFrame(); } // ---- swing stuff to build the frame and all its components ---- /** * Create the Swing frame and its content. */ private void makeFrame() { frame = new JFrame("Quiz8_GUI"); makeMenuBar(frame); Container contentPane = frame.getContentPane(); //set the Container to flow layout //Your...
Can you please fix the error. package com.IST240Apps; import java.awt.*; import java.awt.event.*; import java.io.*; import javax.swing.*; import java.net.*; public class LinkRotator extends JFrame implements Runnable, ActionListener { String[] pageTitle = new String[5]; URI[] pageLink = new URI[5]; int current = 0; Thread runner; JLabel siteLabel = new Jlabel(); public LinkRotator() { setDefaultCloseOperation(Jframe.EXIT_ON_CLOSE); setSize(300, 100); FlowLayout flo = new Flowlayout(); setLayout(flo); add(siteLabel); pageTitle = new String[] { "Oracle Java Site", "Server Side", "JavaWorld", "Google", "Yahoo", "Penn State" }; pageLink[0] = getUR1("http://www.oracle.com/technetwork/java");...
import javax.swing.*; import java.awt.event.*; import java.awt.*; public class BookReview extends JFrame implements ActionListener { private JLabel titleLabel; private JTextField titleTxtFd; private JComboBox typeCmb; private ButtonGroup ratingGP; private JButton processBnt; private JButton endBnt; private JButton clearBnt; private JTextArea entriesTxtAr; private JRadioButton excellentRdBnt; private JRadioButton veryGoodRdBnt; private JRadioButton fairRdBnt; private JRadioButton poorRdBnt; private String ratingString; private final String EXCELLENT = "Excellent"; private final String VERYGOOD = "Very Good"; private final String FAIR = "Fair"; private final String POOR = "Poor"; String...
import javax.swing.*; import java.awt.*; import java.util.List; import java.util.*; /** * Canvas is a class to allow for simple graphical drawing on a canvas. * This is a modification of the general purpose Canvas, specially made for * the BlueJ "shapes" example. * * @author: Bruce Quig * @author: Michael Kolling (mik) * Minor changes to canvas dimensions by William Smith 6/4/2012 * * @version: 1.6 (shapes) */ public class Canvas { // Note: The implementation of this class (specifically the...
import javax.swing.*; import java.awt.*; import java.util.List; import java.util.*; /** * Canvas is a class to allow for simple graphical drawing on a canvas. * This is a modification of the general purpose Canvas, specially made for * the BlueJ "shapes" example. * * @author: Bruce Quig * @author: Michael Kolling (mik) * Minor changes to canvas dimensions by William Smith 6/4/2012 * * @version: 1.6 (shapes) */ public class Canvas { // Note: The implementation of this class (specifically the...
"Polygon Drawer Write an applet that lets the user click on six points. After the sixth point is clicked, the applet should draw a polygon with a vertex at each point the user clicked." import java.awt.*; import java.awt.event.MouseListener; import java.awt.event.MouseEvent; import java.applet.*; public class PolygonDrawer extends Applet implements MouseListener{ //Declares variables private int[] X, Y; private int ptCT; private final static Color polygonColor = Color.BLUE; //Color stuff public void init() { setBackground(Color.BLACK); addMouseListener(this); X = new int [450]; Y =...
import java.awt.*; import javax.swing.*; import java.awt.event.*; import javax.swing.BorderFactory; import javax.swing.border.Border; public class Q1 extends JFrame { public static void createAndShowGUI() { JFrame frame = new JFrame("Q1"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Font courierFont = new Font("Courier", Font.BOLD, 40); Font arialFont = new Font("Arial", Font.BOLD, 40); Font sansFont = new Font("Sans-serif", Font.BOLD, 20); JLabel label = new JLabel("Enter a word"); label.setFont(sansFont); Font font1 = new Font("Sans-serif", Font.BOLD, 40); ...