paintcomponent

Drawing shapes in JPanel over an image

若如初见. 提交于 2019-11-30 18:18:26
问题 I need to draw shapes (circle or free line) over an image that is shown in a JLabel of a JPanel. I based my code on the questions How to draw thin line with no gap while dragging the cursor? and Draw a circle using 2 mouse clicks. The code is bellow. The problem is that when I start drawing the image disappears and only reappears after I stop. If I comment the line super.paintComponent(g); that doesnt happen but when I draw the circle it maintains a path of the previous positions. public

Circular Progress Bar for Java Swing not working

╄→гoц情女王★ 提交于 2019-11-30 08:42:45
问题 i've discovered this test project from Oracle site because i want to add a circular progress bar in my project. I'm developing the application with Netbeans, and when i start the application, the JPanel where the circle should be.... disappaer. I've removed all the code that is not useful to solve this problem and i've got this code: import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.beans.PropertyChangeEvent; import javax.swing.*; import

Slow movement using paintComponent method

耗尽温柔 提交于 2019-11-29 18:18:34
I decided to re-write my game using Swing's painting technique paintComponent() method(someone on SO actually told me to use this method). I decided to use JPanel as the game's base instead of Canvas. My previous written game uses a Canvas but the game could not show up on my 64 bit desktop but could show up on my 32 bit labtop which is why this game had to be re-written. Problem now is, while the ship's movement works, the drawing seems awfully slow(unless it is my laptop problem?) compare to what I did before which was using AWT's double buffering drawing technique. I spend a whole day but

Multiple problems regarding Java paint program while painting

牧云@^-^@ 提交于 2019-11-29 18:06:09
I have a Java paint program, and I've got two problems to do with it. Both problems are relatively simple, and just regard how the mouse input is handled and how the image uses colors. Here's a photo of the app: So here's my first problem: As you can see, by the look of the app, there's a spray of dots on the paint area. Each of those dots is a mouseclick. The program does not recognize when a user is holding down the mouse button, so you have to click individually. This is obviously counterproductive, user-unfriendly and unacceptable. Now, how I fix this, I'm not sure. I've tried using a

Can you add a mouse listener to an object in paintComponent

痴心易碎 提交于 2019-11-29 17:13:38
Just a quick question, I was wondering if there is any way to add a mouse listener to a paint component? For example, say you drew a rectangle, could you make it so when you click that rectangle, it will then do something. public void paintComponent(Graphics g) { g.drawRect(50, 50, 20, 20); //Do something when this rectangle is clicked on } To answer your topic question: Can you add a mouse listener to an object in paintComponent Yes, sort of. You can't do anything to objects that are declared within the paintComponent method, since their scope is limited to that method, but you can react to

Ball Animation in Swing

老子叫甜甜 提交于 2019-11-29 14:40:35
The Problem is when I resize the JFrame the animation goes on with it's Pre supplied dimensions for JComponent . Is there a way that I can update my width and height variables as I resize the JFrame , so that the Animation can run along with the new Co-ordinates. In simpler terms, say the JComponent has initial width = 300 and height = 300 , so the BALL moves inside these specified Co-ordinates . Now if I resize my JFrame , the size for the JComponent still remains as is i.e. width = 300 and height = 300 , but what I was hoping for, is a way to modify these variables with the present size of

how can i put a JButton on an image?

拟墨画扇 提交于 2019-11-29 07:43:32
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 please? here is the code public final class Test extends JComponent { private Image background; private

Circular Progress Bar for Java Swing not working

倾然丶 夕夏残阳落幕 提交于 2019-11-29 07:37:30
i've discovered this test project from Oracle site because i want to add a circular progress bar in my project. I'm developing the application with Netbeans, and when i start the application, the JPanel where the circle should be.... disappaer. I've removed all the code that is not useful to solve this problem and i've got this code: import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.beans.PropertyChangeEvent; import javax.swing.*; import javax.swing.plaf.LayerUI; public class Loading_Test extends javax.swing.JFrame { static final

Painting in a BufferedImage inside Swing

六眼飞鱼酱① 提交于 2019-11-29 05:22:32
Im working on a paint application written in java and running into a few (more) problems. I have a gui and a working program(kinda), my only problem is that the lines and graphics that the user draws are not saved(disappear after next one is drawn). From a past question i learned that i will need to use a BufferedImage to store the drawings then paint that inside my paint class. My questions are, Can anyone provide a basic explanation/example of how to use a bufferedimage to store and paint the drawing and if i need to, how will i pass the color and thickness of the drawn line into a stored

Java: Add Background image to frame [duplicate]

二次信任 提交于 2019-11-28 14:50:16
Possible Duplicate: java swing background image drawing your own buffered image on frame I am trying to add a back ground image to my frame, but nothing that I have done works. I designed a slot machine consisting of several panels added to the container. Now, I am trying to add a nice background to the frame. I tried using the paint method. But, since I am already using the paint method to paint the reel images, it is not working on the background. I also tried adding a JLabel, but when I do it overwrites everything or get overwritten depending on how I call it. Following is my code; any help