@Override public void paint(Graphics g) { // Paint the gradient g.drawImage(this.image, 0, 0, null); int targetX = targetPosition.x - (SELECTOR_RADIUS / 2); int targetY = targetPosition.y - (SELECTOR_RADIUS / 2); // Paint the target (selector) g.setColor(Color.WHITE); g.fillOval(targetX, targetY, SELECTOR_RADIUS, SELECTOR_RADIUS); g.setColor(Color.BLACK); g.drawOval(targetX, targetY, SELECTOR_RADIUS, SELECTOR_RADIUS); }
int diameter = originalSize.height - (gap * 2); g.setColor(circleColor); g.fillOval(x, y, diameter, diameter); MyButton button = new MyButton("Hello, World!"); JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(400, 400); contentPane.add(button); frame.setVisible(true);
JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLayout(new BorderLayout()); frame.add(new Balls()); g.setColor(color); g.fillOval(0, 0, 30, 30); //adds color to circle g.setColor(Color.black); g2.drawOval(0, 0, 30, 30); //draws circle
JFrame workingManager = new JFrame("Hello"); Graphics g = workingManager.getGraphics(); JPanel jp = (JPanel) workingManager.getContentPane(); workingManager.paintComponents(g); g.fillOval(0, 0, 30, 30); g.drawOval(0, 50, 30, 30); g.setColor(Color.CYAN); workingManager.setSize(500, 500); workingManager.setVisible(true);
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class NewJApplet extends JApplet { private Color currentColor = Color.YELLOW; @Override public void paint(Graphics g) { super.paint(g); g.setColor(currentColor); g.fillOval(100, 100, 100, 100); } private void jButton2ActionPerformed(ActionEvent evt){ currentColor = Color.BLUE; repaint(); } }
import javax.swing.*; import java.awt.*; public class DrawGraphics extends JFrame{ public DrawGraphics(String title) throws HeadlessException { super(title); InitialElements(); } private void InitialElements(){ setSize(300, 250); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); // This one does not work // getContentPane().setBackground(new Color(70, 80, 70)); } public void paint(Graphics draw){ //Here you can perform any drawing like an oval... draw.fillOval(40, 40, 60, 50); getContentPane().setBackground(new Color(70,80,70)); } }
g2.setColor(GRAPH_COLOR); g2.setStroke(GRAPH_STROKE); for (int i = 0; i < graphPoints.size() - 1; i++) { g2.setColor(GRAPH_POINT_COLOR); for (int i = 0; i < graphPoints.size(); i++) { int x = graphPoints.get(i).x - GRAPH_POINT_WIDTH / 2; int ovalW = GRAPH_POINT_WIDTH; int ovalH = GRAPH_POINT_WIDTH; g2.fillOval(x, y, ovalW, ovalH); JFrame frame = new JFrame("DrawGraph"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(mainPanel); frame.pack(); frame.setLocationByPlatform(true); frame.setVisible(true);
/** Draw the entire panel from the state */ public void drawState() { // clear(); Map.Entry entry; Point pt; Color col; synchronized(state) { for(Iterator it=state.entrySet().iterator(); it.hasNext();) { entry=(Map.Entry)it.next(); pt=(Point)entry.getKey(); col=(Color)entry.getValue(); gr.setColor(col); gr.fillOval(pt.x, pt.y, 10, 10); } } repaint(); }
public class CirclePanel extends JPanel { public static void main(String[] args) throws Exception { JFrame f = new JFrame(); f.setContentPane(new CirclePanel()); f.setSize(700,500); f.setVisible(true); } public void paint(Graphics g) { super.paint(g); //Draws the line g.drawOval(0,0,this.getWidth(), this.getHeight()); //draws filled circle g.setColor(Color.red); g.fillOval(0,0,this.getWidth(), this.getHeight()); } }
import java.awt.*; import java.awt.geom.*; import javax.swing.*; public class CircleButton extends JButton { // Graphics g = this.getGraphics(); Color col = Color.pink; public CircleButton(){ //commented as unuseful.. super call is implicit if constructor has no arguments // super(); setContentAreaFilled(false); } protected void paintComponent(Graphics g) { g.setColor(this.color); g.fillOval(0, 0, getSize().width-1, getSize().height-1); super.paintComponent(g); } public void changeColor(Color c) { this.color = Color.blue; //only change the color. Let paintComponent paint this.repaint(); } }
g2.setColor(Color.WHITE); g2.fillRect(padding + labelPadding, padding, getWidth() - (2 * padding) - labelPadding, getHeight() - 2 * padding - labelPadding); g2.setColor(Color.BLACK); int y1 = y0; if (scores.size() > 0) { g2.setColor(gridColor); g2.drawLine(padding + labelPadding + 1 + pointWidth, y0, getWidth() - padding, y1); g2.setColor(Color.BLACK); int ovalW = pointWidth; int ovalH = pointWidth; g2.fillOval(x, y, ovalW, ovalH); this.scores = scores; invalidate(); this.repaint(); JFrame frame = new JFrame("DrawGraph"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(mainPanel); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true);
/** Draw the entire panel from the state */ public void drawState() { // clear(); Map.Entry entry; Point pt; Color col; synchronized(state) { for(Iterator it=state.entrySet().iterator(); it.hasNext();) { entry=(Map.Entry)it.next(); pt=(Point)entry.getKey(); col=(Color)entry.getValue(); gr.setColor(col); gr.fillOval(pt.x, pt.y, 10, 10); } } repaint(); }
g.drawImage(background, 0, 0, this); double theta = 2 * Math.PI * index++ / 64; g.setColor(Color.blue); rect.setRect( (int) (Math.sin(theta) * w / 3 + w / 2 - RADIUS), (int) (Math.cos(theta) * h / 3 + h / 2 - RADIUS), 2 * RADIUS, 2 * RADIUS); g.fillOval(rect.x, rect.y, rect.width, rect.height); g.setColor(Color.white); if (frameCount == FRAMES) { averageTime = totalTime / FRAMES; this.repaint(); Dimension d = field.getPreferredSize(); field.setBounds(e.getX(), e.getY(), d.width, d.height); add(field); Graphics2D g = background.createGraphics(); g.clearRect(0, 0, w, h); g.setColor(Color.green.darker()); for (int i = 0; i < 128; i++) { g.drawLine(w / 2, h / 2, r.nextInt(w), r.nextInt(h));
/** * Adds pixel to queue and calls repaint() whenever we have MAX_ITEMS pixels in the queue * or when MAX_TIME msecs have elapsed (whichever comes first). The advantage compared to just calling * repaint() after adding a pixel to the queue is that repaint() can most often draw multiple points * at the same time. */ public void drawPoint(DrawCommand c) { if(c == null || gr == null) return; Color col=new Color(c.rgb); gr.setColor(col); gr.fillOval(c.x, c.y, 10, 10); repaint(); if(state != null) { synchronized(state) { state.put(new Point(c.x, c.y), col); } } }
jpb.setForeground(Color.blue); jpb.setIndeterminate(true); this.add(jpb); RenderingHints.VALUE_ANTIALIAS_ON); r = getBox(r); g.setColor(progressBar.getForeground()); g.fillOval(r.x, r.y, r.width, r.height); JFrame f = new JFrame("ProgressBarUITest"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.add(this); f.pack(); f.setLocationRelativeTo(null); f.setVisible(true);
/** * Adds pixel to queue and calls repaint() whenever we have MAX_ITEMS pixels in the queue * or when MAX_TIME msecs have elapsed (whichever comes first). The advantage compared to just calling * repaint() after adding a pixel to the queue is that repaint() can most often draw multiple points * at the same time. */ public void drawPoint(DrawCommand c) { if(c == null || gr == null) return; Color col=new Color(c.rgb); gr.setColor(col); gr.fillOval(c.x, c.y, 10, 10); repaint(); if(state != null) { synchronized(state) { state.put(new Point(c.x, c.y), col); } } }
JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.add(new MainPanel()); f.pack(); f.setLocationRelativeTo(null); f.setVisible(true); Control control = new Control(model, view); JLabel label = new JLabel("Guess what color!", JLabel.CENTER); this.add(label, BorderLayout.NORTH); this.add(view, BorderLayout.CENTER); this.add(control, BorderLayout.SOUTH); PieceButton pb = (PieceButton) e.getSource(); icon.color = pb.piece.color; label.repaint(); model.check(pb.piece); RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2d.setColor(color); g2d.fillOval(x, y, size, size);
@Override public void paintIcon(Component c, Graphics g, int x, int y) { g.setColor(TangoColorFactory.ALUMINIUM_6); g.fillOval(x, y, DIAMETER, DIAMETER); g.setColor(Color.WHITE); g.fillArc(x, y, DIAMETER, DIAMETER, 90, -(360 * period.getDuration() / maximumPeriodDuration)); g.setColor(TangoColorFactory.ALUMINIUM_6); g.drawOval(x, y, DIAMETER, DIAMETER); }