Jump to content


Check out our Community Blogs

lethalwire

Member Since 15 Dec 2010
Offline Last Active Mar 19 2016 09:27 AM
*****

#642520 visibility of attributes when class is private?

Posted by lethalwire on 21 October 2012 - 10:52 AM

Hi everybody here!! I hope you all alright
I'm not good in Java, my problem is with a basic concept: what's the difference of private, public, friendly modifiers when applied to attributes, in the case of a >private class< (I think there is no difference!, we can't read these attributes from outside the class, right?), I hope my question is clear,
Thank's in advance.


It depends. You can read those attributes if they come from an inner private class regardless of if they're private/public. The class that can read those attributes is the outer class.

Example:

public class OuterClass {
    
    private class InnerClass {
        private int value = 4;
    }
    
    public void printInner() {
        InnerClass inner = new InnerClass();
        System.out.println(inner.value);
    }
    
    public static void main(String[] args) {
        OuterClass outer = new OuterClass();
        outer.printInner();
    }
}


This example compiles and executes with an output of
4

  • 1


#642114 [SOLVED]Determine all (i,j) pairs such that A[i] = A[j]

Posted by lethalwire on 16 October 2012 - 11:44 AM

In the following way, I'm thinking you could acquire O(n), linear time:

The following assumes you know the maximum value of the numbers within the array and that they are integers.
With this method, you can use the indices of a secondary array to determine where to find duplicates.
So let's say you create a class Duplicate and you know the values in your array range from [0, 20,000) exclusive.
You can then create an array Duplicate duplicates[20,000].
So if your array contains the following [1, 2 1, 10, 15, 15, 0] then at position 0 of duplicates, you'll hold a list which contains 6. (Because the number zero is located at position 6).
Position 1 of duplicates would then hold a list that contains 0 and 2. (Because the number 1 is located at position 0 and 2)
Position 2 of duplicates would then hold a list that contains 1. (Because the number 2 is located at position 1)
...

Then the algorithm would look something like:
for( int i: i < array.length; i++ )
  duplicates[array[i]].addDuplicateIndex(i)

The obvious downside of this is algorithm is space requirements. But, if you know you've got a small range to work with, then this method could be feasible.
  • 1


#629000 New Feature: Dropdown Menu Across Top Navigation Bar

Posted by lethalwire on 03 May 2012 - 06:11 PM

Very nice. This is much more convenient.
  • 1


#628999 Update: Default Blog Names Renamed

Posted by lethalwire on 03 May 2012 - 06:10 PM

That actually looks pretty nice.
"Lethalwire's blog"
  • 1


#627964 A Well Coded Prank

Posted by lethalwire on 24 April 2012 - 10:27 AM

Lol, I am too not too fond of pranks. Just curious, he didn't ever compare the file sizes? Or your code is so good that the sizes stayed the same?


I don't even think he thought about comparing sizes. He automatically pointed his finger at the mp3 player! :) That's why it's so funny haha.

I don't even think he thought about comparing sizes. He automatically pointed his finger at the mp3 player! :) That's why it's so funny haha.


Posted Image
  • 2


#627252 Java Login Dialog [Part 1]

Posted by lethalwire on 17 April 2012 - 07:24 PM

Long time no see Alien. Try the tutorial. It should be easy to follow. If you have any questions, you know where to ask your questions. ;)
  • 1


#625565 Making A Mouse Click?

Posted by lethalwire on 04 April 2012 - 05:39 PM

Is this useful for you?
http://www.java2s.co...gRobotclass.htm

You'll just need to add a timer that will click every x seconds.
  • 1


#625479 CODECALL migrated to IPBoard 3.3

Posted by lethalwire on 03 April 2012 - 07:21 PM

The mobile version of CC was great. The only thing that went wrong was that this exact thread wasn't showing any replies. Yet there were at least 2 pages of replies.

I jumped out of the mobile version, but I'll be setting it back.

The mobile interface seemed pretty responsive.

I'm trying to find a way to show more than X posts per page though. I can't seem to find anything.
  • 1


#620628 Executor.execute() - At the discretion of the Executor?

Posted by lethalwire on 27 January 2012 - 01:13 PM

The newCachedThreadPool() method returns a ThreadPoolExecutor object.
The execute() method of the ThreadPoolExecutor object states that:

Executes the given task sometime in the future. The task may execute in a new thread or in an existing pooled thread. If the task cannot be submitted for execution, either because this executor has been shutdown or because its capacity has been reached, the task is handled by the current RejectedExecutionHandler.

So to me, it would seem that this implementation of Executor does NOT try running the command in the calling thread.
  • 1


#620090 Help executing mySQL query

Posted by lethalwire on 21 January 2012 - 07:39 AM

I'm using netbeans and when I'm setting up a db application it makes a successfull connection to the DB...

You say you are making a successful connection, but turn around and say that those lines of code aren't working.

Where are you able to make the successful connection? And then why are you now not able to make a successful connection?

---------- Post added at 09:39 AM ---------- Previous post was at 09:37 AM ----------

Do you also have the correct driver in your directory? It could be a ClassNotFoundException.
  • -1


#618479 Java Login Dialog [Part 1]

Posted by lethalwire on 31 December 2011 - 01:45 PM

Objectives:
This tutorial focuses on creating a customized login dialog.

Prerequisites:
A fair amount of knowledge of Java is expected before beginning this tutorial.
Some intermediate level concepts that you'll see are:
  • Extending other JComponents (JPanel, JDialog)
  • Overriding methods
  • The final keyword
  • The static keyword
  • Use of Image and ImageIcon
  • Use of primitive arrays[]
  • ActionListeners
  • JComponents
Step 1 - Sketch Out the Idea
sketchOfLoginDialog.png

Step 2 - Decide What Classes Are Relevant For Your Idea
  • JPanel - Used for the custom background image
  • JDialog - Used to create the Dialog Window & will hold the UI components
  • JButton - Login/Cancel buttons
  • JLabel - Used to Label the text fields
  • JTextField - A text area to show the username
  • JPasswordField - A special text field used to mask a password

Step 3 - Programming the JPanel - BackgroundJPanel.java

Extending JPanel and overriding JPanel's paintComponent(Graphics g) should give us the effect we need to fulfill our custom background.
public class BackgroundJPanel extends JPanel {

In the constructor, we'll take a String parameter that gives us a url pointing to the background image.
We use an Image object to store the image data and use this data to draw the image onto the JPanel.
	private final Image backgroundImage;
	
	public BackgroundJPanel(String imagePath) {
		super();
		backgroundImage = new ImageIcon(imagePath).getImage();
		if(getBackgroundImage() != null)
			setPreferredSize(new Dimension(backgroundImage.getWidth(null), backgroundImage.getHeight(null)));
	}

getbackgroundImage(...) is a simple getter method that is defined as follows:
    private Image getBackgroundImage() {
	return backgroundImage;
    }


Now, we'll override paintComponent(Graphics g) method and draw the background image
	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		
		Image img = getBackgroundImage();
		if(img != null){
			g.drawImage(img, 0, 0, null);
		}
	}

Step 4 - Programming the Dialog - PasswordJDialog.java

Extending JDialog will give us the capabilities of existing JDialogs. On top of this, we'll add our own implementation to the dialog to create the custom login look.
public final class PasswordJDialog extends JDialog {

The only class variable we have is a
private final JPanel backgroundPanel;
We'll add the textfields, labels, and buttons to this panel.

In the constructor, we initialize the backgroundPanel and tell the panel where to find the background image.
Next, we initialize the components needed to fulfill the idea. (JTextAreas, JButtons, JLabels, etc.)
Next, we add the backgroundPanel to the dialog then display the dialog.
	private PasswordJDialog() {
		backgroundPanel = new BackgroundJPanel("http://forum.codecall.net/images/backgrounds/login.png");
		initComponents();
		this.setContentPane(backgroundPanel);
		this.setUndecorated(true);
		this.pack();
		this.setLocationRelativeTo(null);
		this.setVisible(true);
	}

The only undefined portion of code thus far is the initComponents() method.
In the body of this method is where the construction of the UI takes place.

The code is pretty straight forward. We declare the components that we stated in step 2 and initialize them.
The componentPanel JPanel is used to hold the components in a tighter area. Note that this panel is also uses a GridLayout to place the components into a 3x3 grid.

componentPanel.setOpaque(false) is called to make the background of this JPanel completely transparent.
After all components have been added to the componentPanel; the componentPanel is added to the backgroundPanel.

	private void initComponents() {
		JButton[] buttons = new JButton[2];
		JTextField usernameField = new JTextField(10);
		JPasswordField passwordField = new JPasswordField(10);
		passwordField.setEchoChar('*');
		JLabel[] labels =new JLabel[2];
		
		buttons[0] = new JButton("Login");
		buttons[1] = new JButton("Cancel");
		labels[0] = new JLabel("Username: ");
		labels[0].setHorizontalAlignment(JLabel.CENTER);
		labels[1] = new JLabel("Password: ");
		labels[1].setHorizontalAlignment(JLabel.CENTER);
		
		buttons[0].addActionListener( new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// your turn
			}
		});

		buttons[1].addActionListener( new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				close();
			}
		});
		
		JPanel componentPanel = new JPanel();
		componentPanel.setPreferredSize(new Dimension(250,100));
		componentPanel.setOpaque(false);
		componentPanel.setLayout(new GridLayout(3, 3, 5, 5));
		componentPanel.add(labels[0]);
		componentPanel.add(usernameField);
		componentPanel.add(labels[1]);
		componentPanel.add(passwordField);
		componentPanel.add(buttons[0]);
		componentPanel.add(buttons[1]);
		
		backgroundPanel.add(componentPanel);
	}

The undefined method close() is simple to understand and is as follows:
protected void close() {
		this.dispose();
	}

In order to show this dialog, we create the following static method:
public static void showPasswordDialog() {
		new PasswordJDialog();
	}
To see your dialog, you'll simply make a call that looks like:
PasswordJDialog.showPasswordDialog();

Result:
result.png

Backgrounds:
login3.png

Part 2: http://forum.codecal...g-part-2-a.html

Attached Thumbnails

  • login2.png
  • login.png

  • 2


#614201 What's the easiest way of reading text files?

Posted by lethalwire on 15 November 2011 - 03:55 PM

Is (char)10 valid as a newline in all operating systems?

Depending on the data I'm reading, I'll switch from BufferedReader to Scanner.
When I want to quickly read different types of data (ints, doubles, longs, tokens, etc) I'll use Scanner for the convenience parsing methods given with this class.

When I'm reading other data, like longer strings, or a string composed of 1 or 2 other values, I'll use a BufferedReader and just parse the data myself.

Using a BufferedReader is also faster than a Scanner(probably due to buffer size?).

Now I'm wondering if it'll be any faster if you wrap a BufferedReader inside of Scanner
  • -1


#611481 Is there an easier way to program a 3 dice game in Java?

Posted by lethalwire on 16 October 2011 - 12:27 PM

I'm not understanding your logic.

Is 3 1's equal to 100 points also?
Is 3 2's equal to 100 points also?
...
is 3 5's equal to 100 points also?

From my understanding of what you have, only 3 6's are worth 100 points. Everything else is considered 10 points.
In that case this will work:
if(dice1 == 6 && dice2 == 6 && dice3 == 6)
{
    //Give 100 points to user
} else { // any other case
    //give 10 points to user
}

If it's not the case you could write:
boolean rewarded = false;
for(int i = 1; i <= 6 && !rewarded; ++i) {
    if( dice1 == dice2 && dice2 == dice3 && dice3 == i ){
          //reward 100 points
          rewarded = true;
    }
}
//reward 10 points if rewarded = false.

  • 1


#610448 Witch language use more using JAVA or C#?

Posted by lethalwire on 02 October 2011 - 08:12 PM

Java is the most popular language in the world. :-P
  • -1


#598147 Counting instances of words in a file, best way?

Posted by lethalwire on 26 April 2011 - 04:41 PM

StringTokenizer parser = new StringTokenizer(currentLine, " \t\n\r\f.,;:!?'[COLOR="red"]\"[/COLOR]");

  • 1




Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download