Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

Bookmark the Swing tutorials.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

Perhaps you need to look up pseudocode then, because you have nothing like it.

You need to outline the steps in a logical format that describes the process the program needs to complete.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

Only if you start with an effort to outline your thoughts on how to implement the program or some code that you have written to start with.

Write it in pseudocode if you don't understand how to do it in Java and ask specific questions about the portions that you are unsure of.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

"stinkin java won't read the "sdf"" because you haven't "stinkin" declared it properly in any of the code you posted. You would need to do this:

SimpleDateFormat  sdf = new SimpleDateFormat ("EEEE MMMM DD, YYYY");

and even then, masijade's right - it isn't going to work your class just because you called it "date".

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

You can't - it's null. It has no value and the only context your method has for a relationship to a class is the parameter type, which you have denoted as Object.

You don't mention the larger context of this usage, but it raises suspicion of a design problem that would be better addressed with an interface for the parameter or behavioral design pattern.

Edit: Posted at the same time as masijade. Answer pertains to the original question.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

There are some tricky nuances to clone() and you are probably best served by using a copy constructor in it's stead.
http://www.javapractices.com/topic/TopicAction.do?Id=71

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

There are no macros in Java. You would define a class or interface constant for that purpose.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster
Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

He means references to them.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster
Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

No, Im trying to help him with the simplest way as you can see he is newbie.

Even, im a newbie. im just sharing my knowledge here :) If im wrong, you seniors are here to help us.

Regards,
PuneetK

No, you fixed some of his code and then provided no explanation of what you did or why, which does nothing to promote learning. javaAddict was merely trying to provide the explanation that you did not bother to give.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

Just to answer the question on waiting for one thread to finish before starting the next, here's a trivial example using the join() method

public class JoinExample {

    public static void main(String args[]) {
        Thread t1 = new Thread(new FirstTask());
        Thread t2 = new Thread(new SecondTask());
        
        t1.start();
        try {
            // wait for t1 to complete
            t1.join();
        } catch (InterruptedException ex) {
            Thread.currentThread().interrupt();
        }
        // start the next
        t2.start();
    }
    
    static class FirstTask implements Runnable {
        int counter=0;
        
        public void run() {
            try {
                while (counter<10){
                    System.out.println(++counter);
                    Thread.sleep(500);
                }
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        }
    }
    
    static class SecondTask implements Runnable {
        int counter=64;
        
        public void run() {
            try {
                while (counter<74){
                    System.out.println(Character.toChars(++counter));
                    Thread.sleep(1000);
                }
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        }
    }
}
peter_budo commented: Thank you for advice +10
Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

I'm curious, why you are wanting to use separate threads for what seems to be a synchronous process? You describe wanting to process another song after the current one is finished, but haven't mentioned anything that would require a concurrent process.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

Yes, the Threads in your example don't really do anything. You need to extend Thread and override run() or supply a Runnable to the constructor. Have you looked through the tutorial on concurrency as a starting point?
http://java.sun.com/docs/books/tutorial/essential/concurrency/runthread.html

The java.util.concurrent package adds a lot of new higher level support structure for asynchronous operations as well. It's covered further towards the end of the tutorial trail.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

Actually, disregard the comment on the inner class. I just noticed that it is not actually an inner class of ColorForms.

Since the "jumping" occurs on a top-level repaint of the containment heirarchy and CanvasPanel is a component of "bottomPanel", the new transform seems to be created relative to the bottomPanel graphics context when the paintComponent() call comes down from that container. Your other events are calling repaint directly on the CanvasPanel and the transform is fine on those.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

The problem is originating with your creation of the affine transform that you are rotating:

AffineTransform af = new AffineTransform ();

If you change that to pass the original transform to the constructor, it works just fine:

AffineTransform af = new AffineTransform (orig);

Without backtracing the stack on every repaint() call, I can't tell you for certain, but I would guess the new transform is getting generated against the frame context when you resize and using the CanvasPanel on the other events. The fact that it's an inner class of another visual component further clouds the issue.

Honestly, you don't need to create a new transform from scratch anyway. Just call rotate directly on the graphics context

g2.rotate(angle, ovalCenterX, ovalCenterY);

and it behaves just fine.

Also, if you move the setVisible() call to the end of the ColorForms constructor, it will paint the components you added properly on construction.

VernonDozier commented: Very helpful. +7
Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

Perhaps you should look through this for some clarification: http://java.sun.com/docs/books/tutorial/java/concepts/index.html

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

No, each employee is it's own instance. The class is just a template for that instance. All Employee objects would have the same property fields, but the values of those properties are different for each employee.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

Well, go through each of those methods and ask yourself, "Does this data or action apply to one and exactly one particular employee?". If it does, it goes there. If it doesn't then it goes in some other class that manages that particular functionality.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

Everyone learns at some point :)

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

It is declared, yes, but it is not initialized

Stopwatch Clock = new Stopwatch();

(It really should be "clock" instead of "Clock". Common convention is for variables to start with a lowercase letter. Classes are capitalized.)

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

No, it would go in an Employee class, which would contain all of the data pertaining to a particular employee.

The payroll program works with employees, but is not itself an employee. Class design is an exercise in separation of responsibilities.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

Recommended reading:
http://java.sun.com/docs/codeconv/html/CodeConvTOC.doc.html

Good reference for general Java language topics:
http://www.codeguru.com/java/tij/

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

Study the information found in the "Read Me: Starting Java" thread that is stickied at the top of this forum.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

i don't know weather it is involved in a network or not. how i should know wheather it is involved in a network or not.

If he's not on a network, how do you figure he's going to have an IP address? Do you know what an IP address is for?

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

Spilling is most definitely a drinking problem.

(... a problem of having less to drink, obviously)

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster
Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

I am a drinker and have spilled drinks on others. Will I be punished?

Most definitely.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

You are going to have to apply thresholding methods to determine that the photos meet whatever criteria you deem close enough to identical. You will most likely never get two different photos with the exact same pixel values in the exact same positions. The field of computer vision can get very complex quickly depending upon your expectations.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

How you compare the images is completely up to you. There are many techniques for image comparison and it depends on what you are trying to resolve from the two. All of them are going to use pixel values at some level of granularity.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

http://java.sun.com/j2se/1.4.2/docs/guide/imageio/spec/imageio_guideTOC.fm.html

("plss" and "thx" are not words. If you're too lazy to communicate properly, don't expect too much help.)

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

God will punish smokers not for what they are doing to themselves, but for what they are doing to others.

Sure, and leprechauns will have their vengeance too, because they are really tired of ash falling on their heads. :-/

Are there any other dire warnings of retribution from make-believe land that we should be aware of?

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

That is just a method declaration, not a constructor. As already stated above, constructors do not specify a return type because they always return an instance of that class.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

" tried to help anywayz." ?
Odd way of thanking those who gave you information based upon your less-than-precise posts. If by "tried to" you mean "didn't write the code for you", then perhaps you misunderstood their intent.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

Yes, some of their examples can be more complex than need be and obscure the usage they are trying to demonstrate. :(

One more thing worth noting with respect to AffineTransforms is that they represent the current state of the coordinate system within the graphics context. Since you can store references to as many AffineTransforms as you want, you can effectively "save" any state (translation, orientation, scale, and sheer) you wish and restore it at will. If you have many objects that render themselves independently, each can maintain it's own transform that it uses for rendering. When that object's turn to be rendered comes, you can save the current transform of your graphics context, apply that object's transform, render it, and then restore the original transform.

Here's an example of that:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.geom.AffineTransform;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class TransformExample extends JFrame {

    GraphicPanel gPanel;

    public TransformExample() {
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        gPanel = new GraphicPanel();
        getContentPane().add(gPanel);
        setBounds(100, 100, 200, 200);
        setVisible(true);
    }

    class GraphicPanel extends JPanel {
        TextLabel label1;
        TextLabel label2;
        TextLabel label3;

        protected void paintComponent(Graphics g) {
            if (label1 == null) {
                // lazy initilization so I have dimensions to work with
                FontMetrics fm = g.getFontMetrics();
                int width = getWidth();
                int height = getHeight();
                
                label1 = new TextLabel("Hi Vernon", Color.YELLOW, 
                  20, height/2, -Math.PI/2);
                
                String labelText = "I'm over here";
                int labelLen = fm.stringWidth(labelText);
                label2 = new TextLabel(labelText, Color.CYAN, 
                  width-labelLen, height/2, …
VernonDozier commented: Exactly what I was looking for! +6
Alex Edwards commented: Excellent example of professional programming. +3
Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

If you bothered to read the link he posted, in Drawing Geometric Primitives, they discuss the rounded rectangle and oval both.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

You have made this a bit more complex than need be. For simple transformations, the Graphics2D methods rotate() and translate() will suffice (they additively modify the current AffineTransform of the graphics context) without any need to obtain or work with an AffineTransform object directly.

I wrote this small example that might help

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class TextExample extends JFrame {

    GraphicPanel gPanel;

    public TextExample() {
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLayout(new BorderLayout());
        gPanel = new GraphicPanel();
        getContentPane().add(gPanel, BorderLayout.CENTER);
        setBounds(100, 100, 200, 200);
        setVisible(true);
    }

    class GraphicPanel extends JPanel {
        protected void paintComponent(Graphics g) {
            Graphics2D g2 = (Graphics2D)g;
            g2.setBackground(Color.BLACK);
            g2.clearRect(0, 0, getWidth(), getHeight());
            g2.setColor(Color.WHITE);
            String msg = "Hi Vernon";
            FontMetrics metrics = g2.getFontMetrics();
            // translate context to center of panel
            // the point 0,0 now resides here
            g2.translate(getWidth() / 2, getHeight() / 2);
            g2.drawString(msg, -metrics.stringWidth(msg) / 2, 
              metrics.getMaxAscent() / 2);
            // rotate 45 deg
            g2.rotate(-Math.PI / 4);
            g2.setColor(Color.YELLOW);
            g2.drawString(msg, -metrics.stringWidth(msg) / 2, 
              metrics.getMaxAscent() / 2);
            // rotate 45 deg again - these transforms are additive
            g2.rotate(-Math.PI / 4);
            g2.setColor(Color.ORANGE);
            g2.drawString(msg, -metrics.stringWidth(msg) / 2, 
              metrics.getMaxAscent() / 2);
        }
    }

    public static void main(String... args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                new TextExample();
            }
        });
    }
}
Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

i don't understand how byte stream could read images. does it need help of applets or else.

Images are comprised of bytes.
You want to read them.
Byte streams read bytes.
You use a byte stream.

...

Alex Edwards commented: Spoon feed-- +3
Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

I'm just getting to the point I won't read code that isn't in code tags. I may reply that they need to post the code in tags, but if the code isn't formatted the question can go unanswered for all that I care.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

I assume you are running it in the AppletViewer? I think that close button is part of a window created by the applet viewer itself. Since applets are designed to run in a browser, they don't really have a close button themselves. They have life cycle methods that respond to browser events, like leaving the page or closing.
http://java.sun.com/docs/books/tutorial/deployment/applet/lifeCycle.html
You will probably need to put that action code in one of those life cycle methods.
(I've never had use for an applet personally so I would just imagine that would be the place to handle it)

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

If you can use them, key bindings are easier to handle with regards to focus:
http://java.sun.com/docs/books/tutorial/uiswing/misc/keybinding.html
but if you can't use those then you could call requestFocusInWindow() on the JApplet (or whatever other container you need to have focus) at the end of your button listener code

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JOptionPane;


public class FocusTest extends JApplet {
 
    JButton button;

    public void init() {
        button = new JButton("Ok");
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                requestFocusInWindow();
            }
        });
        setLayout(new BorderLayout());
        add(button,BorderLayout.SOUTH);
        
        setFocusable(true);
        addKeyListener(new KeyAdapter() {
            public void keyPressed(KeyEvent e) {
                JOptionPane.showMessageDialog(FocusTest.this, "Key pressed");
            }
            
        });
    }
}
Alex Edwards commented: You rock! +3
Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

Does the Pope really wear red shoes?

http://en.wikipedia.org/wiki/Papal_shoes

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

Alex, the point was to keep in mind that other classes in the JDK, such as Runtime, System, the class loader, etc. are also allocating and using objects and primitives internally. The total memory reflects all of those things, not just the ones you are creating in your own code directly.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

> ok, but why would bluej do things differently?

I really couldn't say. Netbeans and BlueJ both are interacting with the JVM when they run your program and freememory and totalmemory make no distinctions about the specifics or source of that allocation. If you want to examine the specifics you need to be working with heap dumps instead of raw aggregate memory numbers like those.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

If you look at a heap dump taken on the last line of main(), at which you print the memory, you'll see where that extra memory is going:

There is a lot of stuff on the heap beyond those three objects you are looking at.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

Was it a Baptist church?
:twisted:

scru commented: hehe +3
Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

"Our world has grown weary of greed, exploitation and division, of the tedium of false idols and piecemeal responses, and the pain of false promises," he told the crowd.

So he's saying the world has grown weary of the church?

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

Can any one tell about the difference between the Abstract Class and Interface?

Abstract classes can contain implementations of some methods, while interfaces cannot. This allows abstract classes to serve as a base implementation of a class hierarchy that shares some (or much) common code and require that subclasses implement only the portions that are specific to themselves.

Can abstract class replaces Interface?

In some cases, yes, in others, no. See next comment below.

If all things can be done in Abstract class Why we need Interface?

Java only allows for single inheritance of classes, but the number of interfaces implemented is unlimited. Use of interfaces allows classes to act as more than one type of object. Without interfaces, all methods that you may want a group of classes to share must be stuffed into a common base class. What if some of these methods only apply to some classes but not others? Your class hierarchy would become a polluted mess, littered with methods that may not even apply to a particular class. Interfaces operate as a contract that guarantees a class implements one or more methods without forcing them to descend from a common base class. Many different kinds of class hierarchies may want to be Printable, or Comparable, or Displayable, without having to extend from some common ancestor. That can only be accomplished with interfaces.

You read more about this here if you wish: http://www.codeguru.com/java/tij/tij0080.shtml

Alex Edwards commented: Expert Answer +2
Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

You could also purchase the book Head First Java

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

Glad it worked for you.