Domanda

I'm fairly new to this, so my apologies if this is something super easy that I'm missing!

I'm working on an assignment where I'm supposed to make an insect that moves around the screen based on commands given by the user. The issue I'm having is when I use my JButton to use the move() method. If I print out it's location each time, I know that it's calling the move() method properly, but I can't get it to repaint each time. So, essentially, I'm clicking my move button and my bug's just sitting there. I'm hoping someone can point me in the right direction to get it to repaint.

This first class (BugTester) has my main, and is what runs the overall program:

public class BugTester {

public static void main(String[] args) {
    final JFrame frame = new JFrame();
    frame.setSize(700, 700);
    frame.setTitle("Final Project - Grant Cooper");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    final Bug testBug = new Bug(10);
    final JComponent i = new JComponent() {

        public void paintComponent(final Graphics g) {
            testBug.paintComponent(g);
            JPanel panel = new JPanel();
            JButton moveButton = new JButton("Move");
            JButton turnButton = new JButton("Turn");
            panel.add(turnButton);
            panel.add(moveButton);
            frame.add(panel);
            class ClickListenerMove implements ActionListener {

                public void actionPerformed(ActionEvent event) {
                    testBug.move();

                    System.out.println(testBug.getPosition());

                }
            }
            class ClickListenerTurn implements ActionListener {

                public void actionPerformed(ActionEvent event) {
                    testBug.turn();

                }
            }
        }

    };

    frame.add(i);
    frame.setVisible(true);

}

And the rest of the program is here: package SecondAttempt;

import java.awt.*;
import java.awt.geom.*;
public class Bug {

public int leftRight;
public int direction;

//Bug constructor
public Bug(int initialPosition) {
    leftRight = initialPosition;
    direction = 1;

}

public void turn() //Turn Bug around - currently only moves in 2 dimensions, need to add up and down
{
    switch (direction) {
        case 1:
            direction = 1;
        case 2:
            direction = -1;
    }

}

public void move() {
    getPosition();
    getDirection();
    if (direction > 0) {
        leftRight++; //move Bug right
        System.out.println(getPosition());
    }
    if (direction < 0) {
        leftRight--; //move Bug left
        System.out.println(getPosition());
    }
}

public int getPosition() {

    return leftRight;
}

public int getDirection() {

    return direction;
}

//create Bug
public void paintComponent(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;

    //create Bug body
    //(X Coord of body, Y Coord of body, left/right stretch, up/down stretch)
    Ellipse2D.Double body = new Ellipse2D.Double(300 + getPosition(), 310, 85, 25);
    g2.draw(body); //draw Bug body
    g2.setColor(Color.RED);
    g2.fill(body); //color Bug body
    g2.setColor(Color.BLACK); //reset color

    //create Bug head
    //(X coord head, y coord head, left/right stretch, up/down stretch)
    Ellipse2D.Double head = new Ellipse2D.Double(380 + getPosition(), 312, 25, 20);
    g2.draw(head); //draw Bug head
    g2.setColor(Color.GREEN);
    g2.fill(head); //color Bug head
    g2.setColor(Color.BLACK); //reset color

    //create Bug legs
    //First set of legs
    //(Top part of leg x position, top y position, bottom x position, bottom y position)
    g2.setColor(Color.YELLOW);
    Line2D.Double leg1_left = new Line2D.Double(365 + getPosition(), 295, 365 + getPosition(), 310);
    g2.draw(leg1_left);
    g2.setColor(Color.YELLOW);
    Line2D.Double leg1_right = new Line2D.Double(365 + getPosition(), 333, 365 + getPosition(), 349);
    g2.draw(leg1_right);
    g2.setColor(Color.YELLOW);
    //Second set of legs
    Line2D.Double leg2_left = new Line2D.Double(341 + getPosition(), 292, 341 + getPosition(), 310);
    g2.draw(leg2_left);
    g2.setColor(Color.YELLOW);
    Line2D.Double leg2_right = new Line2D.Double(341 + getPosition(), 336, 341 + getPosition(), 354);
    g2.draw(leg2_right);
    g2.setColor(Color.YELLOW);
    //Third set of legs
    Line2D.Double leg3_left = new Line2D.Double(320 + getPosition(), 295, 320 + getPosition(), 310);
    g2.draw(leg3_left);
    g2.setColor(Color.YELLOW);
    Line2D.Double leg3_right = new Line2D.Double(320 + getPosition(), 333, 320 + getPosition(), 349);
    g2.draw(leg3_right);
    g2.setColor(Color.YELLOW);

    //create Bug antennae
    //(left x, left y, right x, right y)
    Line2D.Double antenna1 = new Line2D.Double(403 + getPosition(), 315, 410 + getPosition(), 315);
    g2.draw(antenna1);
    g2.setColor(Color.YELLOW);
    Line2D.Double antenna2 = new Line2D.Double(403 + getPosition(), 329, 410 + getPosition(), 329);
    g2.draw(antenna2);
    g2.setColor(Color.YELLOW);

}
}

Does anyone have any insight on how I can make it repaint? I know the program itself isn't entirely functional yet, but I feel confident I can finish it once I figure this part out.

È stato utile?

Soluzione 2

I think there are two problems in your code. At first the two inner classes in your Component are not used. So if you click the buttons nothing would happen, because they are not linked to any code. I would suggest to add an ActionListener to your buttons:

                JButton moveButton = new JButton("Move");
                  moveButton.addActionListener(new ActionListener(){
                    public void actionPerformed(ActionEvent e){
                        testBug.move();

                        repaint();
                    }
                });

            JButton turnButton = new JButton("Turn");
                turnButton.addActionListener(new ActionListener(){
                    public void actionPerformed(ActionEvent e){
                        testBug.turn();
                        repaint();
                    }
                });

I already added repaint(). The bug starts moving then.

Altri suggerimenti

First, you are not overriding the function paintComponent, because you're not extending any swing of the swing interfaces, so there is no point to name the function like that. You have of course to use the appropriate Graphics handler to draw your Bug where you can get from the main Swing interface in your app.

You need to have an update timer or a loop and inside it you update the positions in a place and another place where it draws your Bug.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top