paintcomponent

GUI freezes when drawing Wave from animation on JPanel though i used Swing Timer

一个人想着一个人 提交于 2019-11-28 05:29:09
问题 plese look at my code snippets , wha is wrong with it , it frrezes GUI when the Swing timer stats which is repeteadly paints on the jpnael ?? class WaveformPanel extends JPanel { Timer graphTimer = null; AudioInfo helper = null; WaveformPanel() { setPreferredSize(new Dimension(200, 80)); setBorder(BorderFactory.createLineBorder(Color.BLACK)); graphTimer = new Timer(15, new TimerDrawing()); } /** * */ private static final long serialVersionUID = 969991141812736791L; protected final Color

Components disappear after resizing JPanel

放肆的年华 提交于 2019-11-28 05:24:42
问题 I am trying to create JPanel with draggable crosses which appear after mouse clicking. Everything works fine but when I resize the JPanel the crosses disappear. I tried to override the paintComponent method in my JPanel but then all crosses are at coordinates (0,0). How can I fix it? import java.awt.*; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.MouseMotionAdapter; import java.util.ArrayList; import javax

java what is heavier: Canvas or paintComponent()?

喜你入骨 提交于 2019-11-28 02:26:50
Can someone tell me what should I use for painting graphics on a JPanel : Canvas , or simply drawing everything in paintComponent() ? I am painting hundreds of small images around 30 times per second, and I wonder which one would be the most lightweight, and in what conditions should I use both? Thanks. MadProgrammer The issue is rather broad and the context rather slim. Consider taking a look at Rotating multiple images causing flickering. Java Graphics2D and Swing animation running extremely slow for example. The first uses paintComponent to render up to 10, 000 rotating images at 25fps (or

how can i put a JButton on an image?

核能气质少年 提交于 2019-11-28 01:05:00
问题 I am trying to fix a JFrame where there will be a background image and on the image JButtons which will do some commands. I try to do it without layout because i want to put small buttons in some specific locations on the JFrame but every time i do it, the background image comes to the front or the JFrame has size equal to the JFrame size. With the following code, the JButton has the same size to JFrame. I have tried to change the size and location of the JButton but nothing. Can you help me

Not showing graphics in JPanel which is added to another JPanel

北城余情 提交于 2019-11-27 16:20:49
When adding a JPanel that has graphics to a JFrame , it's working fine. But when I try to add a JPanel in which I have added another JPanel with graphics, its not showing in the JFrame . Please see code below package sample; import java.awt.Graphics; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class Main extends JFrame{ public static void main(String[] args) { new Main(); } public Main(){ setTitle("Sample"); setVisible(true); setSize(500,500); setDefaultCloseOperation(EXIT_ON_CLOSE); add(new SamplePanel2()); } } class SamplePanel2 extends JPanel{

Why not to draw directly inside JFrame [duplicate]

99封情书 提交于 2019-11-27 15:58:22
This question already has an answer here: Difference between paint() and paintcomponent()? 2 answers Can anyone explain me why shouldn't i use paint method to draw directly inside a JFrame window, and i should use paintComponent method with a JPanel inside the JFrame ? Thanks in advance. MadProgrammer Three main reasons... Top level containers aren't double buffered, which cause flickering when the frame is repainted, yes, you can implement you're own double buffering, but... Painting inside a frame does not take into consideration the frames borders, meaning that it's possible to paint under

Implementing a mouse click event on a tile in a map

时间秒杀一切 提交于 2019-11-27 09:51:47
I'm trying to implement a mouse click event for an image (basically a tile on a map) on a JPanel. I'm just not able to figure out how to go about it. I have a Main class that extends JPanel. I'm retrieving tiles from a tile server and displaying them in the paintComponent() method of the Main class based on the specific zoom level. I use tiny locator images to represent a specific monument or a building in a city in the same paintComponent() method. They are placed on top of these tiles based on corresponding latitude and longitude. When I click on these locator images, I must be able to add

paintComponent draws other components on top of my drawing

人走茶凉 提交于 2019-11-27 09:42:08
I'm trying to build a simple paint tool. The mouseDrag events creates a new ellipse and causes my JPanel to repaint() . This works fine so far. However, if I press any button (or any other UI component) before firing the mouseDrag event for the first time, the button is painted in the upper left corner of my panel. I have isolated the code into this test application: import java.awt.BasicStroke; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import javax.swing

Why does calling dispose() on Graphics object cause JPanel to not render any components

半城伤御伤魂 提交于 2019-11-27 09:29:32
After learning that dispose() should be called on Graphics / Graphics2D object after use, I went about changing my game to incorporate this. When I added g2d.dispose() in overridden paintComponent(Graphics g) of JPanel , my components which I added (extensions of JLabel class) where not rendered I was able to still click on them etc but they would not be painted . I tested with a normal JLabel and JButton with same effect (though JButton is rendered when mouse is over it). So my question is why does this happen? Here is an SSCCE to demonstrate: after uncommenting call to dispose() in

JPanel doesn't update until resize Jframe

北慕城南 提交于 2019-11-27 09:25:07
I subclass JPanel to overwrite paintComponent(Graphics), I want to draw an image onto jpanel in a jframe. But my image hasn't shown up until I make a change to jframe's size. This is my code: public class ImagePanel extends JPanel{ public void setImage(BufferedImage bi) { image = bi; revalidate(); } @Override public void paintComponent(Graphics g) { super.paintComponent(g); if(image != null) { g.drawImage(image, 0, 0, this); } } } trashgod Verify that you invoke setVisible() after adding components and calling pack() , as discussed in this related example . You may also need to adopt an