LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Java: How to define paint into a certain panel java GUI (http://www.linuxquestions.org/questions/programming-9/java-how-to-define-paint-into-a-certain-panel-java-gui-801874/)

mitchell7man 04-13-2010 09:29 PM

Java: How to define paint into a certain panel java GUI
 
My Driver:
Code:

import javax.swing.*;
public class Driver
{
        public static void main (String[] args)
        {
                JFrame frame = new JFrame("Shapes Suck");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
               
                GuiClass panel = new GuiClass();
                frame.getContentPane().add(panel);
                frame.pack();
                frame.setVisible(true);
               
               
        }
}

My GuiClass:
Code:

import javax.swing.*;
import javax.swing.plaf.basic.BasicOptionPaneUI.ButtonActionListener;

import javax.swing.border.*;
import javax.swing.event.*;
import java.util.ArrayList;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JPanel;
public class GuiClass extends JPanel
{
        JButton circle = new JButton();
        JButton oval = new JButton();
        JButton rectangle = new JButton();
        JButton square = new JButton();
       
        ArrayList<Shapes> myArrayList = new ArrayList<Shapes>();
       
        public GuiClass()
        {
        setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
        //Crate Panels For This
        JPanel top = new JPanel();
        JPanel bottom = new JPanel();
        JPanel draw = new JPanel();
        JPanel check = new JPanel();
        JPanel manip = new JPanel();
        //Set Borders for Panels
        check.setBorder(BorderFactory.createRaisedBevelBorder());
        draw.setBorder(BorderFactory.createTitledBorder("Draw"));
        TitledBorder bd = BorderFactory.createTitledBorder("Manipulate");
        bd.setTitleJustification(TitledBorder.RIGHT);
        Border bdd = BorderFactory.createLoweredBevelBorder();
        manip.setBorder(BorderFactory.createCompoundBorder(bd,bdd));
        //Set Sizes for Panels
        top.setPreferredSize(new Dimension(720, 90));
        bottom.setPreferredSize(new Dimension(720,620));
        check.setLayout(new GridLayout(2,2));
        manip.setLayout(new GridLayout(2,2));
        //check.setPreferredSize(new Dimension(120,60));
        //Set BG Color for Panels
        bottom.setBackground(Color.black);
        //Create Buttons
        //Buttons and Labels Created and added to manip2
        JLabel translateL = new JLabel("Translate");
        JLabel resizeL = new JLabel("Resize");
        JRadioButton translate = new JRadioButton();
        JRadioButton resize = new JRadioButton();
        ButtonGroup buttonGroup = new ButtonGroup();
        buttonGroup.add(resize);
        buttonGroup.add(translate);
        manip.add(translate);
        manip.add(translateL);
        manip.add(resize);
        manip.add(resizeL);
        //Buttons for and added to draw.
       
        Image circleI = getToolkit().createImage("myCircle.gif");
        ImageIcon circleIC = new ImageIcon(circleI);
        circle.setIcon(circleIC);
       
        Image ovalI = getToolkit().createImage("myOval.gif");
        ImageIcon ovalIC = new ImageIcon(ovalI);
        oval.setIcon(ovalIC);
       
        Image squareI = getToolkit().createImage("mySquare.gif");
        ImageIcon squareIC = new ImageIcon(squareI);
        square.setIcon(squareIC);
       
        Image rectangleI = getToolkit().createImage("myRectangle.gif");
        ImageIcon rectangleIC = new ImageIcon(rectangleI);
        rectangle.setIcon(rectangleIC);
        draw.add(circle);
        draw.add(oval);
        draw.add(square);
        draw.add(rectangle);
        //
        //Crap for and added to filled and coordinates options.
        JCheckBox one = new JCheckBox();
        JCheckBox two = new JCheckBox();
        JLabel jOne = new JLabel("Filled");
        JLabel jTwo = new JLabel("Coordinates");
        check.add(one);
        check.add(jOne);
        check.add(two);
        check.add(jTwo);

       
        top.add(draw);
        top.add(check);
        top.add(manip);
        add (top);
        add (bottom);
        addMouseListener (new GuiListener());
        ButtonListener listener = new ButtonListener();
        rectangle.addActionListener(listener);
        square.addActionListener(listener);
        circle.addActionListener(listener);
        oval.addActionListener(listener);
       
        }
       
private class ButtonListener implements ActionListener
{
        public void actionPerformed(ActionEvent event)
        {
               
                if (event.getSource() == circle)
                {       
                        Circle me = new Circle();
                        myArrayList.add(me);
                        repaint();                       
                }
                if (event.getSource() == oval)
                {
                        System.out.println("You clicked on oval button.");       
                        Oval me = new Oval();
                        myArrayList.add(me);
                        repaint();
                }
                if (event.getSource() == square)
                {
                        System.out.println("You clicked on square button.");
                        Square me = new Square();
                        myArrayList.add(me);
                        repaint();
                }
                if (event.getSource() == rectangle)
                {
                        System.out.println("You clicked on rectabgle button.");
                        Rectangle me = new Rectangle();
                        myArrayList.add(me);
                        repaint();
                }
        }
}
private class GuiListener implements MouseListener
{
        public void mousePressed (MouseEvent event)
        {

        }
        public void mouseClicked (MouseEvent event)
        {

        }
        public void mouseReleased (MouseEvent event)
        {

        }
        public void mouseEntered (MouseEvent event)
        {

        }
        public void mouseExited (MouseEvent event)
        {

        }
}
public void paint(Graphics page)
{
        setBackground(Color.black);
        int count1 = myArrayList.size();
        int counter = 0;
        while (counter<count1)
        {
                myArrayList.get(counter).paintComponent(page);
                counter++;
        }
}
}

Okay, so I have this thing successfully drawing the shapes when I click the buttons, thing is my gui does not look how it should. I have the top panel with the menu options that should be on top with grey background etc. What I really want to do is draw on JPanel bottom's content pane, how do I draw to that area specifically?

I may also need to modify my shapes which print as exemplefied below: (it calls into the obect...)
Code:

import java.awt.*;
import java.util.Random;
public class Oval extends Shapes
{
        public Oval()
        {
                //Generate random coordinates
                int random1 =(int)(Math.random() * 2);
                //X Coordinates
                if (random1 == 1)
                        x1 =360 - (int)(Math.random() * 180);
                else
                        x1 =360 -  (-1) * (int)(Math.random() * 180);
                //Y Coordinates
                random1 =(int)(Math.random() * 2);
                if (random1 == 1)
                        y1 =310 - (int)(Math.random() * 155);
                else
                        y1 =310 -  (-1) * (int)(Math.random() * 155);
               
                w = (int)(Math.random() * 200);
                h = (int)(Math.random() * 200);
        Random r = new Random();
        color = new Color(r.nextInt(256), r.nextInt(256), r.nextInt(256));

               
        }
        public void paintComponent(Graphics page)
        {
                super.paintComponents(page);
                page.setColor(color);
                page.drawOval(x1,y1,w,h);
        }

       
       
}


devnull10 04-14-2010 07:21 AM

You are calling paintComponent for the main JPanel which contains everything. You coutn create a new class "ImagePanel" which extends JPanel and put the code for paintComponent() in there. In the constructor for that class, pass in a paramter "parent" which is the calling object, which will then let you reference the array list in your GuiClass (if they are public - use accessor methods if not).

Code:

public class ImagePanel extends JPanel {
  GuiClass parent=null;
  public ImagePanel(GuiClass p) {
    parent=p;
  }

  public void paintComponent(Graphics g) {
    //Draw your stuff to the graphic here. You can reference the array list using the "parent" object.
  }
}

then

Code:

public class GuiClass {
  // blah blah
  JPanel draw=new ImagePanel(this);
}


mitchell7man 04-22-2010 02:18 AM

Thanks.


All times are GMT -5. The time now is 12:53 PM.