LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-22-2015, 05:21 PM   #1
Daedra
Senior Member
 
Registered: Dec 2005
Location: Springfield, MO
Distribution: Slackware64-15.0
Posts: 2,730

Rep: Reputation: 1393Reputation: 1393Reputation: 1393Reputation: 1393Reputation: 1393Reputation: 1393Reputation: 1393Reputation: 1393Reputation: 1393Reputation: 1393
Java + ActionListener help


Nevermind, I found the solution


Hi Guys,

I am an active member over at the slackware forums, but I am doing some Java homework and I have a quick question.

Also please note that I know the proper etiquette, however when it comes to a programming question I may not ask correctly, so If I don't please gently correct me lol.

But I have my homework program working, its pretty simple its a GUI clock that displays hours and minutes of 0,15,30, and 45. It is finished I just need to to basically change the time when I ever I click the anywhere in the program. so if it loaded as 7:30 when I clicked it would change to what ever random time I have programmed in my range.

Thank you for any help in advance. And like I said if I am asking wrong please correct me. Here is my code, I know some of the formatting is off, the professor said it was ok to copy a portion of the code from the ebook, I just haven't formatted it yet to make it look nice. As you can see toward the top is where my mouselistener is, I am assuming that is where I need to put code to redraw and re-randomize the time, I just cant figure out how to do this?


import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;

import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JApplet;

import javax.swing.JFrame;
import javax.swing.JPanel;

/*
* 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.
*/

/**
*
* @author Matt
*/
public class DisplayClock extends JApplet
{
public DisplayClock()
{
// create an analog clock for the current time
StillClock clock = new StillClock();


// Display hour, minute, and second in the message panel
MessagePanel messagePanel = new MessagePanel(clock.getHour() + ":" + clock.getMinute());
messagePanel.setCentered(true);
messagePanel.setForeground(Color.blue);
messagePanel.setFont(new Font("Courier", Font.BOLD, 16));

// Add the clock and message panel to the frame

add(clock);
add(messagePanel, BorderLayout.SOUTH);

addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
This is where I need to have the clock redraw and have a new time?
}
});
}




public static void main(String[] args)
{
JFrame frame = new JFrame("");

DisplayClock applet = new DisplayClock();
frame.add(applet);
frame.setTitle("Display Clock");
frame.setSize(300,350);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}

public class StillClock extends JPanel
{
private int hour;
private int minute;
private int second;

public StillClock()
{
setCurrentTime();
}

public StillClock(int hour, int minute, int seconds)
{
this.hour = hour;
this.minute = minute;
this.second = second;
}

public int getHour()
{
return hour;
}

public void setHour(int hour)
{
this.hour = hour;
repaint();
}

public int getMinute()
{
return minute;
}

public void setMinute(int minute)
{
this.minute = minute;
repaint();
}

public int getSecond()
{
return second;
}

public void setSecond(int second)
{
this.second = second;
repaint();
}

//@Override
protected void paintComponent(Graphics g)
{
super.paintComponents(g);

int clockRadius = (int)(Math.min(getWidth(), getHeight()) * 0.8 * 0.5);
int xCenter = getWidth() / 2;
int yCenter = getHeight() / 2;

g.setColor(Color.BLACK);
g.drawOval(xCenter - clockRadius, yCenter - clockRadius, 2 * clockRadius, 2 * clockRadius);
g.drawString("12", xCenter - 5, yCenter - clockRadius + 12);
g.drawString("9", xCenter - clockRadius + 3, yCenter + 5);
g.drawString("3", xCenter + clockRadius - 10, yCenter + 3);
g.drawString("6", xCenter - 3, yCenter + clockRadius - 3);

//int sLength = (int)(clockRadius * 0.8);
//int xSecond = (int)(xCenter + sLength * Math.sin(second * (2 * Math.PI / 60)));
//int ySecond = (int)(yCenter - sLength * Math.cos(second * (2 * Math.PI / 60)));
//g.setColor(Color.red);
//g.drawLine(xCenter, yCenter, xSecond, ySecond);

int mLength = (int)(clockRadius * 0.65);
int xMinute = (int)(xCenter + mLength * Math.sin(minute * (2 * Math.PI / 60)));
int yMinute = (int)(yCenter - mLength * Math.cos(minute * (2 * Math.PI / 60)));
g.setColor(Color.blue);
g.drawLine(xCenter, yCenter, xMinute, yMinute);

int hLength = (int)(clockRadius * 0.5);
int xHour = (int)(xCenter + hLength * Math.sin((hour 12 + minute / 60.0) * (2 * Math.PI / 12)));
int yHour = (int)(yCenter - hLength * Math.cos((hour % 12 + minute / 60.0) * (2 * Math.PI / 12)));
g.setColor(Color.blue);
g.drawLine(xCenter, yCenter, xHour, yHour);
}

public void setCurrentTime()
{
//Calendar calendar = new GregorianCalendar();

//this.hour = calendar.get(Calendar.HOUR_OF_DAY);
//this.minute = calendar.get(Calendar.MINUTE);
//this.second = calendar.get(Calendar.SECOND);
int min = (int)(Math.random() * (4 - 1)) + 1;

this.hour = (int)(Math.random() * (12 - 1)) + 1;

if (min == 1)
{
this.minute = 0;
}
else if (min == 2)
{
this.minute = 15;
}
else if (min == 3)
{
this.minute = 30;
}
else if (min == 4)
{
this.minute = 45;
}

}

public Dimension getPreferredSize()
{
return new Dimension(200,200);
}



}
public class MessagePanel extends JPanel {

/** The message to be displayed */

private String message = "Welcome to Java";



/** The x coordinate where the message is displayed */

private int xCoordinate = 20;



/** The y coordinate where the message is displayed */

private int yCoordinate = 20;



/** Indicate whether the message is displayed in the center */

private boolean centered;



/** The interval for moving the message horizontally and vertically */

private int interval = 10;



/** Construct with default properties */

public MessagePanel() {

}



/** Construct a message panel with a specified message */

public MessagePanel(String message) {

this.message = message;

}



/** Return message */

public String getMessage() {

return message;

}



/** Set a new message */

public void setMessage(String message) {

this.message = message;

repaint();

}



/** Return xCoordinator */

public int getXCoordinate() {

return xCoordinate;

}



/** Set a new xCoordinator */

public void setXCoordinate(int x) {

this.xCoordinate = x;

repaint();

}



/** Return yCoordinator */

public int getYCoordinate() {

return yCoordinate;

}



/** Set a new yCoordinator */

public void setYCoordinate(int y) {

this.yCoordinate = y;

repaint();

}



/** Return centered */

public boolean isCentered() {

return centered;

}



/** Set a new centered */

public void setCentered(boolean centered) {

this.centered = centered;

repaint();

}



/** Return interval */

public int getInterval() {

return interval;

}



/** Set a new interval */

public void setInterval(int interval) {

this.interval = interval;

repaint();

}



/** Paint the message */

protected void paintComponent(Graphics g) {

super.paintComponent(g);



if (centered) {

// Get font metrics for the current font
FontMetrics fm = g.getFontMetrics();



// Find the center location to display
int stringWidth = fm.stringWidth(message);

int stringAscent = fm.getAscent();

// Get the position of the leftmost character in the baseline
xCoordinate = getWidth() / 2 - stringWidth / 2;

yCoordinate = getHeight() / 2 + stringAscent / 2;

}



g.drawString(message, xCoordinate, yCoordinate);

}



/** Move the message left */

public void moveLeft() {

xCoordinate -= interval;

repaint();

}



/** Move the message right */

public void moveRight() {

xCoordinate += interval;

repaint();

}



/** Move the message up */

public void moveUp() {

yCoordinate -= interval;

repaint();

}



/** Move the message down */

public void moveDown() {

yCoordinate += interval;

repaint();

}



/** Override get method for preferredSize */

public Dimension getPreferredSize() {

return new Dimension(200, 30);

}

}
}

Last edited by Daedra; 03-22-2015 at 07:01 PM.
 
Old 03-23-2015, 12:31 AM   #2
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
Please surround your code with [code] tags because it makes it much easier to read.
 
Old 03-24-2015, 06:29 PM   #3
Daedra
Senior Member
 
Registered: Dec 2005
Location: Springfield, MO
Distribution: Slackware64-15.0
Posts: 2,730

Original Poster
Rep: Reputation: 1393Reputation: 1393Reputation: 1393Reputation: 1393Reputation: 1393Reputation: 1393Reputation: 1393Reputation: 1393Reputation: 1393Reputation: 1393
Will do man, sorry about that.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Mountain Lion (OS X 10.8) Java Update: Unable to Access Java Settings and Preferences rm_-rf_windows Other *NIX 2 01-03-2013 09:34 AM
[SOLVED] Java Woes: A Java Runtime Environment (JRE) or Java Development Kit (JDK) must be available ... chytraeus Slackware 10 11-27-2010 10:04 AM
Java plugin installed correctly for Firefox but not able to view any java applet tvn Linux - Software 10 04-15-2010 02:13 AM
LXer: Java news met with cautious optimism in free Java community LXer Syndicated Linux News 0 11-14-2006 10:21 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:19 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration