Archived

This topic is now archived and is closed to further replies.

Lubb

[java] Drawing Inside Anything, I'm Stumped

Recommended Posts

I want to put the "hello" inside the (named) scrollpane [scrollPix]. Every drawing example I sem to be able to find draws directly on the window client surface, and not inside any component. This is probably easy to do, but I can''t find any actual code examples and everything I''ve guessed doesn''t work. I can add components, but can''t access any of them. -I want to name the scrollpane, so I can have more than one, and choose which to draw into--so I don''t really want some method that defaults to "the only scrollpane in the window". ~
import java.awt.*;
import java.awt.event.*;

public class button1 extends Frame {
	public static button1 button1frame;
	Color textcolor=Color.black;
	Button White_Textbtn=new Button();
	Button Red_Textbtn=new Button();
	ScrollPane scrollPix = new ScrollPane();

	/* constructor */
	public button1(String title) {
	super(title);
	}

	/* init methods */
	public void init() {
	setBackground(Color.blue);
	Panel upperpanel=new Panel();
	Panel lowerpanel=new Panel();
	White_Textbtn=new Button ("White Text");
	White_Textbtn.addActionListener(new White_Textbtnlis());
	upperpanel.add(White_Textbtn);
	Red_Textbtn=new Button ("Red Text");
	Red_Textbtn.addActionListener(new Red_Textbtnlis());
	upperpanel.add(Red_Textbtn);
	scrollPix.setSize(300, 300);
	lowerpanel.add(scrollPix);
	add(BorderLayout.NORTH, upperpanel);
	add(BorderLayout.SOUTH, lowerpanel);
	}

	public void paint (Graphics g) {
	g.setColor(textcolor);
	g.drawString("Hello",150,150);
	}

	/* class declaration */
	class White_Textbtnlis implements ActionListener {
	public void actionPerformed(ActionEvent e) {
		textcolor=Color.white;
		button1frame.repaint();
		}
	}


	/* class declaration */
	class Red_Textbtnlis implements ActionListener {
	public void actionPerformed(ActionEvent e) {
		textcolor=Color.red;
		button1frame.repaint();
		}
	}

	static class winlis extends WindowAdapter {
	public void windowClosing(WindowEvent e) {
		System.exit(0);
		}
	}

	public static void main (String args[]) {
	button1frame=new button1("button1");
	button1frame.addWindowListener(new winlis());
	button1frame.init();
	button1frame.setBounds(10,10,800,600);
	button1frame.setVisible(true);
	}

}
 

Share this post


Link to post
Share on other sites
Create a class that extends JPanel and overrides its paint method, then place an instance of that class inside your JScrollPane.

[edited by - HenryAPe on October 16, 2002 1:35:16 AM]

Share this post


Link to post
Share on other sites
You're so close already . Every component defines a paint() function, you're just overridding the one in Frame. To draw in a specific component, simply subclass that component and override its paint() function.

This might not get the expected results in a ScrollPane however. You'll probably want to subclass another component to paint in and then add that component to your ScrollPane.

EDIT: HenryApe beat me to it.

[edited by - wayfarerx on October 16, 2002 1:40:40 AM]

Share this post


Link to post
Share on other sites
Still nuts:
I added a separate file/class named drawpanel extending Panel, and added an instance of drawpanel (named myPanel) inside scrollPix.
Anything I put in the button1 init() works (like setting myPanel''s background to white) but after that, I can''t draw anything on it. I can''t figure out how to get the graphics object of it, and nothing called on it works, or usually even compiles. Where should the code to do this go? What should it look like?

Share this post


Link to post
Share on other sites
Ever closer....

import java.awt.*;
import java.awt.event.*;

public class button1 extends Frame {

public static button1 button1frame;

public static Color textcolor=Color.black;

Button White_Textbtn=new Button();
Button Red_Textbtn=new Button();

public static drawpanel myPanel = new drawpanel();
ScrollPane scrollPix = new ScrollPane();

/* constructor */
public button1(String title) {
super(title);
}

/* init methods */
public void init() {
setBackground(Color.blue);
Panel upperpanel=new Panel();
Panel lowerpanel=new Panel();

White_Textbtn=new Button ("White Text");
White_Textbtn.addActionListener(new White_Textbtnlis());
upperpanel.add(White_Textbtn);

Red_Textbtn=new Button ("Red Text");
Red_Textbtn.addActionListener(new Red_Textbtnlis());
upperpanel.add(Red_Textbtn);

scrollPix.setSize(300, 300);
myPanel=new drawpanel();
myPanel.setBackground(Color.white);


scrollPix.add(myPanel);
lowerpanel.add(scrollPix);

add(BorderLayout.NORTH, upperpanel);
add(BorderLayout.SOUTH, lowerpanel);
}

public void paint (Graphics g) {

g.setColor(textcolor);
g.drawString("Lettering should start out black.", 100, 150);

}

/* class declaration */
class White_Textbtnlis implements ActionListener {
public void actionPerformed(ActionEvent e) {
textcolor=Color.white;
myPanel.getGraphics().setColor(textcolor);
myPanel.getGraphics().drawString("Hello", 150, 150);
button1frame.repaint();
}
}

/* class declaration */
class Red_Textbtnlis implements ActionListener {
public void actionPerformed(ActionEvent e) {
textcolor=Color.red;
myPanel.getGraphics().setColor(textcolor);
myPanel.getGraphics().drawString("Hello", 150, 150);
button1frame.repaint();
}
}
static class winlis extends WindowAdapter {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
public static void main (String args[]) {
button1frame=new button1("button1");
button1frame.addWindowListener(new winlis());
button1frame.init();
button1frame.setBounds(10,10,800,600);
button1frame.setVisible(true);
}
}

#####################(external file)###################

import java.awt.*;

import java.awt.event.*;


public class drawpanel extends Panel {

/* init methods */
public void init() {

}

public void paint (Graphics g) {

}

}

By the by, why does the lettering on the Frame clip?

Share this post


Link to post
Share on other sites
There are 2 ways to draw to any component in your program. One way is to subclass the component and override the paint() method.

  
import java.awt.*;

public class MyScrollPane extends ScrollPane {
public void paint(Graphics g) {
g.drawString("Hello",100,100);
}
}

// then in your file


import java.awt.*;
import java.awt.event.*;
public class button1 extends Frame {
public static button1 button1frame;
Color textcolor=Color.black;
Button White_Textbtn=new Button();
Button Red_Textbtn=new Button();
MyScrollPane scrollPix = new MyScrollPane();


Your second option is to get a graphics context for the component

  
import java.awt.*;
import java.awt.event.*;
public class button1 extends Frame {
public static button1 button1frame;
Color textcolor=Color.black;
Button White_Textbtn=new Button();
Button Red_Textbtn=new Button();
ScrollPane scrollPix = new ScrollPane();
.
.
.
public void paint(Graphics g) {
Graphics gr=scrollPix.getGraphics();
gr.drawString("Hello",100,100);
}


The first way is the better way.



Make it work.
Make it fast.

"I’m happy to share what I can, because I’m in it for the love of programming. The Ferraris are just gravy, honest!" --John Carmack: Forward to Graphics Programming Black Book

Share this post


Link to post
Share on other sites