Search code examples
javajpanelpaintcomponentgraphics2d

paintComponent problems displaying


I am having a terrible time trying to display the triangles that I am trying to draw. I have looked over the web and and many of the options that I have found I implemented that haven't fixed my display issue. My button panel displays perfectly but my JPanel for drawing either isn't being used or isn't being drawn on. Any help that you guys can give would be great I've been staring for a few days and haven't had any luck. I have more code to implement but I want to get the bare-bones of my code running before I add too much. Here's my code thanks for any help ahead of time.

    import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.geom.Point2D;

import javax.swing.*;

public class RotateAndShiftTriangles extends JPanel{

int rWidth, rHeight, centerX, centerY, prevMove = -1, maxX, maxY;
float pixelSize;
double radians;
static Point2D pA, pB, pC;

//Default constructor
RotateAndShiftTriangles(){
    rWidth = Integer.parseInt(JOptionPane.showInputDialog("Enter the rWidth of the panel: "));
    rHeight = Integer.parseInt(JOptionPane.showInputDialog("Enter the rHeight of the panel: "));
}

// main method of the program that all it really does is call the create method
public static void main(String [] argv){
    RotateAndShiftTriangles tri = new RotateAndShiftTriangles();
    tri.create();
}

public void create(){
    // Initializing graphics 
    JFrame win = new JFrame();
    Dimension d = new Dimension(800, 600);
    maxX = d.width - 1;
    maxY = d.height - 1;
    pixelSize = Math.max(rWidth/maxX, rHeight/maxY);
    centerX = maxX/2; centerY = maxY/2;
    win.setSize(d);
    win.setMinimumSize(d);
    win.setPreferredSize(d);
    win.getContentPane().setLayout(new BorderLayout());
    Point2D centerPoint = new Point2D.Double(centerX, centerY);
    JPanel draw = new JPanel();
    JPanel buttonPanel = new JPanel();
    JButton shiftButton = new JButton("Shift");
    JButton rotateButton = new JButton("Rotate");
    JButton shiftandRotateButton = new JButton("Shift and Rotate");
    JButton resetButton = new JButton("Reset");
    JButton backButton = new JButton("Back");

    // setting layout
    win.add(this);
    win.add(draw, BorderLayout.CENTER);
    buttonPanel.add(shiftButton);
    buttonPanel.add(rotateButton);
    buttonPanel.add(shiftandRotateButton);
    buttonPanel.add(resetButton);
    buttonPanel.add(backButton);
    win.add(buttonPanel, BorderLayout.SOUTH);
    win.pack();
    win.setLocationRelativeTo(win.getParent());

    // makes window visible and sets the default closing operations
    win.setVisible(true);
    win.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}

// Page 19 mappings to take the logical coords and change them to ints.
int iX(float x){
    return Math.round(centerX + x/pixelSize);
    }
int iY(float y){
    return Math.round(centerY - y/pixelSize);
    }

// Standard paintComponent method 
protected void paintComponent(Graphics g){
    super.paintComponent(g);
    g.setColor(Color.blue);
    g.drawRect(0, 0, maxX, maxY);
    float side = 0.95F * pixelSize, sideHalf = 0.5F * side, 
        h = sideHalf * (float)Math.sqrt(3), xA, yA, xB, yB, xC, yC, xA1, yA1, xB1, yB1, xC1, yC1, p, q;
    q = 0.05F; 
    p = 1 - q; 
    xA = centerX - sideHalf; 
    yA = centerY - 0.5F * h;
    xB = centerX + sideHalf; 
    yB = yA;
    xC = centerX; 
    yC = centerY + 0.5F * h;
    pA.setLocation(xA, yA);
    pB.setLocation(xB, yB);
    pC.setLocation(xC, yC);

    for (int i=0; i<50; i++) 
    {  
        g.drawLine(iX(xA), iY(yA), iX(xB), iY(yB));
        g.drawLine(iX(xB), iY(yB), iX(xC), iY(yC));
        g.drawLine(iX(xC), iY(yC), iX(xA), iY(yA));
        xA1 = p * xA + q * xB; 
        yA1 = p * yA + q * yB; 
        xB1 = p * xB + q * xC; 
        yB1 = p * yB + q * yC;
        xC1 = p * xC + q * xA; 
        yC1 = p * yC + q * yA; 
        xA = xA1; xB = xB1; xC = xC1;
        yA = yA1; yB = yB1; yC = yC1;
     } 
}

}


Solution

  • win.add(this);
    win.add(draw, BorderLayout.CENTER);
    

    You add your triangle panel to the window. When you don't specify a constraint it will default to BorderLayout.CENTER.

    Then you add the "draw" component, which replaces your panel since only one component can be added to the "CENTER".

    Try adding your triangle panel to the BorderLayout.NORTH. However, when you do this you will also need to override the getPreferredSize() method of your triangle panel, otherwise the preferred size will be (0, 0) and there will be nothing to paint.

    Read the section from the Swing tutorial on Custom Painting for more information and examples that do this.

    Edit:

    Taking a second look at your code, I'm not even sure why you created the "draw" panel. You don't add any components to it. So just get rid of the "draw" panel and let your "triangle" panel display in the BordeLayout.CENTER, but you still need to implement the getPreferredSize() method otherwise the pack() method will ignore the "triangle" panel.