K, well, I'm kind of screwed right now, I am supposed to make a game by using java, and this is all like minor stuff.

The code so far, and I have little idea of what I'm doing. I want to make it so that when you click in a certain point on the board, the chip will fall into that place with the mousePressed, but for some reason, when I open the GUIWindow, the piece is already on the board without any clicking.

Colorpanel

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class ColorPanel1 extends JPanel{

    int x;
    int y;

    private ImageIcon image, image2, image3;

    public ColorPanel1(Color backColor, ImageIcon i, ImageIcon z, ImageIcon k){
        setBackground(backColor);
        image = i;
        image2 = z;
        image3 = k;

    }

    public void paintComponent(Graphics g){
        super.paintComponent(g);
        image.paintIcon(this, g, 0, 0);
        if (x <= 78 && x >= 0 && y <=78 && y >= 0)
            image2.paintIcon(this, g, 0, 0);
    }
    public void mousePressed(MouseEvent e){
        x = e.getX();
        y = e.getY();
    }
}

GUIWindow

import javax.swing.*;
import java.awt.*;

public class GUIWindow{

    public static void main(String[] args){
        JFrame theGUI = new JFrame();
        theGUI.setTitle("Connect 4");
        theGUI.setSize(545, 490);
        theGUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        ImageIcon image = new ImageIcon("checkerboard.gif");
        ImageIcon image2 = new ImageIcon("red.gif");
        ImageIcon image3 = new ImageIcon("black.gif");
        ColorPanel1 panel = new ColorPanel1(Color.black, image, image2, image3);
        Container pane = theGUI.getContentPane();
        pane.add(panel);
        theGUI.setVisible(true);
    }
}

first set the other class as object of the main so you can call it in your main program

There is absolutely no reason to call it from main().

first set the other class as object of the main so you can call it in your main program

There is absolutely no reason to call it from main().

hmm, I really cant understand whats wrong with main() method ?!?!?

To aznlover,

First you have to implement MouseListener interface because you have to do work on mousePressed.

public class ColorPanel1 extends JPanel implements java.awt.event.MouseListener {...

and then, override methods of interface

@Override
public void mouseClicked(MouseEvent e) {
	// TODO Auto-generated method stub
}

@Override
public void mouseEntered(MouseEvent e) {
	// TODO Auto-generated method stub
}

@Override
public void mouseExited(MouseEvent e) {
	// TODO Auto-generated method stub
}

@Override
public void mouseReleased(MouseEvent e) {
	// TODO Auto-generated method stub
}

@Override
public void mousePressed(MouseEvent e) {
	// TODO Auto-generated method stub
	// CODE HERE FOR PAINTING ICONS
}

do NOT paint icons in paintComponent(Graphics g) method because this method is invoked automatically when you create object of class.

Also, dont forget to add MouseListener in ColorPanel1 class by adding this line in constructor (will be good enough) of ColorPanel1 class

this.addMouseListener(this);

Best of luck!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.