paintcomponent

Multiple problems regarding Java paint program while painting

岁酱吖の 提交于 2019-12-18 09:44:20
问题 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

Can you add a mouse listener to an object in paintComponent

旧巷老猫 提交于 2019-12-18 09:37:23
问题 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 } 回答1: 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

Ball Animation in Swing

試著忘記壹切 提交于 2019-12-18 08:49:33
问题 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

Painting in a BufferedImage inside Swing

落花浮王杯 提交于 2019-12-18 04:27:06
问题 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

JPanel Graphics clearing and repainting?

你。 提交于 2019-12-18 04:16:06
问题 I have a JPanel with a paintComponent() function. I'll call it once, then when the user clicks a different JButton , I'll set some flag and want to call this function again as it will do something slightly different after the flag is set. So here's what I'm wondering: how do I clear the existing stuff from paintComponent ? And to redraw, do I just call paintComponent again? Currently I'm trying the following: flag2 = true; repaint(); //I expect (want) paintComponent to be called again In

How does one properly handle keypresses and repainting of a JComponent in the context of moving a ball around a screen?

ぐ巨炮叔叔 提交于 2019-12-17 20:53:21
问题 I thought I would try and write a program that would paint a ball, and when the arrow keys are pressed, would move the ball around the screen in the direction pressed. First I started off to try and make a program that would just do "UP" arrow key motion. I've looked around for a solution, and just can't figure out what's wrong with this code. I don't know if it's a problem with my input and action maps (i.e., a problem with the program recognizing key presses) or if it's a problem with how

Delay is not working in java graphics

五迷三道 提交于 2019-12-17 20:42:05
问题 This is a code for drawing points on calculated locations by Bresenham's algorithm: public void drawBresenhamPoints(Graphics2D g2, List<Point> bresenham) throws InterruptedException { Graphics2D g = (Graphics2D) g2; if(bresenham == null) return; g.setColor(Color.DARK_GRAY); for(int i = 0; i < bresenham.size(); i = i+20) { int x = bresenham.get(i).x - pointWidth1/2; int y = bresenham.get(i).y - pointWidth1/2; int ovalW = pointWidth1; int ovalH = pointWidth1; g.fillOval(x, y, ovalW, ovalH); //

Not showing graphics in JPanel which is added to another JPanel

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-17 14:54:19
问题 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);

Change the angle/position of a drawing with a algorithm in Java

喜夏-厌秋 提交于 2019-12-17 10:09:11
问题 Hello I am very curious how to solve this problem: I created a pacman with the fillArc, drawArc method in Java and I have a pacman guy on my screen now that is always looking to the right no matter what direction it goes.. my question is.. is there a way to change the object by degrees or flip it horizontally in Java? i tried to use AffineTransform but i don't get where i want with the documentation... How should I be able to achieve this using a switch statement? I tried to do the following

Switch imageIcon in java?

一个人想着一个人 提交于 2019-12-17 10:04:52
问题 I have many planes(threads) that move in window, and I want switch the ImageIcon according to the direction of the plane. For example: if a plane goes to the right, the imageIcon of the plane is right and then plane goes to the left, exchange the imageIcon for the plane is left. How can I do that in method paintComponent? Sorry for my bad english. 回答1: If you're talking about swapping the ImageIcon displayed by a JLabel, then you should not switch ImageIcons in paintComponent but rather