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

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

Previous Fields

OS Preference:
Favorite Browser:
Favorite Processor:
Who Cares
Favorite Gaming Platform:
Who Cares
Your Car:
Who Cares
Dream Kudos:
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
    		//The same for thread
    		Thread t1 = new Thread(this);
    		//finally you start the Thread
  2. In Topic: MouseListener only triggering on top-level JPanel

    Posted 7 Aug 2013

    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

    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() {
    	private static void createGUI() {
    		JFrame f = new JFrame();
    		f.setPreferredSize(new Dimension(640,480));
    		Container c = f.getContentPane();
    		GamePanel gp = new GamePanel();
    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);
    	public void paintComponent(Graphics g) {
    		Graphics2D g2d = (Graphics2D)g;
    	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
    		//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
    		//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);
    		//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

My Information

Member Title:
New D.I.C Head
Age Unknown
Birthday Unknown
Years Programming:
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

Click here to e-mail me


hax95 hasn't added any friends yet.


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