Not able to see the graphics stuff inside paint() method - java

I'm doing all Graphics stuffs such as g.drawLine() etc. inside class MyCanvas.java
public class MyCanvas extends JComponent{
public void paint(Graphics g){...}
}
And my Main.java class is
class Main{
public static void main(String[] args){
JFrame window = new JFrame();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setBounds(30, 30, 1000, 350);
window.getContentPane().setLayout(new FlowLayout());
MyCanvas mc = new MyCanvas();
JScrollPane jsp = new JScrollPane(mc);
jsp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
window.getContentPane().add(jsp);
window.getContentPane().setBackground(Color.WHITE);
window.setVisible(true);
}
}
However, JFrame is only showing white plain background, It isn't showing any Graphics stuff from paint(Graphics g).
Note that, If I remove JScrollPane from above Main.java, then it shows everything perfectly.
class Main{
public static void main(String[] args){
JFrame window = new JFrame();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setBounds(30, 30, 1000, 350);
window.getContentPane().add(new MyCanvas());
window.getContentPane().setBackground(Color.WHITE);
window.setVisible(true);
}
}
Where my logic is going wrong? Is there any problem while adding JScrollPane ? Please help. Thanks.

class MyCanvas extends JComponent{
public void paint(Graphics g){...}
}
Should be:
class MyCanvas extends JComponent{
#Override
public void paintComponent(Graphics g){...}
}
By using paintComponent we respect the painting chain. Also be sure to add super.paintComponent(g); as the first statement in the overridden method, to assure that the BG color and borders etc. are painted.

Related

Why can't I draw in a JPanel?

public void display() {
pan.repaint();
fen.add(pan);
fen.addKeyListener(this);
fen.setResizable(false);
fen.setTitle("Le Jeu 2D");
img.setText("Coucou");
pan.add(img);
pan.repaint();
pan.setBackground(Color.yellow);
fen.setVisible(true);
fen.setSize(480, 272);
pan.repaint();
fen.revalidate();
}
public void paintComponent(Graphics g) {
System.out.println("zzz");
pan.paint(g);
g.setColor(Color.red);
g.drawRect(10, 10, 10, 10);
}
It doesn't draw anything. Why? I have defined the paint component method, so I don't understand why.
Edit: I edited my code, please take a look
You don't create an instance of your Game class and don't add it to the JFrame.
Here is the Game panel which you are painting on:
Game.java
public class Game extends JPanel {
#Override
public final void paintComponent(final Graphics g) {
super.paintComponent(g);
g.setColor(Color.red);
g.drawRect(10, 10, 10, 10);
}
#Override
public Dimension getMinimumSize() {
return new Dimension(300, 300);
}
#Override
public Dimension getPreferredSize() {
return new Dimension(300, 300);
}
#Override
public Dimension getMaximumSize() {
return new Dimension(300, 300);
}
}
You then need to create an instance of this Game panel and add it to your JFrame:
GameFrame.java
public class GameFrame extends JFrame {
public GameFrame() {
setLocationRelativeTo(null);
setResizable(false);
setTitle("2D Game");
Game game = new Game();
setContentPane(game);
pack();
setVisible(true);
}
}
Then when you create an instance of the JFrame:
Example.java
public class Example {
public static void main(String[] args) {
new GameFrame();
}
}
The panel will be added, and painted:
You never create an instance of the Game class and you never add the Game class to the frame. Even if you did create an instance the size would still be (0, 0) so there would be nothing to paint.
Basically the whole structure of your code is wrong.
I suggest you start over and start with the demo code found in the Swing tutorial on Custom Painting.
The basic structure of your code seems weird.
You could instantiate a JFrame in your main control class, ie. it should be GAME class in this case. Then you can create a new JPanel class and add its object into the JPanel object. Within the JPanel class, you can create all the elements you need and set corresponding parameters. You could also add the event listener in an inner class or a separate class.

How to draw a roundedRectangle with text inside a class extending JPanel

I'm new with Java and I'm trying to a class that extends JPanel. I have a updateView() method that is suppose to draw rounded rectangles with text in the middle inside the JPanel. Also, I would need to stock the textfield and the rounded rectangle in a LinkedHashMap. I already got a var for this, I just don't know witch type of var I need to use.
I'have been searching the solution for a while, and all the answers I found are either to complicated for me to understand or just doesn't apply to my case.
I know that it's almost nothing, but here is what I got so far...
package game;
import javax.swing.JPanel;
public class GameNumView extends JPanel
{
private Map<Integer,Integer> backgroundText = new LinkedHashMap<"My rounded rectangle","My textfield">();
public GameNumView()
{
}
public void UpdateView(String[] pNumbers)
{
//Create the background
//Create the text
}
}
I'll give you the right path. It's up to you to adapt it to your existing code.
The idea is to use the Graphics object with the paintComponent method of an extended class of JPanel.
Here is the code, it is clear enough I think but if you have any question, do not hesitate.
The MyFrame class :
public class MyFrame extends JFrame {
public MyFrame(){
this.setTitle("Hello");
this.setSize(200, 200);
this.setLocationRelativeTo(null);
MyPanel pan = new MyPanel();
pan.setBackground(Color.ORANGE);
this.setContentPane(pan);
this.setVisible(true);
}
public static void main(String[] args) {
MyFrame f = new MyFrame();
}
}
And here is the MyPanel class :
public class MyPanel extends JPanel{
#Override
public void paintComponent(Graphics g) {
g.setColor(Color.black);
g.drawRoundRect(10, 10, this.getWidth()-20, this.getHeight()-20, 15, 15);
g.setColor(Color.black);
g.drawString("Hello", 75, 75);
}
}
And here is a picture of what you should have :

Can we use Paint Component without extending JPanel Class?

I'm making a gui in which draw a line using paint component method i know i can do that with extends JPanel. But i really want to know how to do this without extending anything.Like in this program
public class Main {
static JPanel panel;
public static void main(String[] args) {
JFrame frame = new JFrame("Our Frame");
panel = new JPanel();
frame.setSize(600,480);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.add(panel);
}
}
And on this Way
public class Main {
public static void main(String[] args) {
Frame obj = new Frame();
}
}
The frame
public class Frame extends JFrame{
private JPanel panel;
public Frame(){
panel = new JPanel();
setSize(600,480);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
add(panel);
}
}
You can do something like this:
JPanel panel = new JPanel(true) {
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
// do painting
}
}
Well, technically you can use component.getGraphics(); to get the graphics context for a given component, then draw on that. But since the component will also repaint itself, your drawing will vanish as soon as the repaint occurs. So don't even think about doing that, you will only waste your time!
However if you have a component that renders a BufferedImage in paintComponent(), you can draw on the image (with img.getGraphics(); etc.) and the component will then reflect those drawings. This will allow you to have a simple MyPanel class, where paintComponent() will only draw the image. You can then get a reference to the image, and draw on that from whereever you want.
You're already extending something in your code, and in your case it's completely unnecessary. You can remove your Frame class and display a JFrame with the following code.
JFrame f = new JFrame();
f.setSize(600, 480);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLocationRelativeTo(null);
f.add(panel);
f.setVisible(true);

Java GUI: How to add several classes to "Frame" class [duplicate]

This question already has an answer here:
Java GUI repaint() problem?
(1 answer)
Closed 8 years ago.
I am a complete beginner at Java coding and I have never worked with GUI before. Here's what I am trying to do. I have main class "Frame" and two classes: "Circle" and "Square". How do I add Circle and Square to Frame that they'll appear on the frame?
Sorry for this easy question but I need your help on this.
Thanks in advance!
public class Frame extends JFrame{
public static void main(String[] args) {
Frame f = new Frame();
Circle circle = new Circle();
Square square = new Square();
f.add(circle);
f.add(square);
}
public Frame(){
setTitle("Frame");
setSize(500, 500);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
public class Circle extends JFrame{
public Circle(){
}
public void paint(Graphics g){
g.setColor(Color.GREEN);
g.drawOval(300, 300, 200, 200);
}
}
public class Square extends JFrame{
public Square(){
}
public void paint(Graphics g){
g.setColor(Color.GREEN);
g.drawRect(300, 300, 500, 500);
}
}
First off all you cannot add a JFrame to a JFrame because you would be adding a window to a window. What you could do though is make circle and square extend JPanel. This way you would be able to add them to your JFrame without any problem and they will do what they have to do. Also my JDK tells me that Circle and Square should have their own file. And also depending on the order you add square and circle in to the JFrame, you'll only see one of them because the second one is painted over the first one. Ok so after this your code would transform into:
File 1 Frame:
public class Frame extends JFrame{
public static void main(String[] args) {
Frame f = new Frame();
Circle circle = new Circle();
Square square = new Square();
f.add(circle);
f.add(square);
}
public Frame(){
setTitle("Frame");
setSize(500, 500);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
File 2 Circle:
public class Circle extends JPanel{
public Circle(){
}
public void paint(Graphics g){
g.setColor(Color.GREEN);
g.drawOval(300, 300, 200, 200);
}
}
File 3 Square:
public class Square extends JPanel{
public Square(){
}
public void paint(Graphics g){
g.setColor(Color.GREEN);
g.drawRect(300, 300, 500, 500);
}
}
Hope this help you and that you understand why this works. If you do not feel free to ask for more clarification.
P.S. Welcome to Java GUI programming ;)

drawing on Jframe

I cannot get this oval to draw on the JFrame.
static JFrame frame = new JFrame("New Frame");
public static void main(String[] args) {
makeframe();
paint(10,10,30,30);
}
//make frame
public static void makeframe(){
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel emptyLabel = new JLabel("");
emptyLabel.setPreferredSize(new Dimension(375, 300));
frame.getContentPane().add(emptyLabel , BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
// draw oval
public static void paint(int x,int y,int XSIZE,int YSIZE) {
Graphics g = frame.getGraphics();
g.setColor(Color.red);
g.fillOval(x, y, XSIZE, YSIZE);
g.dispose();
}
The frame displays but nothing is drawn in it. What am I doing wrong here?
You have created a static method that does not override the paint method. Now others have already pointed out that you need to override paintComponent etc. But for a quick fix you need to do this:
public class MyFrame extends JFrame {
public MyFrame() {
super("My Frame");
// You can set the content pane of the frame to your custom class.
setContentPane(new DrawPane());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 400);
setVisible(true);
}
// Create a component that you can actually draw on.
class DrawPane extends JPanel {
public void paintComponent(Graphics g) {
g.fillRect(20, 20, 100, 200); // Draw on g here e.g.
}
}
public static void main(String args[]){
new MyFrame();
}
}
However, as someone else pointed out...drawing on a JFrame is very tricky. Better to draw on a JPanel.
Several items come to mind:
Never override paint(), do paintComponent() instead
Why are you drawing on a JFrame directly? Why not extends JComponent (or JPanel) and draw on that instead? it provides more flexibility
What's the purpose of that JLabel? If it sits on top of the JFrame and covers the entire thing then your painting will be hidden behind the label.
The painting code shouldn't rely on the x,y values passed in paint() to determine the drawing routine's start point. paint() is used to paint a section of the component. Draw the oval on the canvas where you want it.
Also, you're not seeing the JLabel because the paint() method is responsible for drawing the component itself as well as child components. Overriding paint() is evil =)
You are overriding the wrong paint() method, you should override the method named paintComponent like this:
#Override
public void paintComponent(Graphics g)
You need to Override an exist paint method that actually up to dates your Frame. In your case you just created your custom new method that is not called by Frame default.
So change your method to this:
public void paint(Graphics g){
}

Categories