Manual
Manual

Reputation: 1687

Status bar , how to make my lines,ovals and rects visible?

I have my lines, rectangles and ovals set and I have one question.

How can I make my lines, ovals and rectangles visible in the main method? For some reason they are not being outputted when I compile and run the file... thanks

 import java.awt.BorderLayout;
 import java.awt.Color;

 import java.awt.Dimension;
 import java.awt.Graphics;


 import javax.swing.JPanel;

 public class LinesRectsOvalsJPanel extends JPanel {


public LinesRectsOvalsJPanel() {


setLayout(new BorderLayout());
setPreferredSize(new Dimension(10, 23));

JPanel rightPanel = new JPanel(new BorderLayout());
rightPanel.setOpaque(true);

add(rightPanel, BorderLayout.EAST);
   }

  public void paintComponent( Graphics g ) {

   super.paintComponent( g ); 

     this.setBackground( Color.WHITE );
     //   x y width height
     g.setColor(Color.BLACK);
     g.drawLine(5,10,5,30);
     g.setColor(Color.BLUE);
     g.drawLine(18,70,127,24);
     g.setColor(Color.RED);
     g.drawLine(25,45,100,38);

     g.setColor(Color.YELLOW);
     g.drawOval(23,25,23,55);
     g.setColor(Color.BLACK);
     g.drawOval(15,14,40,78);
     g.setColor(Color.CYAN);
     g.drawOval(180,102,5,90);
     g.setColor(Color.RED);
     g.drawOval(21,20,89,11);
     g.setColor(Color.BLUE);
     g.drawOval(35,87,39,27);
     g.setColor(Color.YELLOW);
     g.fillRect(87,5,5,60);
     g.setColor(Color.GREEN);
     g.fillRect(105,15,15,85);
     g.setColor(Color.CYAN);
     g.fillRect(14,45,76,86);
     g.setColor(Color.RED);
     g.fillRect(70,79,65,86);
     g.setColor(Color.BLUE);
     g.fillRect(90,108,5,8); 
 }
}

Code 2

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
 import java.awt.Dimension;
 import java.awt.Graphics;

 import javax.swing.BoxLayout;
 import javax.swing.JFrame;
 import javax.swing.JLabel;
  import javax.swing.JPanel;
  import javax.swing.SwingConstants;
 import javax.swing.border.BevelBorder;

  public class Main {
    public static void main(String[] args) {
  JFrame frame = new JFrame("");
 frame.setLayout(new BorderLayout());
frame.setSize(300, 300);

JPanel statusPanel = new JPanel();
statusPanel.setBorder(new BevelBorder(BevelBorder.LOWERED));
frame.add(statusPanel, BorderLayout.SOUTH);
statusPanel.setPreferredSize(new Dimension(frame.getWidth(), 16));
statusPanel.setLayout(new BoxLayout(statusPanel, BoxLayout.X_AXIS));
JLabel statusLabel = new JLabel("status");
statusLabel.setHorizontalAlignment(SwingConstants.LEFT);
statusPanel.add(statusLabel);

frame.setVisible(true);
}
}

Upvotes: 0

Views: 248

Answers (2)

Branislav Lazic
Branislav Lazic

Reputation: 14816

Try something like this:

This is the main class (frame):

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class Frame extends JFrame{

    Panel p = new Panel();

    public Frame(){
        p.setPreferredSize(new Dimension(640,480));
        add(p);
    }

    public static void main(String[] args){
        SwingUtilities.invokeLater(new Runnable(){
            public void run(){
                Frame f = new Frame();
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                f.pack();
                f.setVisible(true);
            }   
        });
    }
}

Your panel class:

import javax.swing.*;

public class Panel extends JPanel{
    public void paintComponent(Graphics g){
        g.setColor(Color.BLUE);
        g.fillRect(20, 20, 50, 50);
    }
}

Upvotes: 0

Reimeus
Reimeus

Reputation: 159844

You have not added your LinesRectsOvalsJPanel component:

frame.add(new LinesRectsOvalsJPanel());

Upvotes: 2

Related Questions