LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 04-13-2010, 08:29 PM   #1
mitchell7man
Member
 
Registered: Jan 2007
Location: Provo, UT
Distribution: OSX - Fedora
Posts: 458

Rep: Reputation: 31
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);
	}

	
	
}

Last edited by mitchell7man; 04-13-2010 at 10:57 PM. Reason: Problem Evolving
 
Old 04-14-2010, 06:21 AM   #2
devnull10
Member
 
Registered: Jan 2010
Location: Lancashire
Distribution: Slackware Stable
Posts: 547

Rep: Reputation: 115Reputation: 115
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);
}
 
Old 04-22-2010, 01:18 AM   #3
mitchell7man
Member
 
Registered: Jan 2007
Location: Provo, UT
Distribution: OSX - Fedora
Posts: 458

Original Poster
Rep: Reputation: 31
Thanks.
 
  


Reply

Tags
component, gui, java


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to define a static member in a template class wanziforever Programming 5 10-08-2009 02:23 AM
c++ question, how to define a member array, and it's size, outside of the class dec.. Winter Knight Programming 2 01-23-2007 07:28 AM
Define a tokenizer class spank Programming 1 10-19-2006 11:10 AM
class A extends B- where is paintComponent(Graphics) supposed to paint? kpachopoulos Programming 1 03-15-2006 06:28 AM
php projects: define each class in a seperate file? ldp Programming 4 09-16-2004 05:00 PM


All times are GMT -5. The time now is 10:19 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration