Jump to content
  • Advertisement
Sign in to follow this  
Gallivan

[java] g.drawLine

This topic is 3609 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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) {}
}

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.*;

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!