GregBrannon's Profile
Reputation: 1986
Grandmaster
- Group:
- Expert
- Active Posts:
- 4,837 (4.9 per day)
- Joined:
- 10-September 10
- Profile Views:
- 9,256
- Last Active:
32 minutes ago- Currently:
- Viewing Forum: Java
Previous Fields
- Country:
- US
- OS Preference:
- Linux
- Favorite Browser:
- Chrome
- Favorite Processor:
- Intel
- Favorite Gaming Platform:
- Who Cares
- Your Car:
- Ford
- Dream Kudos:
- 100
- Expert In:
- Java
Posts I've Made
-
In Topic: how to make a mouse listener / key listener
Posted 24 May 2013
I'm not sure either approach is particularly easier than the other, but both are useful and important in the right situation. Moving a graphic object to the point a mouse is clicked is a tool you should definitely have in your toolbox. Here's a simple demo that shows how to do that. Let me know if you have any questions.
import java.awt.Color; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import javax.swing.JFrame; import java.awt.Graphics; import javax.swing.JPanel; import javax.swing.SwingUtilities; import java.awt.Dimension; public class MouseClickDemo extends JFrame { SquarePanel squarePanel; // the default, no-argument constructor public MouseClickDemo() { setTitle("Mouse Click Demo"); setLocationRelativeTo(null); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); squarePanel = new SquarePanel(); add( squarePanel ); pack(); } // end no-argument constructor // a main() method to create an instance of MouseClickDemo // on the EDT and to set it visible public static void main (String[] args) { SwingUtilities.invokeLater( new Runnable() { public void run() { MouseClickDemo frame = new MouseClickDemo(); frame.setVisible(true); } } ); } // end method main() final class SquarePanel extends JPanel implements MouseListener { // instance variables. x, y are used to capture the cursor // coordinates when the mouse is clicked private int x; private int y; final int SQUARE_WIDTH = 50; final int SQUARE_HEIGHT = 50; final int PANEL_WIDTH = 800; final int PANEL_HEIGHT = 800; // the default, no-argument constructor to add the mouse listener public SquarePanel() { addMouseListener( this ); setPreferredSize( new Dimension( PANEL_WIDTH, PANEL_HEIGHT ) ); setBackground( Color.LIGHT_GRAY ); } // end constructor // the paintComponent() method updates the rectangle when called by // the repaint() method, either by the system or manually by the // mousePressed() method public void paintComponent( Graphics g ) { super.paintComponent( g ); // choose a color for the rectangle to be drawn g.setColor( Color.RED ); g.fillRect( x, y, SQUARE_WIDTH, SQUARE_HEIGHT ); } // end method paintComponent() // ************* the required mouse listener methods ************** // the mouseClicked() method 'seems' like the natural place to control // the application, but mouseClicked events are not registered if ANY // mouse movement is detected. use the mousePressed() method instead. @Override public void mouseClicked( MouseEvent me ) { } // end method mouseClicked() // the mousePressed() method creates a new group of squares that are // added to the squares array which will originate from the // point of the mouse click as defined by x, y @Override public void mousePressed( MouseEvent me ) { // store the current cursor coordinates x = me.getX(); y = me.getY(); repaint(); } // end method mousePressed() @Override public void mouseReleased(MouseEvent me) {} @Override public void mouseEntered(MouseEvent me) {} @Override public void mouseExited(MouseEvent me) {} } // end class SquarePanel } // end class MouseClickDemo -
In Topic: transforming a three different arrays into multidimensional array
Posted 24 May 2013
I think when you say "associated with each other," you mean that that the array's rows are ordered (or sorted) by the first element of each row. Is that right? If so, parse the file into an array just as you've described, and then sort it by the first element in each row.
If I'm not understanding, please explain better.
You might also consider using a class of Person or Employee and use a collection of dates and times in each class to store the same attributes you're loading into the arrays. But that would be an OOP way, and you may not be there yet. -
In Topic: error in my program, assistance?
Posted 23 May 2013
If you're using StdIn from Princeton's stdlib library because you have to or it's critical to your study, then be sure you're importing it correctly and/or stdlib.jar is in your CLASSPATH. I haven't used it myself, so I'm not sure how it is typically used, but you must have been told somewhere how to include it. -
In Topic: Partitioning a matrix
Posted 23 May 2013
The error line:
X.matrix[i][j] = ZZ.matrix[i][j];
By definition, the X matrix is going to be larger than the ZZ matrix. At some point, the X matrix will have to be filled with zeros rather than elements from the ZZ matrix, because those ( i, j ) elements do not exist in the ZZ matrix.
Edit: A simple solution is to stay consistent with Java's standards and bound the equation by ZZ rather than X.
I don't know what values the X.matrix[][] contains after this statement:
StrassenMatrix X = new StrassenMatrix(nextPow);
but if it were a standard Java array, it would be filled with zeros. If filled with zeros, the padding would be done, and all that would remain to achieve the desired result would be to copy the values from ZZ to X, using ZZ's dimensions, not X's.
That help? -
In Topic: I want to calculate area of triangle using herons formula using method
Posted 23 May 2013
You should be getting an error for line 40, fairly self-explanatory. If you comment out that line (for now), your program will compile and run.
Other than that, ask for specific help. It appears you're on your way to accomplishing what you've described but are unable to recover from minor stumbles. You should "play around" as needed to discover corrections to minor problems, but if you're completely stumped, just ask.
My Information
- Member Title:
- D.I.C Lover
- Age:
- Age Unknown
- Birthday:
- Birthday Unknown
- Gender:
-
Contact Information
- E-mail:
- Private
Friends
-

h4han
-

herlambang
-

wifi34
-

macosxnerd101
-

superman04
-

Elcric
-

samdoor
-

cpe
-

Suffiyanish
-

linuxnut
-

amture106
-

Mylo
-

New Java
-

kyffrb124
-

jruss1212
-

skymonkier
-

harrypotter2
-

Kawenlou
-

BudgieBr0phy
-

ric1989
-

Martyr2
-

pascal7
-

BrendanH
-

mohsen-matrix
-

Javacodes
-

JoePatriot
-

SilverEleak
-

xonewinged...
-

vbabey
-

aravindbal...
-

dasrhino
-

Ytry
-

rbk3ai
-

raghav.nag...
-

lilVaratep
-

mardziah87
-

heathbb
-

WolverineX
-

BobtheCat1er
-

Quang Pham
-

Odko
-

crisdeveloper
-

coolbud012
-

Garg
-

hambonie
-

Anuben
-

Flukeshot
-

ssaktheevel
-

jjh08
-

ruymonroy
Showing 50 random friends of 55 (View all)
|
|


Find Topics
Find Posts
View Reputation Given
|
Comments
CoolTech
24 Feb 2013 - 16:06