paintcomponent

Visual Artifacts appearing on JPanel

泪湿孤枕 提交于 2019-11-28 14:15:51
I am trying to create a program with 2 JPanel using BorderLayout . The center panel is for random drawing of rectangle while the south panel is for the buttons. I am getting a weird image of the button on the top left corner of the JFrame whenever I hover the mouse cursor over the North or South button. I did some research and found out that this could be the reason for having a transparent background. I tried using super.paintComponent(g) for the panel but the rest of the rectangles drawn earlier disappear. I need the rectangles to stay in the JPanel but not the weird image on the top left. I

Having two objects move in an SwingBot

懵懂的女人 提交于 2019-11-28 14:09:01
I'm trying to make it so that both shapes move when the commands are pressed. My question is: How do I get the blue polygon to move along with the yellow rectangle? I can't seem to figure it out, no matter what I do. Any help is appreciated! thanks! EDIT: Removed Timer Code (it is for something different) import javax.swing.JFrame; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Rectangle; import javax.swing.JComponent; import java.awt.geom.Ellipse2D; import java.awt.geom.Line2D; import java.awt.Color; import java.awt.Polygon; import java.util.Scanner; import java.awt

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

…衆ロ難τιáo~ 提交于 2019-11-28 14:03:29
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 the JComponent and JFrame classes work in swing. I thought maybe the problem might also be focus; I don

Why gif animation doesn't animate when using it in paintComponent()?

纵然是瞬间 提交于 2019-11-28 13:32:23
I'm using paintComponent() to paint a gif animated image at the backgound of JPanel . It shows up the gif but doesn't animate. I use java 1.5 and i know that i can use label with icon. Does any body know why and how to fix it? private static class CirclePanel extends JPanel { ImageIcon imageIcon = new ImageIcon(BarcodeModel.class.getResource("verify.gif")); Point point = f.getLocation(); protected void paintComponent(Graphics g) { Graphics gc = g.create(); Graphics2D g2d = (Graphics2D) g.create(); g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2d

about drawing a Polygon in java

拈花ヽ惹草 提交于 2019-11-28 13:27:14
hi there i'm trying to improve myself about java2D and first of all i'm dealing with drawing polygons. However, i can not see the polygon on frame. I read some tutorials and examples but as i said i face with problems. here is the sample code of drawing a polygon; import java.awt.Color; import java.awt.Graphics; import java.awt.Polygon; import javax.swing.JFrame; public class jRisk extends JFrame { private JFrame mainMap; private Polygon poly; public jRisk(){ initComponents(); } private void initComponents(){ mainMap = new JFrame(); mainMap.setSize(800, 600); mainMap.setResizable(false);

Delay is not working in java graphics

拟墨画扇 提交于 2019-11-28 13:11:30
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); // delay try { Thread.sleep(10); } catch(Throwable e) { System.out.println(e.getMessage()); } } } The list

Slow movement using paintComponent method

女生的网名这么多〃 提交于 2019-11-28 12:57:04
问题 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

Java JPanel getGraphics()

喜夏-厌秋 提交于 2019-11-28 12:30:28
Since Java only supports single inheritance , I desire to paint directly on an instance of a JPanel that is a member of the class Panel . I grab an instance of Graphics from the member and then paint whatever I desire onto it. How can I not inherit from JComponent or JPanel and still utilize getGraphics() for painting on this without overriding public void paintComponent(Graphics g) ? private class Panel { private JPanel panel; private Grahics g; public Panel() { panel = new JPanel(); } public void draw() { g = panel.getGraphics(); g.setColor(Color.CYAN); g.draw(Some Component); panel.repaint(

Program not painting screen properly

若如初见. 提交于 2019-11-28 12:25:25
问题 I've been constructing a short program that basically draws a spaceship on a JPanel and listens for keys that tell the program to shoot a bullet. The problem is that it's not even painting the spaceship or the bullets on the screen. I also suspect that the KeyBindings may not be working as that was a previous problem (that I may or may not have fixed), but the main issue at hand is still the fact that my screen isn't being painted. Here is my code: public enum Direction { LEFT, RIGHT, SPACE }

Swing - paintComponent method not being called

无人久伴 提交于 2019-11-28 08:02:39
问题 i simply implemented class that inherits JPanel like below public class Orpanel extends JPanel { .... @Override public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D)g; g2d.setPaint(tpaint); g2d.fill(rect); } .... } class Orpanel is loading image and adjusting it's own size. here's question. Calling JFrame's setContentpane(Orpanel's instance) makes it works fine but when i attach Orpanel to JFrame calling add() method instead of setContentpane (i know