Results 1 to 7 of 7
- 11-13-2010, 02:29 AM #1
Member
- Join Date
- Nov 2010
- Posts
- 15
- Rep Power
- 0
JPanel and JLabels Not Repainting
I am helping someone write a very simple program. However, I am running into a repainting issue. Below is my code.
And then the Tiles class.Java Code:import java.awt.*; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.*; import javax.swing.*; public class CheckerBoard extends JFrame implements MouseListener, ActionListener { //Rather than create just JLabels, it is better to create an array of //objects. The Tiles class is posted under neither this main class. Tiles[][] boardPieces = new Tiles[8][8]; JRadioButton[] buttons = new JRadioButton[3]; String[] colors = {"Red","Blue","Green"}; Color theColor = Color.RED; JPanel panel1,panel2; public CheckerBoard(){ super("CheckerBoard 1.0"); //Create the JPanel panel1 = new JPanel(new GridLayout(boardPieces.length,boardPieces.length)); panel2 = new JPanel(new GridLayout(buttons.length,buttons.length)); //Start making all of the pieces. It is better to use the .length value that way //That way you can create as many as you like. for(int i = 0; i<boardPieces.length; i++){ for(int k = 0; k<boardPieces.length; k++){ //Creating each piece. This will become more apparent when you look //into the Tiles class. boardPieces[i][k] = new Tiles(i,k,0); //Adding the MouseEvent boardPieces[i][k].addMouseListener(this); boardPieces[i][k].setColor(theColor); //Adding it to the JPanel panel1.add(boardPieces[i][k]); } } for(int j = 0; j<buttons.length; j++){ buttons[j] = new JRadioButton(colors[j]); buttons[j].setBounds(510,510,1,1); buttons[j].addActionListener(this); panel2.add(buttons[j]); } buttons[0].setSelected(true); //Adding the Panel to the Frame add(panel1,BorderLayout.CENTER); add(panel2,BorderLayout.EAST); } public void actionPerformed(ActionEvent e){ JRadioButton clicked = (JRadioButton)e.getSource(); if(clicked == buttons[0]){ buttons[1].setSelected(false); buttons[2].setSelected(false); theColor = Color.RED; } if(clicked == buttons[1]){ buttons[0].setSelected(false); buttons[2].setSelected(false); theColor = Color.BLUE; } if(clicked == buttons[2]){ buttons[0].setSelected(false); buttons[1].setSelected(false); theColor = Color.GREEN; } panel1.repaint(); } public void mouseClicked(MouseEvent e){ //Getting the source of Tiles and creating an object: clicked Tiles clicked = (Tiles)e.getSource(); //If you clicked something that is displayed, then paint as necessary. if(clicked.isDisplayable()){ //Call the function that will paint the JLabel clicked.setColor(theColor); clicked.paintComponents(clicked.getGraphics()); } } public void mouseEntered(MouseEvent e) { } public void mousePressed(MouseEvent e) { } public void mouseReleased(MouseEvent e) { } public void mouseExited(MouseEvent e) { } public static void main(String[] args) { CheckerBoard cb = new CheckerBoard(); cb.setSize(530, 530); cb.setVisible(true); cb.setResizable(false); } }
The problem I am having is when I minimize the program, the JLabels that have changed colors are no longer painted. I know I need a repaint() or validate somewhere, however I do not know when the GUI calls the repainting nor do I know where to put the repaint exactly.Java Code:import java.awt.*; import javax.swing.*; import java.awt.Graphics; //As noted, it is better to create an array of objects. You have much //more functionality in here. public class Tiles extends JLabel { int x,y; int width = 50; int height = 50; int value; Color theColor; Graphics h; //These are all the initial values that you pass in the for-loop public Tiles(int X, int Y, int v){ //Establish the coordinates and the bounds. this.x = X; this.y = Y; //Set all the pieces originally to 0. That way //we know they are all empty. this.value = v; this.setBounds(this.x*50,this.y*50,width,height); this.setOpaque(true); //Draw the initial shape. The original value will //simply return the color specified. if(this.x % 2 == 0){ this.setBackground(Color.black); } else { this.setBackground(Color.white); } } public void setColor(Color a){ this.theColor = a; } //This is the function that will draw the graphics. public void paintComponents(Graphics g){ super.paintComponents(g); //First determine the value of the piece clicked. if(this.value == 0){ //If empty, change the value to being occupied //by red. Change the oval color to red. this.value = 1; g.setColor(theColor); } else { //If it is not empty, then change it //back to it's original color. this.value = 0; if(this.x % 2 == 0){ g.setColor(Color.black); } else{ g.setColor(Color.WHITE); } } //Fill the oval information. g.fillOval(x, y, width, height); h = g; } }Last edited by phosphide; 11-13-2010 at 02:41 AM. Reason: Fixing Code
- 11-13-2010, 02:33 AM #2
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 4
For one: You don't paint with paintComponents. Rather you use the paintComponent method, and the super call is also super.paintComponent.
- 11-13-2010, 02:38 AM #3
Member
- Join Date
- Nov 2010
- Posts
- 15
- Rep Power
- 0
- 11-13-2010, 02:41 AM #4
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 4
For one, you need to tell us with as much detail as necessary just what the program is supposed to be doing, and what's not working. I don't see anywhere where you've said this, and it kind of would help us to figure out what's wrong. It's always best to assume that we can't read your mind whenever you post a question.
- 11-13-2010, 02:48 AM #5
Member
- Join Date
- Nov 2010
- Posts
- 15
- Rep Power
- 0
No problem. The program is suppose to work like this. Your given an 8x8 set of JLabels that you are allowed to click on. If you click an empty JLabel, then paint a circle on the JLabel with the specified color that you have selected. (Via one of the radio buttons.) If you click a JLabel that is occupied, then change the JLabel back to it's original state. That is all it is. The problem arises, as already specified in the OP, that the JLabels that are occupied are not being repainted when the program is minimized and then restored.
- 11-13-2010, 02:59 AM #6
Member
- Join Date
- Nov 2010
- Posts
- 15
- Rep Power
- 0
Nevermind. I figured it out. Thanks anyways.
- 11-13-2010, 03:17 AM #7
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 4
Cool, so did I. What was your solution?
What the heck, might as well post it...
Java Code:import java.awt.*; import java.awt.event.*; import javax.swing.*; public class CheckerBoard2 extends JFrame { // Link String with Color enum TileColor { RED("Red", Color.red), ORANGE("Orange", Color.orange), YELLOW("Yellow", Color.yellow), GREEN("Green", Color.green), BLUE("Blue", Color.blue); private String name; private Color color; private TileColor(String name, Color color) { this.name = name; this.color = color; } public String getName() { return name; } public Color getColor() { return color; } } private static final int COLS = 8; private static final int ROWS = COLS; private ButtonGroup radioGroup = new ButtonGroup(); public CheckerBoard2() { JPanel checkerPanel = new JPanel(new GridLayout(ROWS, COLS)); MyMouseAdapter mouseAdapter = new MyMouseAdapter(); for (int i = 0; i < ROWS; i++) { for (int j = 0; j < COLS; j++) { Color c = ((i + j) % 2 == 0) ? Color.black : Color.white; MyTile tile = new MyTile(c); tile.addMouseListener(mouseAdapter); checkerPanel.add(tile); } } JPanel radioPanel = new JPanel(new GridLayout(1, 0)); for (TileColor tileColor : TileColor.values()) { JRadioButton rBtn = new JRadioButton(tileColor.getName()); rBtn.setActionCommand(tileColor.getName()); radioGroup.add(rBtn); radioPanel.add(rBtn); } getContentPane().add(checkerPanel, BorderLayout.CENTER); getContentPane().add(radioPanel, BorderLayout.SOUTH); } private class MyMouseAdapter extends MouseAdapter { @Override public void mousePressed(MouseEvent e) { ButtonModel selectedModel = radioGroup.getSelection(); if (selectedModel == null) { return; } String actionCommand = selectedModel.getActionCommand(); if (actionCommand == null) { return; } for (TileColor tileColor : TileColor.values()) { if (actionCommand.equals(tileColor.getName())) { MyTile tile = (MyTile) e.getSource(); Color color = (tile.getColor() == null) ? tileColor.getColor() : null; tile.setColor(color); tile.repaint(); return; } } } } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { JFrame frame = new CheckerBoard2(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } }); } } class MyTile extends JLabel { private static final int SIDE = 64; private static final Dimension TILE_SIZE = new Dimension(SIDE, SIDE); private Color color = null; public MyTile(Color background) { setOpaque(true); setPreferredSize(TILE_SIZE); setBackground(background); } public Color getColor() { return color; } public void setColor(Color color) { this.color = color; } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); if (color != null) { Graphics2D g2 = (Graphics2D) g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g.setColor(color); int width = getWidth(); int height = getHeight(); g.fillOval(0, 0, width, height); } } }Last edited by curmudgeon; 11-13-2010 at 05:20 AM.
Similar Threads
-
repainting more efficiently
By imorio in forum AWT / SwingReplies: 2Last Post: 08-24-2010, 04:24 AM -
new JLabels not showing in JPanel (doing revalidate&repaint)
By r00tb33r in forum AWT / SwingReplies: 6Last Post: 06-16-2010, 06:03 AM -
repainting a jframe containing two jpanels
By musasabi in forum New To JavaReplies: 0Last Post: 05-12-2010, 04:36 AM -
Repainting From Another Class
By habester in forum New To JavaReplies: 1Last Post: 11-13-2009, 02:29 AM -
repainting lines separately
By rstepler in forum Java 2DReplies: 8Last Post: 07-07-2008, 02:46 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks