Results 1 to 4 of 4
  1. #1
    ilovevcoffe is offline Member
    Join Date
    Feb 2011
    Posts
    2
    Rep Power
    0

    Default repaint() does not recall an override method paintComponent(), why is that?

    My problem is that repaint() doesn't recall a method paintComponent() and I don't understand why is that, because I thought it should happen automatically.
    The code is an implementation of the game paper soccer and the idea is that it highlights a vector if the cursor appears when the movement is possible and disappears when it's not, but the board still have to repaint the movements, that have been already made. The highlight method works, but method paintComponent which should paint a path of made movements isn't called by repaint(). If you could check this out and tell me what's wrong.

    Link to my game:
    Download pilkarzyki.rar from Sendspace.com - send big files the easy way

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,963
    Rep Power
    8

    Default

    It should be called by repaint().

    But if you want help with code, you're going to have to post an SSCCE. The code should be as small as possible while still being runnable and exhibiting the problem- please don't try posting all of your code.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    ilovevcoffe is offline Member
    Join Date
    Feb 2011
    Posts
    2
    Rep Power
    0

    Default the code representing the problem

    Ok, I've made code, that just represents the problem.


    package paper_soccer_;

    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Point;
    import java.awt.Rectangle;
    import org.jdesktop.application.Action;
    import org.jdesktop.application.ResourceMap;
    import org.jdesktop.application.SingleFrameApplication;
    import org.jdesktop.application.FrameView;
    import org.jdesktop.application.TaskMonitor;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.ArrayList;
    import javax.swing.Timer;
    import javax.swing.Icon;
    import javax.swing.JDialog;
    import javax.swing.JFrame;
    import javax.swing.JLabel;

    /**
    * The application's main frame.
    */
    public class Paper_soccer_View extends FrameView {

    private static class Board extends JLabel
    {
    public Board() {
    }

    @Override
    public void paintComponent(Graphics g)
    {
    super.paintComponent(g);
    g.setColor(Color.red);
    g.drawOval(38, 38, 4, 4);
    if (toRepaint.size()>0){
    for (int i = 0; i<toRepaint.size(); i++){
    g.setColor(Color.BLACK);
    g.drawLine((int)toRepaint.get(i).getX(), (int)toRepaint.get(i).getY(), (int)toRepaint.get(i+1).getX(), (int)toRepaint.get(i+1).getY());
    }
    }

    }

    }

    static ArrayList<Rectangle> available;
    static ArrayList<Point> toRepaint;
    Board board;

    public Paper_soccer_View(SingleFrameApplication app) {
    super(app);

    initComponents();
    available = new ArrayList<Rectangle>();
    for (int i=0; i<=80; i+=40)
    {
    for (int j = 0; j<=80; j+=40)
    {
    Rectangle r = new Rectangle(i, j, 10, 10);
    available.add(r);
    }
    }
    toRepaint = new ArrayList<Point>();
    board = new Board();
    this.jLabel1.add(board);

    // status bar initialization - message timeout, idle icon and busy animation, etc
    ResourceMap resourceMap = getResourceMap();
    int messageTimeout = resourceMap.getInteger("StatusBar.messageTimeout") ;
    messageTimer = new Timer(messageTimeout, new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    statusMessageLabel.setText("");
    }
    });
    messageTimer.setRepeats(false);
    int busyAnimationRate = resourceMap.getInteger("StatusBar.busyAnimationRat e");
    for (int i = 0; i < busyIcons.length; i++) {
    busyIcons[i] = resourceMap.getIcon("StatusBar.busyIcons[" + i + "]");
    }
    busyIconTimer = new Timer(busyAnimationRate, new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    busyIconIndex = (busyIconIndex + 1) % busyIcons.length;
    statusAnimationLabel.setIcon(busyIcons[busyIconIndex]);
    }
    });
    idleIcon = resourceMap.getIcon("StatusBar.idleIcon");
    statusAnimationLabel.setIcon(idleIcon);
    progressBar.setVisible(false);

    // connecting action tasks to status bar via TaskMonitor
    TaskMonitor taskMonitor = new TaskMonitor(getApplication().getContext());
    taskMonitor.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
    public void propertyChange(java.beans.PropertyChangeEvent evt) {
    String propertyName = evt.getPropertyName();
    if ("started".equals(propertyName)) {
    if (!busyIconTimer.isRunning()) {
    statusAnimationLabel.setIcon(busyIcons[0]);
    busyIconIndex = 0;
    busyIconTimer.start();
    }
    progressBar.setVisible(true);
    progressBar.setIndeterminate(true);
    } else if ("done".equals(propertyName)) {
    busyIconTimer.stop();
    statusAnimationLabel.setIcon(idleIcon);
    progressBar.setVisible(false);
    progressBar.setValue(0);
    } else if ("message".equals(propertyName)) {
    String text = (String)(evt.getNewValue());
    statusMessageLabel.setText((text == null) ? "" : text);
    messageTimer.restart();
    } else if ("progress".equals(propertyName)) {
    int value = (Integer)(evt.getNewValue());
    progressBar.setVisible(true);
    progressBar.setIndeterminate(false);
    progressBar.setValue(value);
    }
    }
    });
    }

    @Action
    public void showAboutBox() {
    if (aboutBox == null) {
    JFrame mainFrame = Paper_soccer_App.getApplication().getMainFrame();
    aboutBox = new Paper_soccer_AboutBox(mainFrame);
    aboutBox.setLocationRelativeTo(mainFrame);
    }
    Paper_soccer_App.getApplication().show(aboutBox);
    }

    /** This method is called from within the constructor to
    * initialize the form.
    * WARNING: Do NOT modify this code. The content of this method is
    * always regenerated by the Form Editor.
    */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

    mainPanel = new javax.swing.JPanel();
    jLabel1 = new javax.swing.JLabel();
    menuBar = new javax.swing.JMenuBar();
    javax.swing.JMenu fileMenu = new javax.swing.JMenu();
    javax.swing.JMenuItem exitMenuItem = new javax.swing.JMenuItem();
    javax.swing.JMenu helpMenu = new javax.swing.JMenu();
    javax.swing.JMenuItem aboutMenuItem = new javax.swing.JMenuItem();
    statusPanel = new javax.swing.JPanel();
    javax.swing.JSeparator statusPanelSeparator = new javax.swing.JSeparator();
    statusMessageLabel = new javax.swing.JLabel();
    statusAnimationLabel = new javax.swing.JLabel();
    progressBar = new javax.swing.JProgressBar();

    mainPanel.setName("mainPanel"); // NOI18N

    org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(p aper_soccer_.Paper_soccer_App.class).getContext(). getResourceMap(Paper_soccer_View.class);
    jLabel1.setText(resourceMap.getString("jLabel1.tex t")); // NOI18N
    jLabel1.setName("jLabel1"); // NOI18N
    jLabel1.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
    public void mouseMoved(java.awt.event.MouseEvent evt) {
    jLabel1MouseMoved(evt);
    }
    });

    javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
    mainPanel.setLayout(mainPanelLayout);
    mainPanelLayout.setHorizontalGroup(
    mainPanelLayout.createParallelGroup(javax.swing.Gr oupLayout.Alignment.LEADING)
    .addGap(0, 400, Short.MAX_VALUE)
    .addGroup(mainPanelLayout.createParallelGroup(java x.swing.GroupLayout.Alignment.LEADING)
    .addGroup(mainPanelLayout.createSequentialGroup()
    .addContainerGap()
    .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
    .addContainerGap()))
    );
    mainPanelLayout.setVerticalGroup(
    mainPanelLayout.createParallelGroup(javax.swing.Gr oupLayout.Alignment.LEADING)
    .addGap(0, 312, Short.MAX_VALUE)
    .addGroup(mainPanelLayout.createParallelGroup(java x.swing.GroupLayout.Alignment.LEADING)
    .addGroup(mainPanelLayout.createSequentialGroup()
    .addContainerGap()
    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 274, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addContainerGap(27, Short.MAX_VALUE)))
    );

    menuBar.setName("menuBar"); // NOI18N

    fileMenu.setText(resourceMap.getString("fileMenu.t ext")); // NOI18N
    fileMenu.setName("fileMenu"); // NOI18N

    javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(p aper_soccer_.Paper_soccer_App.class).getContext(). getActionMap(Paper_soccer_View.class, this);
    exitMenuItem.setAction(actionMap.get("quit")); // NOI18N
    exitMenuItem.setName("exitMenuItem"); // NOI18N
    fileMenu.add(exitMenuItem);

    menuBar.add(fileMenu);

    helpMenu.setText(resourceMap.getString("helpMenu.t ext")); // NOI18N
    helpMenu.setName("helpMenu"); // NOI18N

    aboutMenuItem.setAction(actionMap.get("showAboutBo x")); // NOI18N
    aboutMenuItem.setName("aboutMenuItem"); // NOI18N
    helpMenu.add(aboutMenuItem);

    menuBar.add(helpMenu);

    statusPanel.setName("statusPanel"); // NOI18N

    statusPanelSeparator.setName("statusPanelSeparator "); // NOI18N

    statusMessageLabel.setName("statusMessageLabel"); // NOI18N

    statusAnimationLabel.setHorizontalAlignment(javax. swing.SwingConstants.LEFT);
    statusAnimationLabel.setName("statusAnimationLabel "); // NOI18N

    progressBar.setName("progressBar"); // NOI18N

    javax.swing.GroupLayout statusPanelLayout = new javax.swing.GroupLayout(statusPanel);
    statusPanel.setLayout(statusPanelLayout);
    statusPanelLayout.setHorizontalGroup(
    statusPanelLayout.createParallelGroup(javax.swing. GroupLayout.Alignment.LEADING)
    .addComponent(statusPanelSeparator, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
    .addGroup(statusPanelLayout.createSequentialGroup( )
    .addContainerGap()
    .addComponent(statusMessageLabel)
    .addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED, 230, Short.MAX_VALUE)
    .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED)
    .addComponent(statusAnimationLabel)
    .addContainerGap())
    );
    statusPanelLayout.setVerticalGroup(
    statusPanelLayout.createParallelGroup(javax.swing. GroupLayout.Alignment.LEADING)
    .addGroup(statusPanelLayout.createSequentialGroup( )
    .addComponent(statusPanelSeparator, javax.swing.GroupLayout.PREFERRED_SIZE, 2, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    .addGroup(statusPanelLayout.createParallelGroup(ja vax.swing.GroupLayout.Alignment.BASELINE)
    .addComponent(statusMessageLabel)
    .addComponent(statusAnimationLabel)
    .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
    .addGap(3, 3, 3))
    );

    setComponent(mainPanel);
    setMenuBar(menuBar);
    setStatusBar(statusPanel);
    }// </editor-fold>

    private boolean isAvilable(Point p)
    {
    boolean isavb = false;
    for (int i=0; i<Paper_soccer_View.available.size();i++)
    {
    if(Paper_soccer_View.available.get(i).contains(p))
    {
    isavb = true;
    }
    }
    return isavb;
    }

    private void jLabel1MouseMoved(java.awt.event.MouseEvent evt) {
    // TODO add your handling code here:
    Graphics g = this.jLabel1.getGraphics();
    g.fillOval(38, 38, 4, 4);

    Point p = new Point((int)evt.getX(), (int)evt.getY());
    if (this.isAvilable(p))
    {
    g.setColor(Color.GREEN);
    g.drawLine(40, 40, (int)p.getX(), (int)p.getY());
    }
    else
    {
    this.jLabel1.repaint();
    }
    }

    // Variables declaration - do not modify
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JProgressBar progressBar;
    private javax.swing.JLabel statusAnimationLabel;
    private javax.swing.JLabel statusMessageLabel;
    private javax.swing.JPanel statusPanel;
    // End of variables declaration

    private final Timer messageTimer;
    private final Timer busyIconTimer;
    private final Icon idleIcon;
    private final Icon[] busyIcons = new Icon[15];
    private int busyIconIndex = 0;

    private JDialog aboutBox;
    }

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

    Default

    You were given a link to the SSCCE page, and Kevin summed it up for you. Do you really expect strangers on an international forum to weed out all the irrelevant stuff in the code marathon? (Hint: don't hold your breath)

    db

Similar Threads

  1. Override class method
    By Mekie in forum New To Java
    Replies: 8
    Last Post: 11-01-2010, 06:26 AM
  2. paintComponent() Method straitjacket
    By oldalistair in forum New To Java
    Replies: 5
    Last Post: 09-11-2010, 12:06 AM
  3. Replies: 2
    Last Post: 03-26-2010, 05:12 PM
  4. Replies: 10
    Last Post: 02-28-2010, 01:52 PM
  5. what made paintComponent() method to be called twice??
    By Y. Progammer in forum New To Java
    Replies: 5
    Last Post: 02-21-2010, 10:19 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •