Hi, I have a external class with paint(Graphics g) method and the class itself is working great. I can get it visible by launching it from my launch class by doing add(new Laatikko()); but when I am trying to get it work from third class it wont show up. Basicly I have main class and there I need to run this class which isnt showing up.

package net.viped;

import java.awt.Color;

import javax.swing.JFrame;

public class Launch extends JFrame {

    public Launch() {

        add(new grafiikkaa());
        setSize(300, 300);

        setBackground(Color.black);


        setVisible(true);

        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        new Launch();
    }

}



package net.viped;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.image.BufferStrategy;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class grafiikkaa extends JPanel implements ActionListener{

    int y=100;
    Saie saie = new Saie();

    public grafiikkaa() {
        addKeyListener(new LueNappaimet());
        initGame();
        Laatikko laa = new Laatikko();
        System.out.println(laa.isDisplayable());
        add(new Laatikko());
        System.out.println(laa.getGraphics());
        System.out.println(laa.getBounds());
        setFocusable(true);
        this.repaint();
    }

    public void drawStuff() {
        Graphics g = null;
        Laatikko laa = new Laatikko();

    }


    public void paint(Graphics g) {
        super.paint(g);

        g.setColor(Color.blue);
        g.fillRect(50, y, 10, 10);
        g.setColor(Color.red);
        g.fillRect(50, 70, 10, 10);

        Toolkit.getDefaultToolkit().sync();
        g.dispose();
    }

    public void initGame() {
        saie.start();

    }



    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub

    }

    public class LueNappaimet extends KeyAdapter{
        public void keyPressed(KeyEvent e) {
            int key = e.getKeyCode();
            if (key == KeyEvent.VK_UP) {
                System.out.println("up");
                y--;
                repaint();
            }
        }
    }

    public class Saie extends Thread {
        public void run() {
            while(true) {
                try {
                    sleep(10);
                    repaint();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }

}




package net.viped;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Toolkit;

import javax.swing.JLabel;
import javax.swing.JPanel;

public class Laatikko extends JLabel{
    int width = 10;
    int height = 10;
    int x = 100;
    int y = 50;


    public Laatikko() {
        setBounds(x, y, width, height);
        setVisible(true);

    }

    public void paint(Graphics g) {
        super.paint(g);
        g.setColor(Color.white);
        g.fillRect(100, 150, 10, 10);

        Toolkit.getDefaultToolkit().sync();
        g.dispose();
    }
}

Thanks for advance.

when I am trying to get it work from third class it wont show up

Can you give the name of the "third class" and explain how you are using the Laatikko class?

The posted code creates three instances of the class on lines 57, 59 and 68.
What are all those instances used for? Is there only supposed to be one instance and the multiple instances are a confusion?

Edited 4 Years Ago by NormR1

Yes it is confusion. I tried multiple ways to create instance and just forgotted edit those. All three classes are in code I posted. There is Launch.class from where I launch grafiikkaa.class and last the Laatikko.class

To have the Laatikko class be visible in the GUI you need to add an instance of it to a container that is visible in the GUI.
There should only be one instance of the class created in lines 57 & 59 and that one instance be used.
I don't know what the code on line 68 is supposed to do. It creates an instance of the object but does not use it for anything.

I tried to

Laatikko laa = new Laatikko();
add(laa);

but it didnt works so how do you recommend to do that.

Do some debugging to see what is happening. Add a println in all the paint() methods to print out the local class's bounds using getBounds() and in the grafiikkaa class also print out the bounds of the laa class.

Nothing out of ordinary there. laa.getBounds() returns: java.awt.Rectangle[x=100,y=50,width=10,height=10] and on local: java.awt.Rectangle[x=0,y=0,width=300,height=278]. laa.isDisplayable() returns false, why is that?

What was the bounds of the laa object when you printed it from inside of the paint method in the grafiikkaa class?

Actually there is something odd java.awt.Rectangle[x=150,y=5,width=0,height=0] so why is that? Shouldn't width and height be 10 and y 50?

I don't know why the JLabel should be shown when it has no content.
Try telling the layout manager the preferred size and see if that will cause its paint method to be called.
Or give it some content.

Not helping. Now getBounds() returns java.awt.Rectangle[x=-16233,y=5,width=32767,height=32767]

Not helping

I have no idea what you have done or not done in the code.
What happened when you define the JLabel with a String in the constructor?

You'll have to post the code you are having problems with.

Defining String in a constructor does not change anything. Whole code is posted on the first post so there the flaw should be.

I have suggested several changes to the code. Did you make any of them?

Defining String in a constructor does not change anything

It did for me. When I add a String to the JLabel's constructor I see the String displayed on the screen and the JLabel class's paint method is called and the println executes printing out a line.

Okay. Thanks for your help. I decided to do everything again from the beginning. I ended up to solution where Laatikko extends Rectangular and that pretty much solves everything.

This article has been dead for over six months. Start a new discussion instead.