Skip to main content
GameDev.net gamedev.net
🔒 Locked

[java] g.drawLine

Started by Gallivan Nov 26, 2008 at 9:42 PM 3 replies 950+ views
Original Post
Gallivan
Gallivan
I have a class in Netbeans called Render. One method is the drawCreature:
public void drawCreature(Creature creature) {
        Graphics g = jPanel1.getGraphics();
        Graphics2D g2 = (Graphics2D) g;
        g2.setColor(Color.white);
        g2.drawLine(creature.currentX, creature.currentY,
                    creature.currentX, creature.currentY);
    }
The method that calls it is the menu item action:
private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {
        Creature mike = new Creature();
        drawCreature(mike);
My Creature class is fine, but it still won't draw a white dot to the screen where Mike should be located. Do I have any blatant errors? EDIT: Creature class, in case it helps.

public class Creature {
    Creature parent;
    int health, hunger, speed, power, color;
    int currentX, currentY;
    void Creature() {
        health = 5;
        hunger = 0;
        speed = 5;
        power = 5;
        currentX = 15;
        currentY = 15;
    }
    public void move(int coordX, int coordY) {
        while (currentX != coordX) {
            if (currentX < coordX) {
                currentX++;
            }
            if (currentX > coordX) {
                currentX--;
            }
        }
        while (currentY != coordY) {
            if (currentY < coordY) {
                currentY++;
            }
            if (currentY > coordY) {
                currentY--;
            }
        }
    }
    public void eat() {}
    public void fight(Creature target) {}
    public void mate(Creature target) {}
}
Cornstalks
Cornstalks
You're line is infinitely small since it goes from (x1, y1) to (x1, y1). Try using different x and y values. Like drawing from (x1, y1) to (x1 + 1, y1 + 1). Better yet, for a single pixel, do g.fillRect(creature.currentX, creature.currentY, 1, 1). This specifies a rectangle at (creature.currentX, creature.currentY) with a width and height of 1.
Gallivan
Gallivan
Thanks Mike. That didn't fix the problem, but I do appreciate help on correct coding practices.
Antheus
Antheus
You cannot draw on canvas like that, since first repaint event will clear it.

You need to do drawing in paintComponent(Graphics). To do that, you need to override one of JComponents.

Something like this:
public class CreaturePanel extends JPanel {  public void paintComponent(Graphics g) {    Graphics2D g2 = (Graphics2D) g;    if (creature == null) return;    g2.setColor(Color.white);    g2.drawLine(creature.currentX, creature.currentY,                creature.currentX+1, creature.currentY+1);  }  public Creature getCreature() {    return creature;  }  public void setCreature(Creature newCreature) {    creature = newCreature;    repaint();  }  private Creature creature;};...private void jMenuItem2ActionPerformed(ActionEvent evt) {  if (jCreaturePanel.getCreature() == null) {    jCreaturePanel.setCreature(new Creature());  }}


Of course, then you need to expand the whole thing so that CreaturePanel can hold more than one, so that it can update them, etc...


But you should never call paintComponent() directly. Swing does that in response to events.
leif_dragon
leif_dragon
you need to have a JPanel paint method in order to draw to the Panel.

Something like this,

public void paint(Graphics g)
{
mike.paint(g);
}

then you would put a method in your Creature class called,

public void paint(Graphics g)
{
g.drawLine(currentX, currentY, currentX, currentY);
// or
g.fillRect(currentX, currentY, 1, 1);
// for exactly 1 pixel
g.drawRect(currentX, currentY, 0, 0);
}

dont forget to import java.awt.*;

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.