Swing/AWT/SWT :: How To Call PaintComponent In JPanel
Nov 7, 2014
How can I make a call to paintComponent within the JPanel-class, from another Class?
In class TestClass:
public class TestClass {
public TestClass() {
JFrame frame = new JFrame();
frame.setLayout(new BorderLayout());
frame.setSize(500,500);
initComponents();
[Code] .....
View Replies
ADVERTISEMENT
Dec 2, 2014
I have been baffled by the functioning of repaint() - and the SwingPaintDemo3 with the moving square seems mysterious - you call repaint(x,y,w,h) twice and the first time it clears the clip area and the 2nd time it paints the red box. The code in paintComponent tells it to paint the box both times, yet somehow it ignores the initial box and only paints the 2nd one.
I've been writing code to bounce some balls in a box to try and understand the behavior. I set up an array of ball objects, loop through them, move them adjusting for collisions with walls and each other, then repaint(). I call repaint x2 for each ball, just like in the example. In my paintComponenet code, if I try to just paint the current ball only one ball will move, even if I send a different ball object each time. The only way to get all the balls to show up is to put a loop in paintComponenet that goes through all 100 balls every time I call it. I was worried that to move 100 balls I was painting 100x100 times.
So I put some System.out.println commands in my ball move loop, inside my object ball draw commands, and inside the paint component.
private void calculateMoveBall(BoxBalls oneBall) {
System.out.printf("
Entering calculateMoveBall [%1$2d]
",
[Code].....
So even though I called repaint() 200 times (twice for each ball), repaint was actually only called once, drew all the balls at once, and then went back. And I just noticed it appears to have waited until I exited the calculateMoveBall loop to go into paintComponent! The spooky things is how does it know to do that? Does the Java machine 'see' that it is inside of a loop, and perhaps also sees the loop inside of paintComponent, and somehow correctly guesses that it doesn't have to do it 200 times, but can wait and do it once? If I attempt to do the same thing in code, take the loop out of paintComponent() and call repaint() with the current ball, expecting the machine to do exactly what I tell it, it refuses and does it's own thing, waiting to call paintComponent on the 100th ball, drawing only the last ball (so I guess the loop inside paintComponent is not in the logic).
So a call to repaint() is a request for a higher being to decide if it has the time or energy to repaint the clip. If not, it ignores the call, or stacks them up for later (maybe I should try a million and see if it has room for that!) - well so far up to 4000 it behaves the same. This is useful if you are happy with "this is how it works so use it that way". However I really don't like having some kind of hidden logic that I have to trust to work the right way. If I don't want it to wait until later I'm not sure what to do. And I don't trust the machine to do whatever whenever. How do you debug that???
Questions: Is there documentation to know what repaint() will do or how it decides when to call paintComponent? The Swing tutorial gives the example but not the why. "By now you know that the paintComponent method is where all of your painting code should be placed. It is true that this method will be invoked when it is time to paint" "An important point worth noting is that although we have invoked repaint twice in a row in the same event handler, Swing is smart enough to take that information and repaint those sections of the screen all in one single paint operation. In other words, Swing will not repaint the component twice in a row, even if that is what the code appears to be doing." (What the code appears to be doing - now we have to guess what it is doing)
Is there a way to force repaint() to call paintComponent on a clip rectangle (not just on the whole thing?) I would think invalidate() would force repainting of the whole componenet.
Perhaps this is when you draw to a bitmap in memory and paint the whole thing on the screen...
View Replies
View Related
Oct 25, 2014
Java Code:
package com.cjburkey.games.war.g;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
[code]....
Why is paintComponent not being called by repaint();? Also, paintComponent is called twice when the program starts, that works, repaint is the thing that doesn't work.
View Replies
View Related
Oct 3, 2014
I am teaching myself AWT/Swing ... It concerns an application that creates dynamic graphics using the Graphics class. I understand that the code that creates graphics should be put in a "paintComponent" method of a (descendant of a) JPanel class. And that when you want to change something on this panel outside "paintComponent", you have to call the "repaint" method, that somehow causes "paintComponent" to be invoked.
However, I don't fully understand the explanation of the example at [URL]... . It concerns an application that moves a red square when a mouse button is clicked. The code can be found at the link, but I also repeat it below.
package painting;
import javax.swing.SwingUtilities;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.BorderFactory;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
[Code]...
This code indeed works. However, I don't understand why. The explanation at the Oracle site is the following.
Because we are manually setting the clip, our moveSquare method invokes the repaint method not once, but twice. The first invocation tells Swing to repaint the area of the component where the square previously was (the inherited behavior uses the UI Delegate to fill that area with the current background color.) The second invocation paints the area of the component where the square currently is.
However, what I don't understand: when repaint is first invoked, squareX and squareY still have their old values. If the invocation of repaint simply causes an invocation of paintComponent, then paintComponent should draw a red square at (squareX, squareY). Just like the second invocation of repaint causes paintComponent to draw a red suare at the new values of (squareX, squareY). Instead, the first invocation results in the red square being wiped out by the background color.
So apparently, the first invocation of repaint does not simply cause an invocation of paintComponent. What does it do?
View Replies
View Related
Oct 3, 2014
I would like to know the possible ways to put up JInternalFrame over JPanel to achieve the following output.
Is it even Possible because i learned JInternalFrame can only be added to JDesktopPane.?
View Replies
View Related
Feb 19, 2014
Trying to get an image to display in a GUI.
try {
ByteArrayInputStream in = new ByteArrayInputStream(
transferWorker.bOutput
.toByteArray());
BufferedImage bufferedImage = ImageIO
.read(in);
[Code] ....
But nothing is happening in the GUI. I have stepped through and bufferedimage contains the data, but its not being displayed. Whats happening is, an image is being transmitted to serial port of laptop, so i'm taking it in and storing it in a BufferOutputStream and then trying to open it in my GUI class.
View Replies
View Related
Apr 9, 2014
I have a Jframe and i want to add inside 4 different jpanel.This is easy.
what is difficult is how can i change them dynamically with 4 other new jpanels and 4 other new jpanels?
I have try with BorderLayout but not working.
It's a wizard like but not the same.
I have a jpanel with jbuttons jpanel with a jtable jpanel with textarea and jpanel with jlabels. All this have to change with other 4 jpanels, where to look?
View Replies
View Related
Dec 28, 2014
I have been trying to create a chat client which makes use of a scrollable jpanel and at runtime adds JTextFields to the panel.
Panel Initialising
msgPanel = new JPanel(new MigLayout());
msgPanel.setPreferredSize(new Dimension(400,335));
JScrollPane msgPanelScroll = new JScrollPane(msgPanel);
msgPanelScroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
msgPanelScroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
mainPanel.add(msgPanelScroll,"span 3,alignx center,wrap");
[Code] .....
But now Im facing the following issues. The text field aligns to the center of the panel & on adding more fields the panel accommodates as it can and does not begin scrolling.
Screenshots attatched
View Replies
View Related
Apr 4, 2014
I pick this code from Head first Java
package GUI;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Graphics;
import java.awt.Color;
public class gui5 {
int x=0;
int y=0;
public static void main(String... x) {
[code]...
but just a Jframe is appearing no Jpanel no green color circle?What is wrong with code
View Replies
View Related
Mar 27, 2014
I am try to do an application based in multiples JFrames, each one with its particular responsibilities, and use one JPanel as a menu with buttons that connect one JFrame to another, But this menu is instantiated at run in view (layout made by netbeans) the Main Jframe appears with its internal JPanel, but the instantiated JPanel does not appear or does not show it's buttons. (notice only run method in the second class):
JPanel Menu that will be used in all alone JFrames of application:
public class MenuSuperior extends javax.swing.JPanel {
public MenuSuperior() {
initComponents();
} private void initComponents() {
[Code] .....
View Replies
View Related
Oct 8, 2001
Let me explain by simplifying the GUI to a simple frame in which I have a JCheckbox and a JPanel mypanel.mypanel has a Textfield tf,JComboBox cmb as it's components.So now if I deselect the JCheckbox, all the components in the mypanel should be disabled. I used a code like:
[code]mycheckbox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
if(mycheckbox.isSelected())
mypanel.setEnabled(true); else
mypanel.setEnabled(false); } });[code]
but the components inside the panels are not disabled. In my actual program I have a large number of different kinds of components in mypanel.So disabling/enabling each of them on each actionPerformed of the mycheckbox will be laborious. Isn't there any way by which I can disable/enable the mypanel to disable/enable all the components in it?
View Replies
View Related
Apr 4, 2014
I trying to replace original (and empty) JPanel in JFrame with my own made one, components does no appear right, when I pass the mouse first button appears:
MainViewClass:
...
private void initComponents() {
...
MenujPanel = new MenuSuperior();
MenujPanel.setBorder(
BorderFactory.createTitledBorder("Dados Pessoais"));
[Code] .....
Here is the video:
View Replies
View Related
Apr 12, 2015
I need to add 2 JLists inside a JPanel for a crossword. The JPanel is located SOUTH and I'm using BorderLayout in the constructor to locate the JPanel.
The problem is, I can't see the 2 JLists inside the JPanel. For some strange reason the JLists appear in the center where the crosswordPanel is, even though the clues JPanel method is located SOUTH.
package crossword;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
[code]....
View Replies
View Related
Jul 3, 2014
I am trying to add buttons in a loop to my but when i compile my program i can`t see my buttons. I can see other stuff that added to jpanel ...
static JButton blocks[][];
for (int i = 0; i < block.length; i++) {
for (int j = 0; j < block[i].length; j++) {
block[i][j] = new Block(i, j);
blocks[i][j] = new JButton(i+"");
blocks[i][j].setLayout(new GridLayout((i+1) * 10, (i+1) * 10, 10, 10));
pageAxisPanel.add(blocks[i][j]);
}
}
View Replies
View Related
Jun 4, 2014
I have used the following code and I am trying to render a String in the JPanel. The text is getting displayed in the mirror image format.
I understand that there is a problem with .scale(), not able to get an idea about the corrective step. Please run the below class.
class Surface extends JPanel {
final int PAD = 40;
private void doDrawing(Graphics g)
{
Graphics2D g2d = (Graphics2D) g.create();
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
[Code] ....
View Replies
View Related
Oct 23, 2014
I'd like to enable/disable a jpanel and it's entire contents in one fell swoop. I could of course call each component's .setEnabled() method, but I figured there must be a better way!
View Replies
View Related
Jan 29, 2015
I have created a JFrame that contains 3 JPanel.
My problem is, when i clicked in the bouton to expand the Frame,the 3 JPanel are fixed and do not expanded with the Jframe.
View Replies
View Related
Jan 10, 2014
My JPanel when adding components to it will never make its self larger but it will make my components inside smaller even though I have set the minimum and preferred sizes for those components? I am using the GridBagLayout for my layout manager as I am trying to get used to it.
package manning;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
[code]....
View Replies
View Related
Apr 7, 2015
I want to delete the shape i have drawn on the panel. Here is my code
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.geom.Rectangle2D;
[Code] ....
Why it is not deleting my rectangle.
View Replies
View Related
Dec 9, 2014
I'm writing a Java Swing program for my software development class to allow a user to create a map for a side scroller video game. I'm stuck on a particular part of my GUI where I'm trying to create a properties box for specific tiles. When I draw the components onto the JPanel and display it as it is, it shows two check boxes per row. On the ScrollPane however, all the check boxes go straight onto one line and draw out the scroll pane. Right now I am only concerned with just making the basic GUI.
Here is what I have so far:
Within the main class, I am creating the JFrame and adding components to it. I am attempting to create a JScrollPane which will hold the properties check boxes...
Here is the method where I am implementing the code. It works, but it doesn't display correctly.
private void createPropertiesBox() {
PropertiesBoxPanel pbp = new PropertiesBoxPanel();
propertiesBox = new JScrollPane(pbp);
propertiesBox.setViewportView(pbp);
frame.add(propertiesBox);
[Code] .....
I've been tinkering with this code for quite some time now and I cannot seem to make a breakthrough on how to fix this.
View Replies
View Related
Oct 13, 2014
Will the code below just create a new blank window rather than call the one I have? I want to call the one I have from my main method ideally.
JMenuItem menuDelete = new JMenuItem("Delete");
menuDelete.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
DeleteEnvironmentWindow deleteEnv = new DeleteEnvironmentWindow();
deleteEnv.setVisible(true);
}
View Replies
View Related
May 4, 2015
there is a main JPanel in my application in that main JPanel i want to add two JPanels with a seperator in between . Though i have added the seperator but its taking some extra space and when i am changing its size , there is no effect . how can i remove/reduce this extra space taken by JSeperator? below is view logic ,
mainPanel = new JPanel(new GridLayout());
jp = new JPanel(new GridLayout(8, 2, 5, 5));
jp2 = new JPanel(new GridLayout(8, 1, 5, 5));
jText = new JTextField(10);
jText2 = new JTextField(10);
jText3 = new JTextField(10);
[code]....
View Replies
View Related
Apr 14, 2015
I am trying to plot a graph and graph should display when JButton is clicked. To create data set, I am taking some value through JTextField and then created a chart and plotted it. I've got a problems: the chart doesn't refresh/update when I change the text field value.
public class Test2 extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
private JPanel contentPane;
private ChartPanel chartPanel;
private JTextField textField_1;
double a;
[Code] ....
View Replies
View Related
Jun 20, 2014
Am trying to add buttons dynamically in a new Jpanel on click of a button. i am getting new Jpanel but not able to see the buttons.
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if((!(playerFirst.getText().equalsIgnoreCase("") ||playerSecond.getText().equalsIgnoreCase(""))&&(!playerFirst.getText().equalsIgnoreCase(playerSecond.getText())) ) ){
//unoGameController.playGame(playerFirst.getText(), playerSecond.getText());
CardLayout cardLayout=(CardLayout)(frmUno.getContentPane().getLayout());
[Code]...
prepareHandsForGuiCode is
public UNODeckMain prepareHandsForGui(UNODeckMain unoDeckMain){
JButton[] btnArr =new JButton[50];
for(int i=0;i<50;i++){
btnArr[i]=new JButton("hi");
panel_player1.add(btnArr[i]);
}return unoDeckMain; }
Am i skipping something ?
View Replies
View Related
Jan 15, 2014
I am having problems trying to get my jPanel to display my drawings properly after scrolling. (My drawings is done using by overriding the paint() method as the paintComponent() method does not work in my case.) My jPanel is component of my JScrollpane, and my JScrollpane is part of my jFrame.
The organization of components is: JFrame <-- JScrollPane <-- JPanel ( <-- means added to).
Situation: My drawings did appear when the frame becomes start, but when I start scrolling the scrollbar in any direction, it starts to eat up my drawing leaving only part of drawing which is not affected by scrolling.
I want my jpanel to display the drawing properly even after scrolling in both vertical and horizontal directions. I did use revalidate and repaint methods in my method for start of the JFrame.
Some Ways which I have tried:
1) I have tried adding this.revalidate() and this.repaint() method to the paint() method which did retains the image, but the jPanel keeps blinking on my screen which is trying attempt to update the drawing in a recursive manner.
2) I have tried panel.repaint() and panel.revalidate() in my paint() method, but it did not display my drawing.
3) I have read from other sources that I needed a listener to do it, but I am not sure how it works.
// Code Use for Listener
jScrollPane1.getViewport().addChangeListener(new ListenAdditionsScrolled());
public class ListenAdditionsScrolled implements ChangeListener {
public void stateChanged(ChangeEvent e) {
jPanel3.revalidate();
jPanel3.repaint();
}
}
View Replies
View Related
Feb 5, 2015
I have a problem with the size of my scrollPane. It won't fill the space in the JPanel.
is there a way to let the scrollPane fill the whole space?
View Replies
View Related