Problem with painting a JPanel
Hi,
I have crated two panales with override "paint" methods. One panel draws circles in a place where it is pressed with mouse button, second one draws squares. Everything works almost fine, when clicking just at one panel. If I create for example 5 circles on the first panel and then click on the second one, all circles are drawn and additional square.
What should I make to separate graphics on both panels? That clicking on, let's say left one, will create only circles, and clicking on right only squares.
Thanks in advance
Re: Problem with painting a JPanel
When you create a panel, you should tell the panel what kind of shape to draw when it is clicked on. For example: Pass a shape_to_draw indicating value in the constructor.
2 Attachment(s)
Re: Problem with painting a JPanel
Yes, I did so already and it works fine. The problem is that painting one panel, causes that also second panel in painted on it. So the whole contents of the second panel is drawn on the first panel. And I can't find the reason why it is so.
Here is what happens when I click left panel ( "green" circle ): Attachment 3519
and here after clicking right panel ( "blue" square ): Attachment 3520
Re: Problem with painting a JPanel
You will need to post code that shows the problem. Make a small program that compiles, executes and shows the problem and post it here. Be sure to wrap the code in code tags.
Re: Problem with painting a JPanel
Here is my JPanel class thet objects are created in JFrame.
Code:
/////// myPanel.java ////////
public class MyPanel extends javax.swing.JPanel{
/**
* Creates new form MyPanel
*/
private Database array;
private String figures;
private Random rand;
public MyPanel(){
}
public MyPanel(Figures fig) {
initComponents();
array = new Database();
figures = fig.toString();
rand = new Random();
}
//@Override
public void paint(Graphics g){
array.drawAll(g);
printBorder(g);
}
/**
* 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() {
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 342, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 256, Short.MAX_VALUE)
);
}// </editor-fold>
// Variables declaration - do not modify
// End of variables declaration
public void addFigure(MouseEvent e) {
Color color = new Color(rand.nextInt());
switch(figures){
case "Circle":
{
array.add(new Circle(e.getX(),e.getY(),color));
break;
}
case "Square":
{
array.add(new Square(e.getX(),e.getY(),color));
break;
}
}
}
}
///// myFrame.java //////
circlesPanel = new GUI.MyPanel(Figures.Circle);
squaresPanel = new GUI.MyPanel(Figures.Square);
private void squaresPanelMousePressed(java.awt.event.MouseEvent evt) {
squaresPanel.addFigure(evt);
squaresPanel.repaint();
}
private void circlesPanelMousePressed(java.awt.event.MouseEvent evt) {
circlesPanel.addFigure(evt);
circlesPanel.repaint();
}
If you need, I'll paste more codes.
Re: Problem with painting a JPanel
The code you posted will not execute to show the problem. It does not have a main() method.
Re: Problem with painting a JPanel
Well, I hoped it would be enought because there is a lot of code generated by NetBeans IDE. Here is the whole code.
Code:
//////// Figure.java
package Figures;
import java.awt.Color;
import java.awt.Graphics;
public abstract class Figure {
int x, y;
Color color;
public Figure(int x, int y, Color color){
this.x = x;
this.y = y;
this.color = color;
}
public abstract void draw(Graphics g);
}
////// Square.java ////////
package Figures;
import java.awt.Color;
import java.awt.Graphics;
public class Square extends Figure{
public Square( int x, int y, Color color){
super(x,y,color);
}
@Override
public void draw(Graphics g) {
g.setColor(color);
g.fillRect(x-10, y-10, 20, 20);
}
}
/////// Circle.java ////////
package Figures;
import java.awt.Color;
import java.awt.Graphics;
public class Circle extends Figure{
public Circle( int x, int y, Color color){
super(x,y,color);
}
@Override
public void draw(Graphics g) {
g.setColor(color);
g.fillOval(x-10, y-10, 20, 20);
}
}
/////// Database.java ///////
package Figures;
import java.awt.Graphics;
import java.util.ArrayList;
public class Database extends ArrayList<Figure>{
public void drawAll(Graphics g){
for( Figure fig : this ){
fig.draw(g);
}
}
}
////// MyFrame.java ///////
package GUI;
import java.awt.Color;
import java.awt.Graphics;
import Figures.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Random;
public class MyPanel extends javax.swing.JPanel{
private Database array;
private String figures;
private Random rand;
public MyPanel(){
}
public MyPanel(Figures fig) {
initComponents();
array = new Database();
figures = fig.toString();
rand = new Random();
}
//@Override
public void paint(Graphics g){
array.drawAll(g);
printBorder(g);
}
/**
* 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() {
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 342, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 256, Short.MAX_VALUE)
);
}// </editor-fold>
// Variables declaration - do not modify
// End of variables declaration
public void addFigure(MouseEvent e) {
Color color = new Color(rand.nextInt());
switch(figures){
case "Circle":
{
array.add(new Circle(e.getX(),e.getY(),color));
break;
}
case "Square":
{
array.add(new Square(e.getX(),e.getY(),color));
break;
}
}
}
}
///// MyFrame.java /////////
package GUI;
public class MainFrame extends javax.swing.JFrame {
public MainFrame() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
trianglesPanel = new GUI.MyPanel(Figures.Triangle);
circlesPanel = new GUI.MyPanel(Figures.Circle);
squaresPanel = new GUI.MyPanel(Figures.Square);
trianglesPanel.setBorder(new javax.swing.border.MatteBorder(null));
trianglesPanel.addMouseListener(new java.awt.event.MouseAdapter() {
public void mousePressed(java.awt.event.MouseEvent evt) {
trianglesPanelMousePressed(evt);
}
});
javax.swing.GroupLayout trianglesPanelLayout = new javax.swing.GroupLayout(trianglesPanel);
trianglesPanel.setLayout(trianglesPanelLayout);
trianglesPanelLayout.setHorizontalGroup(
trianglesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 122, Short.MAX_VALUE)
);
trianglesPanelLayout.setVerticalGroup(
trianglesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 0, Short.MAX_VALUE)
);
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Figures drawing");
circlesPanel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
circlesPanel.addMouseListener(new java.awt.event.MouseAdapter() {
public void mousePressed(java.awt.event.MouseEvent evt) {
circlesPanelMousePressed(evt);
}
});
javax.swing.GroupLayout circlesPanelLayout = new javax.swing.GroupLayout(circlesPanel);
circlesPanel.setLayout(circlesPanelLayout);
circlesPanelLayout.setHorizontalGroup(
circlesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 134, Short.MAX_VALUE)
);
circlesPanelLayout.setVerticalGroup(
circlesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 126, Short.MAX_VALUE)
);
squaresPanel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
squaresPanel.addMouseListener(new java.awt.event.MouseAdapter() {
public void mousePressed(java.awt.event.MouseEvent evt) {
squaresPanelMousePressed(evt);
}
});
javax.swing.GroupLayout squaresPanelLayout = new javax.swing.GroupLayout(squaresPanel);
squaresPanel.setLayout(squaresPanelLayout);
squaresPanelLayout.setHorizontalGroup(
squaresPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 186, Short.MAX_VALUE)
);
squaresPanelLayout.setVerticalGroup(
squaresPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 177, Short.MAX_VALUE)
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(circlesPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 122, Short.MAX_VALUE)
.addComponent(squaresPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(87, 87, 87))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(1, 1, 1)
.addComponent(circlesPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(53, 53, 53)
.addComponent(squaresPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(61, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void trianglesPanelMousePressed(java.awt.event.MouseEvent evt) {
}
private void squaresPanelMousePressed(java.awt.event.MouseEvent evt) {
squaresPanel.addFigure(evt);
squaresPanel.repaint();
}
private void circlesPanelMousePressed(java.awt.event.MouseEvent evt) {
circlesPanel.addFigure(evt);
circlesPanel.repaint();
}
// Variables declaration - do not modify
private GUI.MyPanel circlesPanel;
private GUI.MyPanel squaresPanel;
private GUI.MyPanel trianglesPanel;
// End of variables declaration
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new MainFrame().setVisible(true);
}
});
}
}
enum Figures{
Circle, Square, Triangle;
}
Re: Problem with painting a JPanel
What is supposed to happen when the code is executed?
I get a window with two boxes drawn, smaller in the upper left.
When I click in the left box, a colored circle is drawn in that box.
When I click on the right box, a colored square is drawn in that box.
Re: Problem with painting a JPanel
And no circles are in the right box? Cuz what is happening for you is that what I expected. But for me, the left box is drawn on the right box.
Re: Problem with painting a JPanel
circles in left box, squares in right box
1 Attachment(s)
Re: Problem with painting a JPanel
And this is what I get after one click on the left box and then one on the right: Attachment 3525
As you can see, left smaller box appeared in the right box :(
PS. That is damn strange but after minimizing window, this square disappeared. I have no idea what has happened :P
Re: Problem with painting a JPanel
Try rewriting it and do not use the IDE generated code.
Re: Problem with painting a JPanel
Finally had some time to work with that and found a solution. In the methods:
Code:
private void squaresPanelMousePressed(java.awt.event.MouseEvent evt) {
squaresPanel.addFigure(evt);
squaresPanel.repaint();
}
I added simple and it works fine now.
Thanks for Your help!