Jump to content

How to perform custom painting in Java?

- - - - -

  • Please log in to reply
2 replies to this topic

#1
Omar Luna

Omar Luna

    Newbie

  • Members
  • Pip
  • 3 posts
Hello!

This is my first post in this forum thanks for the replies in advice.

I'm trying to make a program that simulates an epidemic of X disease. The problem I have is with the graphics part. I have already the classes which get the number of people sick in a bi-dimensional array whit I plan to use as coordinates for the rectangles or dots in the graphic part. I have been trying to use rectangles to draw it or even been thinking to do it as a grid and colour the number of infected people.

What I need is to:
get the infected people and colour them green.
after 7 days sick they may heal and paint the already infected black

I did it like this:



package epidemia;


import java.awt.Color;

import java.awt.Component;

import java.awt.Graphics;

import javax.swing.JComponent;



class Pixel extends JComponent {

    

    int posX = 0;

    int posY = 0;

    int estado = 0;

    

    public void paint(Graphics g){ 

    

        if(estado == 0){

            g.setColor(Color.CYAN);

            g.fillRect(posX, posY, 20, 20);

            g.drawRect(posX, posY, 20, 20);

        }

        if(estado == 1){

            g.setColor(Color.GREEN);            

            g.fillRect(posX, posY, 20, 20);

            g.drawRect(posX, posY, 20, 20);

        }

        if(estado == 2){

            g.fillRect(posX, posY, 20, 20);

            g.setColor(Color.RED);

            g.drawRect(posX, posY, 20, 20);

        }

    

    }

    

    public void setposX(int X){

        posX = X;

    

    }

    

    

    public int getposX(){

        return posX;

    }

    

    public void setposY(int Y){

        posY = Y;

    }

    

    

    public int getposY(){

        return posY;

    }


    public int getEstado() {

        return estado;

    }


    public void setEstado(int estado) {

        this.estado = estado;

    }

       

}


but now I get only one painting instead of the number of infected people I've got.

I paint them like this:


for(cy = 1; cy < 101; cy++)

            for(cx = 1; cx < 101; cx++){

                try{

                poblacion[cx][cy] = pobTemp[cx][cy];

                persona[cx][cy].setposX(p_temp.X);                    

                persona[cx][cy].setposY(p_temp.Y);                

                persona[cx][cy].setEstado(pobTemp[cx][cy].estado);     

                

                if(poblacion[cx][cy].infectado){                    

                    ventanaGraficos.getContentPane().add(persona[cx][cy]);

                    //form1.Image1.Canvas.Pixels[cx,cy] = rgb(0,0,0);

                }

                

                jLabel6.setText(""+t*3);                

                jLabel7.setText(""+numInfectados);                                

                }catch(Exception e){}


I don''t know what I may be missing

Please help =)

Edited by Omar Luna, 15 October 2011 - 08:44 AM.
Code added


#2
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
This small tutorial seems similar to what you're doing:

Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)

The difference is you can ignore the mouseEvent because you're painting doesn't depend on mouse clicks.

#3
Omar Luna

Omar Luna

    Newbie

  • Members
  • Pip
  • 3 posts
Thanks =) I'll give it a try




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users