Results 1 to 10 of 10
- 02-27-2011, 09:13 PM #1
Senior Member
- Join Date
- Dec 2010
- Location
- Indiana
- Posts
- 202
- Rep Power
- 3
How can I make the graphics visible?
My purpose is to make a MainGraphicsPanel that holds the raw information that will be printed as well as displayed on screen. So I Made a GraphicsView class that copies from MainGraphicsPanel and displays the graphics on screen. The public int print() method will also copy from MainGraphicsPanel and translate and scale what it needs.
I have the print method commented out because I am simply trying to display the GraphicsView class on to the MainPrintPanel.
here is a SSCCE. The main idea right now is to get the graphics to display that is all.
Fubarable you helped me quite a bit with the Component.print(graphics) method. Thank you again. Now I am trying to seperate classes so they can do their own thing.
Again
MainPrintPanel - class that holds the paintComponent I would like to display on screen.
MainGraphicsReference - class that holds the graphics I would like to reference.
As you can see in the code I commented out hugeButton JButton and when I add that instead... the button displays fine. But when I switch back to the Panel nothing displays.... perhaps I have been doing too much java?
Java Code:import javax.swing.*; import java.awt.*; import java.awt.print.*; public class PrintSSCCE extends JFrame { MainPrintPanel mpp = new MainPrintPanel(); JPanel mainPanel = new JPanel(); //Panel with Add and Print buttons JPanel buttonPanel = new JPanel(); JButton addButton, printButton; PrintSSCCE() { super("Printy Printerston"); setSize(1280, 700); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Button Panel addButton = new JButton("Add"); printButton = new JButton("Print"); buttonPanel.add(addButton); buttonPanel.add(printButton); add(buttonPanel, BorderLayout.NORTH); //Test Display //JButton hugeButton = new JButton("YEAHHHH!!!"); //hugeButton.setPreferredSize(new Dimension(500, 500)); //mainPanel.add(hugeButton); // mainPanel.setPreferredSize(new Dimension(500, 500)); mainPanel.add(mpp); add(mainPanel, BorderLayout.SOUTH); setVisible(true); } // ************** MAIN PRINT PANEL *********** private class MainPrintPanel extends JPanel { MainGraphicsReference mgr = new MainGraphicsReference(); //PrinterJob job = PrinterJob.getPrinterJob(); MainPrintPanel() { setPreferredSize(new Dimension(500, 500)); //JFrame frame = new JFrame(); //mgr.setPreferredSize(new Dimension(500, 500)); //frame.getContentPane().add(mgr); //frame.setVisible(true); //add(gv); } public void paintComponent(Graphics g) { super.paintComponent(g); mgr.print(g); Graphics2D g2d = (Graphics2D) g; g2d.translate(10, 10); g2d.scale(.5, .5); } private class MainGraphicsReference extends JPanel { public void paintComponent(Graphics g) { super.paintComponent(g); //Page Graphics g.setColor(Color.WHITE); g.fillRect(0, 0, 850, 1100); } } /*public int Print(Graphics g, PageFormat pf, int page) { if(page > 0) return NO_SUCH_PAGE; this.print(g); return PAGE_EXISTS; }*/ } public static void main(String[] args) { new PrintSSCCE(); } }
- 02-28-2011, 07:27 AM #2
Hi,
I am not sure. However, I guess the problem is in your MainPrintPanel constructor.Mak
(Living @ Virtual World)
- 02-28-2011, 07:39 AM #3
Use my below code .It s working fine.
import javax.swing.*;
import java.awt.*;
import java.awt.print.*;
public class PrintSSCCE extends JFrame {
MainPrintPanel mpp = new MainPrintPanel();
JPanel mainPanel = new JPanel();
//Panel with Add and Print buttons
JPanel buttonPanel = new JPanel();
JButton addButton, printButton;
PrintSSCCE() {
super("Printy Printerston");
setSize(1280, 700);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Button Panel
addButton = new JButton("Add");
printButton = new JButton("Print");
buttonPanel.add(addButton);
buttonPanel.add(printButton);
add(buttonPanel, BorderLayout.NORTH);
//Test Display
//JButton hugeButton = new JButton("YEAHHHH!!!");
//hugeButton.setPreferredSize(new Dimension(500, 500));
//mainPanel.add(hugeButton);
//
add(mpp, BorderLayout.SOUTH);
//mainPanel.setPreferredSize(new Dimension(500, 500));
//mainPanel.add(mpp);
//add(mainPanel, BorderLayout.SOUTH);
setVisible(true);
}
// ************** MAIN PRINT PANEL ***********
private class MainPrintPanel extends JPanel {
MainGraphicsReference mgr = new MainGraphicsReference();
//PrinterJob job = PrinterJob.getPrinterJob();
MainPrintPanel() {
setPreferredSize(new Dimension(500, 500));
//JFrame frame = new JFrame();
mgr.setPreferredSize(new Dimension(500, 500));
//frame.getContentPane().add(mgr);
add(mgr);
setVisible(true);
//paintComponent(new Graphics2D(10,20,30,10));
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
mgr.print(g);
Graphics2D g2d = (Graphics2D) g;
g2d.translate(10, 10);
g2d.scale(.5, .5);
}
private class MainGraphicsReference extends JPanel {
public void paintComponent(Graphics g) {
super.paintComponent(g);
//Page Graphics
g.setColor(Color.RED);
g.fillRect(0, 0, 850, 1100);
}
}
/*public int Print(Graphics g, PageFormat pf, int page) {
if(page > 0) return NO_SUCH_PAGE;
this.print(g);
return PAGE_EXISTS;
}*/
}
public static void main(String[] args) {
new PrintSSCCE();
}
}Mak
(Living @ Virtual World)
- 02-28-2011, 02:14 PM #4
Senior Member
- Join Date
- Dec 2010
- Location
- Indiana
- Posts
- 202
- Rep Power
- 3
Fubarable. I need you man :)
I made a SSCCE and all!
I do not understand why you would add both
as well as... wouldnt that just be copying right over the paint component or vise versa?Java Code:add(mgr);
Java Code:public void paintComponent(Graphics g) { super.paintComponent(g); mgr.print(g); Graphics2D g2d = (Graphics2D) g; g2d.translate(10, 10); g2d.scale(.5, .5); }
-
I'm not 100% sure what you're trying to display with this code. Also, what is the print inside of the paintComponent supposed to do?
- 02-28-2011, 09:13 PM #6
Senior Member
- Join Date
- Dec 2010
- Location
- Indiana
- Posts
- 202
- Rep Power
- 3
I have been having an issue with already transformed and printed graphics to print properly. SO i decided to make multiple Components and take the raw info from one central location. Basically kinda like a parent class with children.
So MainPrintPanel is a seperate class and I want its paintComponent to display on the screen you see pop up, right underneith the buttons. So the paintComponent pulls the Graphics object from MainGraphicsReference. MainGraphicsReference is the main source for the Graphics object. I am also going to add a printable interface that pulls from the same source.
the print method in the paintComponent is from what you showed me the otherday in the API:
void print(Graphics g)
Invoke this method to print the component to the specified Graphics.
I hope this clears up what I am doing.Last edited by AcousticBruce; 02-28-2011 at 09:17 PM.
- 03-01-2011, 02:50 AM #7
Senior Member
- Join Date
- Feb 2011
- Posts
- 118
- Rep Power
- 0
Maybe I'm missing something, but it looks like you forgot to add your MainPrintPanel to your JFrame's content pane (getContentPane.add()). The huge button is displayed just fine because the old AWT add() method in java.awt.Container (of which JFrame is a subclass) knows what to do with a button.
- 03-01-2011, 03:15 AM #8
Senior Member
- Join Date
- Dec 2010
- Location
- Indiana
- Posts
- 202
- Rep Power
- 3
Well my friend you are a genius. That is exactly the deal.
I learned something about your post. Even though I knew it was a JPanel is a subcomponent, i didnt realise i needed get contentPane().
- 03-01-2011, 03:17 AM #9
Senior Member
- Join Date
- Dec 2010
- Location
- Indiana
- Posts
- 202
- Rep Power
- 3
thanks for taking the time on this NRiTH.
A simple issue like that can be hard to spot sometimes.
- 03-01-2011, 03:53 AM #10
Senior Member
- Join Date
- Feb 2011
- Posts
- 118
- Rep Power
- 0
Similar Threads
-
Drawing a graphics onto another Graphics ?
By Ziden in forum Java AppletsReplies: 0Last Post: 01-08-2011, 07:30 PM -
how to make the circle visible clearly, which is drawn by using Graphics object
By prasad.vara in forum AWT / SwingReplies: 3Last Post: 10-20-2010, 06:24 AM -
how to make graphic 'painted' on a button visible after disabling??
By nit_go in forum AWT / SwingReplies: 2Last Post: 01-30-2010, 05:14 AM -
GUI is visible but content is not.
By seemant.bisht in forum AWT / SwingReplies: 3Last Post: 10-07-2009, 06:28 PM -
Text are not visible in GUI
By VinTiger in forum New To JavaReplies: 5Last Post: 05-15-2009, 08:14 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks