hax95's Profile User Rating: -----

Reputation: 7 Worker
Group:
New Members
Active Posts:
8 (0.02 per day)
Joined:
31-July 13
Profile Views:
163
Last Active:
User is offline Aug 12 2013 09:44 AM
Currently:
Offline

Previous Fields

Country:
IT
OS Preference:
Windows
Favorite Browser:
Chrome
Favorite Processor:
Who Cares
Favorite Gaming Platform:
Who Cares
Your Car:
Who Cares
Dream Kudos:
0
Icon   hax95 has not set their status

Posts I've Made

  1. In Topic: java2D repaint() not working

    Posted 8 Aug 2013

    the main problem is that when you declare a thread in the static main method, it's not inside the frame, so, it's not showed.Like GregBannon said, you have to reference the thread inside the GUI() method, with a statment like this:
    		//The class that you want to add in the frame isn't an external class
    		//then you reference the class with 'this' statement
    		frame.add(this);
    		//The same for thread
    		Thread t1 = new Thread(this);
    		//finally you start the Thread
    		t1.start();            
    
    
  2. In Topic: MouseListener only triggering on top-level JPanel

    Posted 7 Aug 2013

    [/quote]
    What I'm doing is creating an array of Rectangle objects that are generated to have random x and y coordinates. As the user clicks on a drawn Rectangle, the method repaints using the next Rectangle in the array. I plan to implement a timer feature to see how quickly one can click all ten random Rectangles in succession
    [/quote]

    if you want to take the time, you can simply use a variable that take the count of the generated rectangles,and add Rectangles dinamically. When the variable reach 10, you stop the program, make a message,or similar.
  3. In Topic: MouseListener only triggering on top-level JPanel

    Posted 7 Aug 2013

    why you don't want to proceed with the Rectangle Method?
    I don't think that JPanel is supposed a component to be re-arranged by user click dinamically.
    I suggest you to thake the second approach.
    Then: why you are using an array, when you can create dinamically rectangles, with a random position, and
    display them?
    import javax.swing.*;
    import java.util.*;
    import java.awt.*;
    import java.awt.event.*;
    
    class click extends JFrame {
    	public static void main(String[] args) {
    		SwingUtilities.invokeLater(new Runnable() {
    			public void run() {
    				createGUI();
    			}
    		});
    	}
    
    	private static void createGUI() {
    		JFrame f = new JFrame();
    		f.setPreferredSize(new Dimension(640,480));
    
    		Container c = f.getContentPane();
    		GamePanel gp = new GamePanel();
    		c.add(gp);
    		f.pack();
    
    		f.setDefaultCloseOperation(EXIT_ON_CLOSE);
    		f.setVisible(true);
    	}
    }
    
    class GamePanel extends JPanel {
    	private int size = 30;
    	private Random r = new Random();
    	private Rectangle currentRect= getRandomSquare(size);
    
    	public GamePanel() {
    		addMouseListener(new MouseAdapter() {
    			public void mouseReleased(MouseEvent e) {
    				//if mousereleased inside bounds of generated rectangle
    				//create a new rectangle, erase old one
    				if (currentRect.contains(e.getX(),e.getY()) != false) {
    					currentRect = getRandomSquare(size);
    					repaint();
    				}
    			}
    		});
    	}
    
    	public void paintComponent(Graphics g) {
    		super.paintComponent(g);
    		Graphics2D g2d = (Graphics2D)g;
    		g2d.draw(currentRect);
    	}
    
    	public Dimension getPreferredSize() {
    		return new Dimension(640,480);
    	}
    
    	private Rectangle makeSquare(int x, int y, int s) {
    		return new Rectangle(x,y,s,s);
    	}
    
    	public Rectangle getRandomSquare(int s) {
    		return makeSquare(r.nextInt(640-s),r.nextInt(480-s),s);
    	}
    
    }
    
    
    
  4. In Topic: Arraylist of JLabels problem

    Posted 7 Aug 2013

    If you want to add dinamically JLabels to the GUI, you can take that approach:
    separate the role of the classes: you can build a JFrame extending class, where you control
    all elements: in this class, you reference a panel where you print the position of the mouse,
    a MouseListener that catch the mouse events, and finally, a class where you can add labels.
    class MyJFrame extends JFrame{
    	protected int mouseX;
    	protected int mouseY;
    	protected int mX;
    	protected int mY;
    	
    	//construct the frame, and instantiate the various classes
    	public MyJFrame(){
    	
    		//you will use BorderLayout
    		setLayout(new BorderLayout());
    		mouseX = 100;
    		mouseY = 100;
    
    		//i will explain you why i use references between classes, when i show
    		//you  the classes
    		
    		//the class that add labels
    		Labels labels = new Labels(this); 
    		
    		//the class that catch mouse events
    		MyMouseListener listener= new MyMouseListener(this,labels);
    	
    		//the class that show mouse position
    		MyJPanel panel = new MyJPanel(this);
    		
    		
    		//Here you add code to put the labels at left, and remaining will
    		//be needed for the panel
    		add(panel,BorderLayout.CENTER);
    		add(labels,BorderLayout.EAST);
    		
    		//you use the grid layout and show the labels in 1 column
    		labels.setLayout(new GridLayout(0,1));
    		
    		
    		//Then you ad the mouse listener using addMouseListener() method
    		
    	}
    }
    
    


    the previous class reference other classes:

     class MyJPanel extends JPanel {
    	 MyJFrame frame;
    	 
    	public MyJPanel(MyJFrame frame){
    		//here, you will reference the frame, because, you have to access
    		//at the frame variables for the mouse state
    		this.frame = frame;
    	}
    	
    	public void paintComponent(Graphics g)
    		//here you add the code to draw strings, referencing the mouse state that is in the               
                   //frame object
    			
    	}
    }
    
    


    class MyMouseListener implements MouseMotionListener,MouseListener{
    	MyJFrame frame;
    	Labels labels;
    	public MyMouseListener(MyJFrame frame,Labels labels){
    		/*here you reference the MyFrame and Labels classes
    		because, when mouse is moved/pressed, you call repaint on the
    		frame, when you press the mouse you also add a label
    		*/
    		this.frame = frame;	
    		this.labels= labels;
    	}
    	public void mousePressed(MouseEvent e){
    		//Here you set the value of the mX and mY of the frame class
                    //This statement, add a label to the panel Labels
    		labels.add(frame.mX,frame.mY);
    		//then you call repaint() on the frame class, that cause the repaint of every subclasses
    	}
    	public void mouseMoved(MouseEvent e){
    		//Here you set the value of the mouseX and mouseY of the frame class
    		//then you call repaint() on the frame class, that cause the repaint of every subclasses
    	}
    	public void mouseDragged(MouseEvent e){
    	}
    	public void mouseReleased(MouseEvent e){}
    	public void mouseClicked(MouseEvent e){}
    	public void mouseEntered(MouseEvent e){}
    	public void mouseExited(MouseEvent e){}
    }
    
    


    class Labels extends JPanel{
    	JFrame frame;
    	
    	public Labels(JFrame frame){
    	//In that class, you reference the frame to call the validate method
    	this.frame = frame;
    	}
    	
    	public void add(int x, int y){
    		JLabel label = new JLabel(x+" "+y);
    		label.setBorder(BorderFactory.createEtchedBorder());
    		label.setBackground(Color.BLUE);
    		label.setForeground(Color.RED);
    		add(label);
    		//the validate method is essential for this program, it's the key that open
    		//the GUI of java: with this method, when you call validate on a class that
                    //extend Container  cause a container to lay out its subcomponents again. It should be
                   //invoked when you add or remove elements (MyJFrame is a subclass of Container, then
                   //it's a conteiner, Labels, is a subcomponent of MyJFrame
    		frame.validate();
    	}	
    }
    
    
  5. In Topic: incorrect results from if statement

    Posted 3 Aug 2013

    like gOOse declared, your logic is completely wrong, if you enter six spaces or six times milk for example it work.I suggest you to use a StringBuilder to join the strings:
    //StringBuilder is moree efficiently in respect to a statment like String ingredients = a1+a2+a3+a4+a5;
    //so i suggest you to use this 
    StringBuilder ingredients = new StringBuilder();
    			ingredients.append(a1);
    			ingredients.append(a2);
    			ingredients.append(a3);
    			ingredients.append(a4);
    			ingredients.append(a5);
    
    


    then you call a method, for example for caffe vanilla, like this ( you write this method in your StarBucks class):

            
    	public static boolean isCaffeVanilla(String ingredients){
    		if(!ingredients.contains("milk")) return false;
    		if(!ingredients.contains("coffee"))return false;
    		if(!ingredients.contains("ice")) return false;
    		if(!ingredients.contains("vanilla bean power"))return false;
    		if(!ingredients.contains("whipped cream")) return false;
    		return true;
    	}
    
    


    finally, you call this method in the main code:

    if (isCaffeVanilla(ingredients.toString())){
    //do what you want
    }
    else{
    //
    }
    
    


    then you write other similary methods like isCaffeVanilla, that works for others frappuccino types

My Information

Member Title:
New D.I.C Head
Age:
Age Unknown
Birthday:
Birthday Unknown
Gender:
Location:
Taranto
Interests:
programming,guitar,boxing
Years Programming:
1
Programming Languages:
Knowledges of java, android, html, css, now i want to study everything that can make me a good programmer (javascript, networking,actionscript)

Contact Information

E-mail:
Click here to e-mail me

Friends

hax95 hasn't added any friends yet.

Comments

hax95 has no profile comments yet. Why not say hello?