Results 1 to 4 of 4
- 02-03-2011, 01:37 PM #1
Member
- Join Date
- Feb 2011
- Posts
- 2
- Rep Power
- 0
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
- 02-03-2011, 01:40 PM #2
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!
- 02-03-2011, 08:59 PM #3
Member
- Join Date
- Feb 2011
- Posts
- 2
- Rep Power
- 0
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;
}
- 02-04-2011, 06:30 AM #4
Similar Threads
-
Override class method
By Mekie in forum New To JavaReplies: 8Last Post: 11-01-2010, 06:26 AM -
paintComponent() Method straitjacket
By oldalistair in forum New To JavaReplies: 5Last Post: 09-11-2010, 12:06 AM -
method not abstract, does not override actionperformed method.
By Theman in forum New To JavaReplies: 2Last Post: 03-26-2010, 05:12 PM -
JOptionPane when called from inside the paintComponent() method
By Y. Progammer in forum New To JavaReplies: 10Last Post: 02-28-2010, 01:52 PM -
what made paintComponent() method to be called twice??
By Y. Progammer in forum New To JavaReplies: 5Last Post: 02-21-2010, 10:19 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks