Josh
Josh

Reputation: 828

Drawing on a java panel - Nothing is appearing

I am trying to draw onto a panel which sits in an applet. I have 5 panels and have coloured the backgrounds just so that i can see where they are. I am trying to draw basic ellipses as a test but the result is that the panel seems to shrink and no ellipses appear.

I would be greatful for any help!!

Many thanks

import java.applet.Applet;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;


public class gui extends Applet{

private JFrame frame = new JFrame("Algorithm Animation");

private JPanel pnlRight = new JPanel();
private JPanel pnlLeft = new JPanel();
private JPanel pnlCenter = new JPanel();
private JPanel pnlTop = new JPanel();
private JPanel pnlBottom = new JPanel();

private JMenuBar mb = new JMenuBar();
private JMenu menuFile = new JMenu("File");
private JMenuItem menuItemQuit = new JMenuItem("Quit");

public void setMenuBar(){
    frame.setJMenuBar(mb);
    menuFile.add(menuItemQuit);
    mb.add(menuFile);
    //listener for quit button
    menuItemQuit.addActionListener(new ListenMenuQuit());

}

public class ListenMenuQuit implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        System.exit(-1);        
    }
}

public void setPanelTop() {
    pnlTop.setBackground(Color.BLUE);
}

public void setPanelBottom() {
    pnlBottom.setBackground(Color.BLUE);
}

public void setPanelLeft() {
    pnlLeft.setBackground(Color.GREEN);
}

public void setPanelCenter() {
    pnlCenter.setBackground(Color.RED);
}

public void setPanelRight() {
    DrawGraph drawG = new DrawGraph();
    pnlRight.add(drawG);
}

public gui() {

    setMenuBar();
    setPanelTop();
    setPanelBottom();
    setPanelLeft();
    setPanelCenter();
    setPanelRight();

    //put panels on the frame
    frame.getContentPane().setLayout(new BorderLayout());
    frame.getContentPane().add(pnlTop, BorderLayout.NORTH);
    frame.getContentPane().add(pnlBottom, BorderLayout.SOUTH);
    frame.getContentPane().add(pnlLeft, BorderLayout.WEST);
    frame.getContentPane().add(pnlCenter, BorderLayout.CENTER);
    frame.getContentPane().add(pnlRight, BorderLayout.EAST);
}

private void launch(){
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);
}

public static void main (String[] args) {
    JFrame.setDefaultLookAndFeelDecorated(false);
    gui g = new gui();
    g.launch();
}

}

and I also have a DrawGraph class (which i dont think is working):

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;

import javax.swing.JPanel;

public class DrawGraph extends JPanel {

Color c1 = Color.BLUE;
Color c2 = Color.RED;
Color c3 = Color.GREEN;
Color c4 = Color.YELLOW;
Color cl1, cl2, cl3, cl4 = Color.BLACK;

int x = 50, y = 100, w = 20, h = 20;

public DrawGraph(){
    super();
    this.setBackground(Color.ORANGE);
}

@Override
public void paintComponent(Graphics g) {
    super.paintComponent(g);

    Graphics2D g2 = (Graphics2D)g;
    g2.setColor(cl1);
    g2.drawLine(x+10, y+10, x+210, y+10);
    g2.setColor(cl2);
    g2.drawLine(x+210, y+10, x+210, y+210);
    g2.setColor(cl3);
    g2.drawLine(x+10, y+210, x+210, y+210);
    g2.setColor(cl4);
    g2.drawLine(x+10, y+10, x+10, y+210);

    Ellipse2D e1 = new Ellipse2D.Double(x, y, w, h);
    g2.setPaint(c1);
    g2.fill(e1);
    Ellipse2D e2 = new Ellipse2D.Double(x+200, y, w, h);
    g2.setPaint(c2);
    g2.fill(e2);
    Ellipse2D e3 = new Ellipse2D.Double(x, y+200, w, h);
    g2.setPaint(c3);
    g2.fill(e3);
    Ellipse2D e4 = new Ellipse2D.Double(x+200, y+200, w, h);
    g2.setPaint(c4);
    g2.fill(e4);

}
}

Any help would be hugely appreciated.

Josh

Upvotes: 1

Views: 3136

Answers (3)

melli-182
melli-182

Reputation: 1234

Did you tried repacking the panels? Or loading them again? I worked in Java long time ago but I remember that I had to refresh the panels in order to display the actual data into them.

Upvotes: 0

KV Prajapati
KV Prajapati

Reputation: 94635

You can use setPreferredSize() method to set width and height of DrawGraph panel.

public DrawGraph(){
    super();
    this.setBackground(Color.ORANGE);
    setPreferredSize(new Dimension(400,400));
}

Upvotes: 2

r.piesnikowski
r.piesnikowski

Reputation: 2951

Since your:

public class DrawGraph extends JPanel

You should put this JPanel subclass to JFrame directly.

frame.getContentPane().add(new DrawGraph(), BorderLayout.EAST);

insted of:

public void setPanelRight() {
    DrawGraph drawG = new DrawGraph();
    pnlRight.add(drawG);
}

But this is just a first thought and may not work.

Upvotes: 0

Related Questions