I am trying to draw a coloured bar, which gets bigger as time goes on. It works when I use the default layoutmanager, but when im trying to implement this with GridBagLayout it wont. I wrote a test project just for testing purposes to investigate what the problem is. I added a few buttons just to have something else apart from the graphics2D object, so it kinda looks like my actual project im working on. But after a couple of days, i have to admit that i dont have a clue, what is going wrong. I hope someone can help me!
Just in advance, dont get confused by some words i used in the code. my mother tongue isn't english.
import java.awt.*;
import javax.swing.*;
import java.util.TimerTask;
import java.util.Timer;
public class FarbBalkenTest extends JPanel {
static Timer timer = new Timer ();
static TimerTask task ;
static int time;
public static void main(String[] args) {
FarbBalkenTest fbt = new FarbBalkenTest();
fbt.init();
}
public static void addComponent(Container cont, GridBagLayout gbl, Component c, int x, int y, int width, int height, double weightx, double weighty ){
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.BOTH;
gbc.gridx=x;
gbc.gridy=y;
gbc.gridwidth=width;gbc.gridheight=height;
gbc.weightx=weightx;gbc.weighty=weighty;
gbl.setConstraints(c,gbc);
cont.add(c);
}
public void init (){
final JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setBackground(new Color(230,230,230));
Container c = frame.getContentPane();
GridBagLayout gbl = new GridBagLayout();
c.setLayout(gbl);
ZeichenTest z_test = new ZeichenTest();
addComponent(c,gbl, new Button("top left"), 0,0,1,1,1.0,1.0);
addComponent(c,gbl, new Button("top right"), 2,0, 1,1,1.0,1.0);
addComponent(c, gbl,z_test, 1,1,1,1,1.0,1.0);
addComponent(c,gbl, new Button("down left"),0,2,1,1,1.0,1.0);
addComponent(c,gbl, new Button("down right"),2,2,1,1,1.0,1.0);
frame.setSize(500,500);
frame.setVisible(true);
task = new TimerTask(){
public void run(){
frame.repaint();
time+= 20;
System.out.println(time);
}
};
timer.scheduleAtFixedRate(task, 0, 1000);
}
public class ZeichenTest extends JComponent {
public void paintComponent (Graphics g){
Graphics2D g2d = (Graphics2D) g;
Color startgreen = new Color(50,205,50);
Color endred = new Color (255, 97, 3);
GradientPaint startend = new GradientPaint(0,25 , startgreen, 400, 25 , endred );
g2d.setPaint (startend);
g2d.fillRect(50, 200 , time, 50);
}
}
}
"It works when I use the default layoutmanager, but when im trying to implement this with GridBagLayout it wont."
GBL respects preferred sizes. Your ZeichenTest has none. You need to explicitly set it by overriding getPreferredSize(). (The default layout of JFrame which is BorderLayout doesn't respect preferred sizes, and will stretch you panel to fit)
public class ZeichenTest extends JComponent {
...
#Override
public Dimension getPreferredSize() {
return new Dimension( ... , ... );
}
}
Also note, after setting the preferred size of the component, just call pack() on the frame, instead of setSize(). The pack() will "fit" everything according to the preferred sizes. You can actually test that your component currently has no preferred size by calling pack() instead of setSize() and you will see the frame shrink on launching. But if you override the getPreferredSize() of the component and call pack, the frame will be size according the new preferred size.
Other Important notes:
Use java.swing.Timer instead of TimerTask. repaints should be done on the EDT and Swing Timer handles this for you. See more at How to Use Swing Timer
I think you mean to repaint() the instance of ZeichenTest and not the frame. It makes a difference.
Always call super.paintComponent in your paintComponent method, as to not leave nasty paint atrifacts
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
}
Swing apps should always be run on the Event Dispatch Thread (EDT). See Initial Threads. Basically, in this case, just wrap the main code in a SwingUtilities.invokeLater(...)
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
public void run() {
FarbBalkenTest fbt = new FarbBalkenTest();
fbt.init();
}
});
}
Related
I have a custom JLayeredPane, and I am repainting it in my game loop. There are two custom JPanels added into the JLayeredPane. These are foreground and background JPanels. How do I successfully only draw my background JPanel once, (And repaint when window is re-sized or any other reason) to reduce impact on system resources, while continuing to update my foreground JPanel constantly.
To re-iterate, I dont want to constantly repaint the background JPanel in a loop. I want to repaint it only when it is nessessary, as the background does not change. and is large.
In my attempt to do this, I have only drawn the background once. However. the background JPanel is simply not visible. while the foreground JPanel updates as normal. It is almost as if the foreground JPanel paints ontop of the background JPanel, even though I have both of the JPanels set to setOpaque(false)
I have made a mvce which shows my attempt at only drawing the background JPanel once, while updating the foreground JPanel constantly.
The problem with my code is that the background JPanel does not show.
Now. I know that if I were to draw it constantly it would show. But that defeats the purpose of what i'm trying to do. I am trying to only draw it once, and have be seen at the same time
My code successfully only draws the background JPanel once. The problem is that the background JPanel does not show. How do I fix THIS problem
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Main extends JLayeredPane {
static JFrame frame;
static Main main;
static Dimension screenSize;
public Main() {
JPanel backPanel = new BackPanel();
JPanel frontPanel = new FrontPanel();
add(backPanel, new Integer(7));
add(frontPanel, new Integer(8));
new Thread(() -> {
while (true){
repaint();
}
}).start();
}
public static void main(String[] args) {
screenSize = Toolkit.getDefaultToolkit().getScreenSize();
frame = new JFrame("Game"); // Just use the constructor
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
main = new Main();
frame.add(main, BorderLayout.CENTER);
frame.pack();
frame.setSize(screenSize);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public class BackPanel extends JPanel{
public boolean drawn = false;
public BackPanel(){
setVisible(true);
setOpaque(false);
setSize(screenSize);
JLabel test1 = new JLabel("Test1");
JLabel test2 = new JLabel("Test2");
add(test1);
add(test2);
}
#Override
public void paintComponent(Graphics g){
super.paintComponent(g);
drawOnce(g);
}
public void drawOnce(Graphics g){
if (!drawn){
g.setColor(Color.red);
g.fillRect(0, 0, screenSize.width, 200);
drawn=true;
}
}
}
public class FrontPanel extends JPanel{
public FrontPanel(){
setVisible(true);
setOpaque(false);
setSize(screenSize);
JLabel test = new JLabel("Test");
add(test);
}
#Override
public void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(Color.blue);
g.fillRect(0+screenSize.width/2, 0, screenSize.width/4, 300);
}
}
}
Try RepaintManager.currentManager(component).markCompletelyClean(component). It will prevent the component from repainting. You might need to do this after each time you add new components.
http://docs.oracle.com/javase/6/docs/api/javax/swing/RepaintManager.html#markCompletelyClean%28javax.swing.JComponent%29
I don't know if this two lines of code
super.paintComponent(g);
drawOnce(g);
are the root of problem, I sincerly don't remember how paintComponent works (a test could help) but try to swap them :
drawOnce(g);
super.paintComponent(g);
maybe, on your original version, you tells JVM to paint the whole component and, only after the AWTEvent has been added to the queue, to draw what you need.
I guess that the awt's documentation will explain it.
I'm trying this very simple code. It runs but doesn't show the animation. I'm new to animations, so I don't know what I'm missing.
package sample;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Sample extends JPanel implements ActionListener {
Timer tm = new Timer(5, this);
int x = 0, Velx = 5;
public void paint(Graphics g) {
g.setColor(Color.RED);
g.fillRect(x, 30, 50, 50);
tm.start();
}
public void actionPerformed(ActionEvent e) {
x = x + Velx;
repaint();
}
public static void main(String[] args) {
Sample X = new Sample();
JFrame a = new JFrame();
a.setTitle("Rectangle RED");
a.setSize(500,500);
a.setVisible(true);
}
}
Sample X = new Sample();
X is never added to the frame. See first tip (the bold part) for how to add X to the frame.
Other tips:
Sample should #Override the getPreferredSize() method to return a sensible size for the canvas. Then we can dispense with a.setSize(500,500); and instead a.add(X); a.pack(); to get the frame to be the exact right size to display the rendering.
The Timer should be started in some place other than the paint methods! I'd go for the constructor.
Custom painting in any JComponent should be done in the paintComponent(Graphics) method.
In all custom painting, we should immediately call the super method to ensure that previous drawings are erased by painting the BG and border of the container.
Please learn common Java nomenclature (naming conventions - e.g. EachWordUpperCaseClass, firstWordLowerCaseMethod(), firstWordLowerCaseAttribute unless it is an UPPER_CASE_CONSTANT) and use it consistently.
JFrame a = new JFrame(); a.setTitle("Rectangle RED"); could be shortened to JFrame a = new JFrame("Rectangle RED");
after running this code appears windows sized 0x0.
i can fix it by writting "f.setSize(640, 480);" in the main method.
i hear its bad pratice, so.. is there way to make this overriding work?
public class Gui {
public static void main(String[] args) throws IOException {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().setBackground(Color.BLACK);
f.add(new DrawingPad());
f.setVisible(true);
}
}
class DrawingPad extends JComponent{
Image img = new ImageIcon("res/icon.jpg").getImage();
Dimension d = new Dimension(640, 480);
public void paintComponent(Graphics g){
g.drawImage(img, 50, 50, null);
}
#Override
public Dimension getPreferredSize(){return d;}
}
Call f.pack() before making the frame visible.
Quote from the javadoc:
Causes this Window to be sized to fit the preferred size and layouts of its subcomponents. The resulting width and height of the window are automatically enlarged if either of dimensions is less than the minimum size as specified by the previous call to the setMinimumSize method.
Also, you should not use swing components from the main thread. Only from the event dispatch thread. Read http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html
So I have a JPanel that's inside a JScrollPane.
Now I am trying to paint something on the panel, but it's always in same spot.
I can scroll in all directions but it's not moving. Whatever I paint on the panel does not get scrolled.
I already tried:
A custom JViewPort
switching between Opaque = true and Opaque = false
Also I considered overriding the paintComponent method of the panel but that would be really hard to implement in my code.
public class ScrollPanePaint{
public ScrollPanePaint() {
JFrame frame = new JFrame();
final JPanel panel = new JPanel();
panel.setPreferredSize(new Dimension(1000, 1000));
//I tried both true and false
panel.setOpaque(false);
JScrollPane scrollPane = new JScrollPane(panel);
frame.add(scrollPane);
frame.setSize(200, 200);
frame.setVisible(true);
//To redraw the drawing constantly because that wat is happening in my code aswell because
//I am creating an animation by constantly move an image by a little
new Thread(new Runnable(){
public void run(){
Graphics g = panel.getGraphics();
g.setColor(Color.blue);
while(true){
g.fillRect(64, 64, 3 * 64, 3 * 64);
panel.repaint();
}
}
}).start();
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
new ScrollPanePaint();
}
});
}
}
The mistake I make is probably very easy to fix, but I just can't figure out how.
How to implement the paintComponent() on JPanel?
Override getPreferredSize() method instead of using setPreferredSize()
final JPanel panel = new JPanel(){
#Override
public void paintComponent(Graphics g){
super.paintComponent(g);
// your custom painting code here
}
#Override
public Dimension getPreferredSize() {
return new Dimension(40, 40);
}
};
Some points:
Override JComponent#getPreferredSize() instead of using setPreferredSize()
Read more Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?
Use Swing Timer instead of Java Timer that is more suitable for Swing application.
Read more How to Use Swing Timers
Set default look and feel using UIManager.setLookAndFeel()
Read more How to Set the Look and Feel
How to fix animation lags in Java?
Try to draw a rectangle with different size, How to fit it in one frame proportionally(assume the frame is fixed)?
public class Draw extends JComponent {
public void paint(Graphics g) {
int width = 100;
int length = 100;
g.drawRect(10, 10, width, length);
}
}
public class DrawRect {
public static void main(String[] a) {
JFrame frame = new JFrame();
frame.setSize(400, 600));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
Container content = frame.getContentPane();
content.add(new Draw());
}
}
Custom painting is done by overriding the paintComponent(...) method, not the paint() method. This advice is made daily. Search the forum for more information and examples.
If you want to know the space available to the component then you can invoke the getWidth() and getHeight() method. Once you know these values you can determine how big you want to paint your rectangle.
Components should be added to the frame BEFORE the frame is made visible.
You don't need to use the getContentPane() method. Since JDK5 you can just add components directly to the frame and they will be added to the content pane for you.